-
-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy path_Objects.svelte
More file actions
45 lines (41 loc) · 1.17 KB
/
_Objects.svelte
File metadata and controls
45 lines (41 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!--
Note: chips must be unique. (They cannot === each other.)
If you need to show the same value, use keyed chips.
-->
<div>
<ChipInput
bind:chips={items}
bind:value
key={(item) => item.id}
getChipLabel={(item) => item.text}
getChipText={(item) => item.text}
chipTrailingAction$class="material-icons"
chipTrailingAction$aria-label="Remove item"
autocomplete$combobox
onSMUIChipInputEntry={handleChipInputEntry}
>
{#snippet chipTrailingAction()}cancel{/snippet}
{#snippet label()}
Shopping List
{/snippet}
</ChipInput>
</div>
<script lang="ts">
import ChipInput from '@smui-extra/chip-input';
let items = $state([
{ id: 1, text: 'Milk' },
{ id: 2, text: 'Milk' },
{ id: 3, text: 'Lemonade' },
]);
let value = $state('');
function handleChipInputEntry(event: CustomEvent<{ text: string }>) {
// This prevents the text itself from being pushed onto the array.
event.preventDefault();
// And we can push our own object containing the text.
items.push({
id: items.length ? Math.max(...items.map((tag) => tag.id)) + 1 : 1,
text: event.detail.text,
});
value = '';
}
</script>