gyyg-components 0.4.2 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gyyg-components",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "private": false,
5
5
  "main": "lib/gyyg-components.umd.js",
6
6
  "scripts": {
@@ -1,21 +1,20 @@
1
1
  <template>
2
2
  <el-select
3
3
  v-model="selected"
4
- @clear="clear"
4
+ @clear="handleClear"
5
5
  @change="handleChange"
6
6
  :placeholder="placeholder"
7
- :multiple="multiple || false"
7
+ :multiple="multiple"
8
8
  :clearable="clearable"
9
9
  :disabled="disabled"
10
10
  :size="size"
11
11
  v-bind="$attrs"
12
12
  :filterable="filterable"
13
- :value="selected"
14
13
  style="width: 100%"
15
14
  >
16
15
  <el-option
17
- v-for="item in option"
18
- :key="item[props.value]"
16
+ v-for="item in optionList"
17
+ :key="getItemKey(item)"
19
18
  :label="item[props.label]"
20
19
  :value="item[props.value]"
21
20
  :disabled="item.disabled"
@@ -35,9 +34,11 @@ export default {
35
34
  required: true,
36
35
  },
37
36
  multiple: {
37
+ type: Boolean,
38
38
  default: false,
39
39
  },
40
40
  props: {
41
+ type: Object,
41
42
  default() {
42
43
  return {
43
44
  label: "name",
@@ -51,23 +52,41 @@ export default {
51
52
  },
52
53
  clearable: {
53
54
  type: Boolean,
55
+ default: true,
54
56
  },
55
57
  disabled: {
56
58
  type: Boolean,
57
- defalut: false,
59
+ default: false,
60
+ },
61
+ size: {
62
+ type: String,
63
+ default: undefined,
58
64
  },
59
- size: String,
60
65
  filterable: {
61
66
  type: Boolean,
62
- defalut: false,
67
+ default: false,
68
+ },
69
+ isSelectFirst: {
70
+ type: Boolean,
71
+ default: false,
63
72
  },
64
73
  },
65
74
  data() {
66
75
  return {
67
- selected: "",
68
- option: [],
76
+ selected: null,
77
+ optionList: [],
78
+ hasAutoSelected: false,
69
79
  };
70
80
  },
81
+ computed: {
82
+ optionMap() {
83
+ const map = new Map();
84
+ this.optionList.forEach((item) => {
85
+ map.set(item[this.props.value], item);
86
+ });
87
+ return map;
88
+ },
89
+ },
71
90
  watch: {
72
91
  value: {
73
92
  handler(val) {
@@ -76,45 +95,61 @@ export default {
76
95
  immediate: true,
77
96
  },
78
97
  options: {
79
- handler: function () {
80
- if (!Array.isArray(this.options) && this.options) {
81
- if (this.options instanceof Promise) {
82
- this.options.then((val) => {
83
- this.option = val.data || val;
84
- console.log(this.option);
85
- });
86
- }
87
- if (typeof this.options == "function") {
88
- this.options().then((val) => {
89
- this.option = val.data || val;
90
- });
91
- }
92
- } else {
93
- this.option = this.options;
94
- }
98
+ handler: function (newOptions) {
99
+ this.processOptions(newOptions);
95
100
  },
96
101
  deep: true,
97
102
  immediate: true,
98
103
  },
99
104
  },
100
105
  methods: {
101
- handleChange(val) {
102
- if (this.multiple) {
103
- this.selected = val;
104
- let info = this.option.filter((item) => val.includes(item[this.props.value]));
105
- this.$emit("change", val, info);
106
- } else {
107
- this.selected = val;
108
- let info = this.option.filter((item) => item[this.props.value] == val);
109
- this.$emit("change", val, info.length > 0 ? info[0] : {});
106
+ async processOptions(options) {
107
+ try {
108
+ let result = options;
109
+
110
+ if (options instanceof Promise) {
111
+ result = await options;
112
+ } else if (typeof options === "function") {
113
+ result = await options();
114
+ }
115
+
116
+ this.optionList = Array.isArray(result) ? result : (result.data || []);
117
+
118
+ if (this.isSelectFirst && !this.hasAutoSelected && !this.value && this.optionList.length > 0) {
119
+ this.hasAutoSelected = true;
120
+ this.handleChange(this.optionList[0][this.props.value]);
121
+ }
122
+ } catch (error) {
123
+ console.error("Failed to process options:", error);
124
+ this.optionList = [];
110
125
  }
111
-
112
- this.$emit("input", this.selected);
126
+ },
127
+
128
+ getItemKey(item) {
129
+ return item[this.props.value];
130
+ },
131
+
132
+ getOptionInfo(value) {
133
+ if (this.multiple && Array.isArray(value)) {
134
+ return value.map((val) => this.optionMap.get(val)).filter(Boolean);
135
+ }
136
+ return this.optionMap.get(value) || {};
137
+ },
138
+
139
+ handleChange(val) {
140
+ this.selected = val;
141
+ const info = this.getOptionInfo(val);
142
+
143
+ this.$emit("change", val, info);
144
+ this.$emit("input", val);
113
145
  this.$emit("update:value", val);
114
146
  },
115
- clear() {
116
- this.selected = "";
147
+
148
+ handleClear() {
149
+ this.selected = this.multiple ? [] : null;
117
150
  this.$emit("clear");
151
+ this.$emit("input", this.selected);
152
+ this.$emit("update:value", this.selected);
118
153
  },
119
154
  },
120
155
  };
@@ -122,4 +157,4 @@ export default {
122
157
 
123
158
  <style scoped>
124
159
  /* 你可以在这里添加自定义样式 */
125
- </style>
160
+ </style>