|
8 | 8 | /** @type {string} */ |
9 | 9 | export let tag; |
10 | 10 |
|
11 | | - /** @type {any[]} */ |
| 11 | + /** @type {{key: string, value: string}[]} */ |
12 | 12 | export let options = []; |
13 | 13 |
|
14 | 14 | /** @type {boolean} */ |
|
38 | 38 | /** @type {boolean} */ |
39 | 39 | let showOptionList = false; |
40 | 40 |
|
41 | | - /** @type {any[]} */ |
| 41 | + /** @type {{key: string, value: string, checked: boolean}[]} */ |
42 | 42 | let innerOptions = []; |
43 | 43 |
|
44 | | - /** @type {any[]} */ |
| 44 | + /** @type {{key: string, value: string, checked: boolean}[]} */ |
45 | 45 | let refOptions = []; |
46 | 46 |
|
47 | 47 | /** @type {string} */ |
|
53 | 53 | onMount(() => { |
54 | 54 | innerOptions = options.map(x => { |
55 | 55 | return { |
56 | | - id: x.id, |
57 | | - name: x.name, |
| 56 | + key: x.key, |
| 57 | + value: x.value, |
58 | 58 | checked: false |
59 | 59 | } |
60 | 60 | }); |
61 | 61 |
|
62 | 62 | refOptions = options.map(x => { |
63 | 63 | return { |
64 | | - id: x.id, |
65 | | - name: x.name, |
| 64 | + key: x.key, |
| 65 | + value: x.value, |
66 | 66 | checked: false |
67 | 67 | } |
68 | 68 | }); |
|
82 | 82 | function changeSearchValue(e) { |
83 | 83 | searchValue = e.target.value || ''; |
84 | 84 | if (searchValue) { |
85 | | - innerOptions = refOptions.filter(x => x.name.includes(searchValue)); |
| 85 | + innerOptions = refOptions.filter(x => x.value.includes(searchValue)); |
86 | 86 | } else { |
87 | 87 | innerOptions = refOptions; |
88 | 88 | } |
|
96 | 96 | * @param {any} option |
97 | 97 | */ |
98 | 98 | function checkOption(e, option) { |
99 | | - const found = innerOptions.find(x => x.id == option.id); |
100 | | - found.checked = e.target.checked; |
| 99 | + const found = innerOptions.find(x => x.key == option.key); |
| 100 | + const refFound = refOptions.find(x => x.key == option.key); |
101 | 101 |
|
102 | | - const refFound = refOptions.find(x => x.id == option.id); |
103 | | - refFound.checked = e.target.checked; |
104 | | - changeDisplayText(); |
105 | | - sendEvent(); |
| 102 | + if (found && refFound) { |
| 103 | + found.checked = e.target.checked; |
| 104 | + refFound.checked = e.target.checked; |
| 105 | + changeDisplayText(); |
| 106 | + sendEvent(); |
| 107 | + } |
106 | 108 | } |
107 | 109 |
|
108 | 110 | /** @param {any} e */ |
|
119 | 121 |
|
120 | 122 | /** @param {boolean} checked */ |
121 | 123 | function syncChangesToRef(checked) { |
122 | | - const ids = innerOptions.map(x => x.id); |
| 124 | + const keys = innerOptions.map(x => x.key); |
123 | 125 | refOptions = refOptions.map(x => { |
124 | | - if (ids.includes(x.id)) { |
| 126 | + if (keys.includes(x.key)) { |
125 | 127 | return { |
126 | 128 | ...x, |
127 | 129 | checked: checked |
|
210 | 212 |
|
211 | 213 | $: { |
212 | 214 | if (options.length > refOptions.length) { |
213 | | - const curIds = refOptions.map(x => x.id); |
214 | | - const newOptions = options.filter(x => !curIds.includes(x.id)).map(x => { |
| 215 | + const curKeys = refOptions.map(x => x.key); |
| 216 | + const newOptions = options.filter(x => !curKeys.includes(x.key)).map(x => { |
215 | 217 | return { |
216 | | - id: x.id, |
217 | | - name: x.name, |
| 218 | + key: x.key, |
| 219 | + value: x.value, |
218 | 220 | checked: false |
219 | 221 | }; |
220 | 222 | }); |
|
296 | 298 | /> |
297 | 299 | </div> |
298 | 300 | <div class="line-align-center select-name"> |
299 | | - {option.name} |
| 301 | + {option.value} |
300 | 302 | </div> |
301 | 303 | </li> |
302 | 304 | {/each} |
|
0 commit comments