Skip to content

Commit e1bea0a

Browse files
author
Jicheng Lu
committed
refine select component
1 parent 608aa06 commit e1bea0a

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

src/lib/common/Select.svelte

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,18 @@
7272
/** @type {boolean} */
7373
let loading = false;
7474
75+
/**
76+
* @type {number | undefined}
77+
*/
78+
let timer;
79+
7580
onMount(() => {
7681
initOptions();
7782
});
7883
7984
$: {
8085
innerOptions = verifySelectedOptions(innerOptions, selectedValues);
8186
refOptions = verifySelectedOptions(innerOptions, selectedValues);
82-
applySearchFilter();
83-
changeDisplayText();
8487
}
8588
8689
$: {
@@ -105,36 +108,39 @@
105108
...newOptions
106109
];
107110
108-
changeDisplayText();
109-
} else {
110-
applySearchFilter();
111-
changeDisplayText();
112111
}
113112
} else {
114113
innerOptions = verifySelectedOptions(options, selectedValues);
115114
refOptions = verifySelectedOptions(options, selectedValues);
115+
}
116+
}
117+
118+
$: {
119+
if (innerOptions && refOptions) {
116120
applySearchFilter();
117121
changeDisplayText();
118122
}
119123
}
120124
121-
122125
function initOptions() {
123-
innerOptions = options.map(x => {
126+
const newInnerOptions = options.map(x => {
124127
return {
125128
label: x.label,
126129
value: x.value,
127130
checked: false
128131
}
129132
});
130133
131-
refOptions = options.map(x => {
134+
const newRefOptions = options.map(x => {
132135
return {
133136
label: x.label,
134137
value: x.value,
135138
checked: false
136139
}
137140
});
141+
142+
innerOptions = newInnerOptions;
143+
refOptions = newRefOptions;
138144
}
139145
140146
/**
@@ -167,8 +173,11 @@
167173
/** @param {any} e */
168174
function changeSearchValue(e) {
169175
searchValue = e.target.value || '';
170-
applySearchFilter();
171-
verifySelectAll();
176+
clearTimeout(timer);
177+
timer = setTimeout(() => {
178+
applySearchFilter();
179+
verifySelectAll();
180+
}, 500);
172181
}
173182
174183
function applySearchFilter() {
@@ -186,7 +195,7 @@
186195
* @param {any} option
187196
*/
188197
function checkOption(e, option) {
189-
innerOptions = innerOptions.map(x => {
198+
const newInnerOptions = innerOptions.map(x => {
190199
const item = { ...x };
191200
if (item.value == option.value) {
192201
item.checked = e == null ? !item.checked : e.target.checked;
@@ -196,7 +205,7 @@
196205
return item;
197206
});
198207
199-
refOptions = refOptions.map(x => {
208+
const newRefOptions = refOptions.map(x => {
200209
const item = { ...x };
201210
if (item.value == option.value) {
202211
item.checked = e == null ? !item.checked : e.target.checked;
@@ -206,7 +215,9 @@
206215
return item;
207216
});
208217
209-
changeDisplayText();
218+
innerOptions = newInnerOptions;
219+
refOptions = newRefOptions;
220+
210221
sendEvent();
211222
hideOptionList();
212223
}
@@ -219,7 +230,6 @@
219230
});
220231
221232
syncChangesToRef(selectAllChecked);
222-
changeDisplayText();
223233
sendEvent();
224234
}
225235
@@ -320,15 +330,17 @@
320330
}
321331
322332
function clearSelection() {
323-
innerOptions = innerOptions.map(x => {
333+
const newInnerOptions = innerOptions.map(x => {
324334
return { ...x, checked: false }
325335
});
326336
327-
refOptions = refOptions.map(x => {
337+
const newRefOptions = refOptions.map(x => {
328338
return { ...x, checked: false }
329339
});
330340
331-
changeDisplayText();
341+
innerOptions = newInnerOptions;
342+
refOptions = newRefOptions;
343+
332344
sendEvent();
333345
hideOptionList();
334346
}

0 commit comments

Comments
 (0)