|
72 | 72 | /** @type {boolean} */ |
73 | 73 | let loading = false; |
74 | 74 |
|
| 75 | + /** |
| 76 | + * @type {number | undefined} |
| 77 | + */ |
| 78 | + let timer; |
| 79 | +
|
75 | 80 | onMount(() => { |
76 | 81 | initOptions(); |
77 | 82 | }); |
78 | 83 |
|
79 | 84 | $: { |
80 | 85 | innerOptions = verifySelectedOptions(innerOptions, selectedValues); |
81 | 86 | refOptions = verifySelectedOptions(innerOptions, selectedValues); |
82 | | - applySearchFilter(); |
83 | | - changeDisplayText(); |
84 | 87 | } |
85 | 88 |
|
86 | 89 | $: { |
|
105 | 108 | ...newOptions |
106 | 109 | ]; |
107 | 110 |
|
108 | | - changeDisplayText(); |
109 | | - } else { |
110 | | - applySearchFilter(); |
111 | | - changeDisplayText(); |
112 | 111 | } |
113 | 112 | } else { |
114 | 113 | innerOptions = verifySelectedOptions(options, selectedValues); |
115 | 114 | refOptions = verifySelectedOptions(options, selectedValues); |
| 115 | + } |
| 116 | + } |
| 117 | +
|
| 118 | + $: { |
| 119 | + if (innerOptions && refOptions) { |
116 | 120 | applySearchFilter(); |
117 | 121 | changeDisplayText(); |
118 | 122 | } |
119 | 123 | } |
120 | 124 |
|
121 | | -
|
122 | 125 | function initOptions() { |
123 | | - innerOptions = options.map(x => { |
| 126 | + const newInnerOptions = options.map(x => { |
124 | 127 | return { |
125 | 128 | label: x.label, |
126 | 129 | value: x.value, |
127 | 130 | checked: false |
128 | 131 | } |
129 | 132 | }); |
130 | 133 |
|
131 | | - refOptions = options.map(x => { |
| 134 | + const newRefOptions = options.map(x => { |
132 | 135 | return { |
133 | 136 | label: x.label, |
134 | 137 | value: x.value, |
135 | 138 | checked: false |
136 | 139 | } |
137 | 140 | }); |
| 141 | +
|
| 142 | + innerOptions = newInnerOptions; |
| 143 | + refOptions = newRefOptions; |
138 | 144 | } |
139 | 145 |
|
140 | 146 | /** |
|
167 | 173 | /** @param {any} e */ |
168 | 174 | function changeSearchValue(e) { |
169 | 175 | searchValue = e.target.value || ''; |
170 | | - applySearchFilter(); |
171 | | - verifySelectAll(); |
| 176 | + clearTimeout(timer); |
| 177 | + timer = setTimeout(() => { |
| 178 | + applySearchFilter(); |
| 179 | + verifySelectAll(); |
| 180 | + }, 500); |
172 | 181 | } |
173 | 182 |
|
174 | 183 | function applySearchFilter() { |
|
186 | 195 | * @param {any} option |
187 | 196 | */ |
188 | 197 | function checkOption(e, option) { |
189 | | - innerOptions = innerOptions.map(x => { |
| 198 | + const newInnerOptions = innerOptions.map(x => { |
190 | 199 | const item = { ...x }; |
191 | 200 | if (item.value == option.value) { |
192 | 201 | item.checked = e == null ? !item.checked : e.target.checked; |
|
196 | 205 | return item; |
197 | 206 | }); |
198 | 207 |
|
199 | | - refOptions = refOptions.map(x => { |
| 208 | + const newRefOptions = refOptions.map(x => { |
200 | 209 | const item = { ...x }; |
201 | 210 | if (item.value == option.value) { |
202 | 211 | item.checked = e == null ? !item.checked : e.target.checked; |
|
206 | 215 | return item; |
207 | 216 | }); |
208 | 217 |
|
209 | | - changeDisplayText(); |
| 218 | + innerOptions = newInnerOptions; |
| 219 | + refOptions = newRefOptions; |
| 220 | +
|
210 | 221 | sendEvent(); |
211 | 222 | hideOptionList(); |
212 | 223 | } |
|
219 | 230 | }); |
220 | 231 |
|
221 | 232 | syncChangesToRef(selectAllChecked); |
222 | | - changeDisplayText(); |
223 | 233 | sendEvent(); |
224 | 234 | } |
225 | 235 |
|
|
320 | 330 | } |
321 | 331 |
|
322 | 332 | function clearSelection() { |
323 | | - innerOptions = innerOptions.map(x => { |
| 333 | + const newInnerOptions = innerOptions.map(x => { |
324 | 334 | return { ...x, checked: false } |
325 | 335 | }); |
326 | 336 |
|
327 | | - refOptions = refOptions.map(x => { |
| 337 | + const newRefOptions = refOptions.map(x => { |
328 | 338 | return { ...x, checked: false } |
329 | 339 | }); |
330 | 340 |
|
331 | | - changeDisplayText(); |
| 341 | + innerOptions = newInnerOptions; |
| 342 | + refOptions = newRefOptions; |
| 343 | +
|
332 | 344 | sendEvent(); |
333 | 345 | hideOptionList(); |
334 | 346 | } |
|
0 commit comments