3
0

VersionSelect.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <el-select
  3. v-model="selected"
  4. class="version-select"
  5. :placeholder="placeholder"
  6. filterable
  7. :clearable="clearable"
  8. :disabled="disabled"
  9. @change="select"
  10. >
  11. <el-option
  12. v-for="item in optionList"
  13. :key="item.id"
  14. :value="item.id"
  15. :label="item.name"
  16. :disabled="disabledOptions.includes(item.id)"
  17. >
  18. </el-option>
  19. </el-select>
  20. </template>
  21. <script>
  22. import { appVersionList } from "../modules/admin/api";
  23. export default {
  24. name: "version-select",
  25. props: {
  26. disabled: { type: Boolean, default: false },
  27. placeholder: { type: String, default: "请选择版本" },
  28. value: { type: [Number, String], default: "" },
  29. clearable: { type: Boolean, default: true },
  30. appId: { type: [String, Number], default: "" },
  31. disabledOptions: {
  32. type: Array,
  33. default() {
  34. return [];
  35. },
  36. },
  37. manualFetch: {
  38. type: Boolean,
  39. default: false,
  40. },
  41. selectDefault: {
  42. type: Boolean,
  43. default: false,
  44. },
  45. filterHandle: {
  46. type: Function,
  47. default: null,
  48. },
  49. },
  50. data() {
  51. return {
  52. optionList: [],
  53. selected: "",
  54. };
  55. },
  56. watch: {
  57. value: {
  58. immediate: true,
  59. handler(val) {
  60. this.selected = val;
  61. },
  62. },
  63. appId(val, oldval) {
  64. if (this.manualFetch) return;
  65. if (val !== oldval) {
  66. this.search();
  67. this.$emit("input", "");
  68. this.$emit("change", {});
  69. }
  70. },
  71. },
  72. created() {
  73. if (!this.manualFetch) this.search();
  74. },
  75. methods: {
  76. async search() {
  77. this.optionList = [];
  78. // if (!this.appId) return;
  79. const res = await appVersionList({
  80. appId: this.appId,
  81. pageNumber: 1,
  82. pageSize: 100,
  83. });
  84. this.optionList = res.records;
  85. if (this.filterHandle) {
  86. this.optionList = this.filterHandle(this.optionList);
  87. }
  88. if (!this.selectDefault) return;
  89. if (this.value) {
  90. const option = this.optionList.find((item) => item.id === this.value);
  91. if (option) {
  92. this.$emit("input", this.value);
  93. this.$emit("change", option);
  94. } else {
  95. this.selectFirst();
  96. }
  97. } else {
  98. this.selectFirst();
  99. }
  100. },
  101. selectFirst() {
  102. if (!this.optionList.length) {
  103. this.selected = "";
  104. this.$emit("input", this.selected);
  105. this.$emit("change", {});
  106. return;
  107. }
  108. this.selected = this.optionList[0].id;
  109. this.$emit("input", this.selected);
  110. this.$emit("change", this.optionList[0]);
  111. },
  112. select() {
  113. this.$emit("input", this.selected);
  114. this.$emit(
  115. "change",
  116. this.optionList.find((item) => item.id === this.selected)
  117. );
  118. },
  119. },
  120. };
  121. </script>