gyyg-components 0.4.8 → 0.4.9

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.8",
3
+ "version": "0.4.9",
4
4
  "private": false,
5
5
  "main": "lib/gyyg-components.umd.js",
6
6
  "scripts": {
@@ -0,0 +1,5 @@
1
+ import MecInputDropdown from "./MecInputDropdown.vue";
2
+
3
+ MecInputDropdown.install = Vue => Vue.component(MecInputDropdown.name, MecInputDropdown);
4
+
5
+ export default MecInputDropdown;
@@ -0,0 +1,280 @@
1
+ <template>
2
+ <el-input
3
+ v-model="inputValue"
4
+ :placeholder="placeholder"
5
+ :type="inputTypeValue"
6
+ :disabled="disabled"
7
+ :readonly="readonly"
8
+ :size="size"
9
+ :maxlength="maxlength"
10
+ :show-word-limit="showWordLimit"
11
+ :clearable="clearable"
12
+ :prefix-icon="prefixIcon"
13
+ :suffix-icon="suffixIcon"
14
+ :rows="rows"
15
+ :autosize="autosize"
16
+ v-bind="$attrs"
17
+ v-feeInput="isFeeInput"
18
+ @input="handleInput"
19
+ @clear="clear"
20
+ @keyup.enter.native="handleEnter"
21
+ @blur="handleBlur"
22
+ >
23
+ <el-dropdown
24
+ slot="append"
25
+ trigger="click"
26
+ :disabled="disabled || optionList.length === 0"
27
+ @command="handleDropdownCommand"
28
+ >
29
+ <span
30
+ class="mec-input-dropdown__text"
31
+ :class="{ 'is-disabled': disabled }"
32
+ >
33
+ {{ selectedLabel }}
34
+ <i class="el-icon-arrow-down el-icon--right"></i>
35
+ </span>
36
+ <el-dropdown-menu slot="dropdown">
37
+ <el-dropdown-item
38
+ v-for="item in optionList"
39
+ :key="getItemKey(item)"
40
+ :command="item[props.value]"
41
+ :disabled="item.disabled"
42
+ >
43
+ {{ item[props.label] }}
44
+ </el-dropdown-item>
45
+ </el-dropdown-menu>
46
+ </el-dropdown>
47
+ </el-input>
48
+ </template>
49
+
50
+ <script>
51
+ import feeInput from "@/directive/mec-feeInput/index";
52
+
53
+ export default {
54
+ name: "MecInputDropdown",
55
+ directives: {
56
+ feeInput,
57
+ },
58
+ props: {
59
+ value: {
60
+ default: "",
61
+ },
62
+ dropdownValue: {
63
+ default: null,
64
+ },
65
+ options: {
66
+ required: true,
67
+ },
68
+ props: {
69
+ type: Object,
70
+ default() {
71
+ return {
72
+ label: "name",
73
+ value: "id",
74
+ };
75
+ },
76
+ },
77
+ placeholder: {
78
+ type: String,
79
+ default: "",
80
+ },
81
+ dropdownPlaceholder: {
82
+ type: String,
83
+ default: "请选择",
84
+ },
85
+ inputType: {
86
+ type: String,
87
+ default: "text",
88
+ },
89
+ type: {
90
+ type: String,
91
+ default: "text",
92
+ },
93
+ disabled: {
94
+ type: Boolean,
95
+ default: false,
96
+ },
97
+ readonly: {
98
+ type: Boolean,
99
+ default: false,
100
+ },
101
+ size: {
102
+ type: String,
103
+ default: "medium",
104
+ },
105
+ maxlength: {
106
+ type: [Number, String],
107
+ default: -1,
108
+ },
109
+ showWordLimit: {
110
+ type: Boolean,
111
+ default: false,
112
+ },
113
+ clearable: {
114
+ type: Boolean,
115
+ default: false,
116
+ },
117
+ prefixIcon: {
118
+ type: String,
119
+ default: "",
120
+ },
121
+ suffixIcon: {
122
+ type: String,
123
+ default: "",
124
+ },
125
+ rows: {
126
+ default: 2,
127
+ },
128
+ autosize: {
129
+ type: [Boolean, Object],
130
+ default: () => {
131
+ return { minRows: 2 };
132
+ },
133
+ },
134
+ isFeeInput: {
135
+ type: Boolean,
136
+ default: false,
137
+ },
138
+ },
139
+ data() {
140
+ return {
141
+ inputValue: "",
142
+ selectedDropdown: null,
143
+ optionList: [],
144
+ };
145
+ },
146
+ computed: {
147
+ inputTypeValue() {
148
+ return this.inputType === "textarea" ? "textarea" : this.type;
149
+ },
150
+ optionMap() {
151
+ const map = new Map();
152
+ this.optionList.forEach((item) => {
153
+ map.set(item[this.props.value], item);
154
+ });
155
+ return map;
156
+ },
157
+ selectedInfo() {
158
+ return this.optionMap.get(this.selectedDropdown) || {};
159
+ },
160
+ selectedLabel() {
161
+ return this.selectedInfo[this.props.label] || this.dropdownPlaceholder;
162
+ },
163
+ },
164
+ watch: {
165
+ value: {
166
+ handler(newValue) {
167
+ this.inputValue = newValue;
168
+ },
169
+ immediate: true,
170
+ },
171
+ dropdownValue: {
172
+ handler(newValue) {
173
+ this.selectedDropdown = newValue;
174
+ this.selectFirstIfNeeded();
175
+ },
176
+ immediate: true,
177
+ },
178
+ options: {
179
+ handler(newOptions) {
180
+ this.processOptions(newOptions);
181
+ },
182
+ deep: true,
183
+ immediate: true,
184
+ },
185
+ },
186
+ methods: {
187
+ async processOptions(options) {
188
+ try {
189
+ let result = options;
190
+
191
+ if (options instanceof Promise) {
192
+ result = await options;
193
+ } else if (typeof options === "function") {
194
+ result = await options();
195
+ }
196
+
197
+ this.optionList = Array.isArray(result) ? result : (result && result.data) || [];
198
+ this.selectFirstIfNeeded();
199
+ } catch (error) {
200
+ console.error("Failed to process options:", error);
201
+ this.optionList = [];
202
+ }
203
+ },
204
+ isEmptyValue(value) {
205
+ return value === undefined || value === null || value === "";
206
+ },
207
+ selectFirstIfNeeded() {
208
+ if (!this.optionList.length || !this.isEmptyValue(this.selectedDropdown)) {
209
+ return;
210
+ }
211
+
212
+ const firstOption = this.optionList[0];
213
+ this.setDropdownValue(firstOption[this.props.value]);
214
+ },
215
+ getItemKey(item) {
216
+ return item[this.props.value];
217
+ },
218
+ getOptionInfo(value) {
219
+ return this.optionMap.get(value) || {};
220
+ },
221
+ handleInput(val) {
222
+ let value = val;
223
+ if (this.inputType === "number") {
224
+ value = value.replace(/[^0-9]/g, "");
225
+ } else if (this.inputType === "chinese") {
226
+ value = value.replace(/[^\u4e00-\u9fa5]/g, "");
227
+ } else if (this.inputType === "english") {
228
+ value = value.replace(/[^a-zA-Z\s]/g, "");
229
+ } else if (this.inputType === "amount") {
230
+ value = value.replace(/[^0-9.]/g, "");
231
+ if (value.split(".").length > 2) {
232
+ value = value.replace(/\.+$/, "");
233
+ }
234
+ } else if (this.inputType === "englishAndNumber") {
235
+ value = value.replace(/[^a-zA-Z0-9\s]/g, "");
236
+ }
237
+
238
+ this.inputValue = value;
239
+ this.$emit("input", value);
240
+ this.$emit("update:value", value);
241
+ },
242
+ handleDropdownCommand(val) {
243
+ if (this.disabled) return;
244
+
245
+ this.setDropdownValue(val);
246
+ },
247
+ setDropdownValue(val) {
248
+ this.selectedDropdown = val;
249
+ console.log(val);
250
+ const info = this.getOptionInfo(val);
251
+
252
+ this.$emit("dropdown-change", val, info);
253
+ this.$emit("change", val, info);
254
+ this.$emit("update:dropdownValue", val);
255
+ },
256
+ clear() {
257
+ this.$emit("clear");
258
+ },
259
+ handleEnter() {
260
+ this.$emit("enter");
261
+ },
262
+ handleBlur() {
263
+ this.$emit("blur", this.inputValue);
264
+ },
265
+ },
266
+ };
267
+ </script>
268
+
269
+ <style lang="less" scoped>
270
+ .mec-input-dropdown__text {
271
+ cursor: pointer;
272
+ color: #606266;
273
+ white-space: nowrap;
274
+
275
+ &.is-disabled {
276
+ cursor: not-allowed;
277
+ color: #c0c4cc;
278
+ }
279
+ }
280
+ </style>