-
-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy path_AddEntries.svelte
More file actions
94 lines (87 loc) · 2.1 KB
/
_AddEntries.svelte
File metadata and controls
94 lines (87 loc) · 2.1 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<div>
<Autocomplete
{options}
getOptionLabel={(option) =>
option ? `${option.label} (${option.id})` : ''}
bind:value
bind:text
noMatchesActionDisabled={false}
onSMUIAutocompleteNoMatchesAction={() => {
newLabel = text;
dialogOpen = true;
}}
label="Dialog"
>
{#snippet noMatches()}
<Text>Add item</Text>
{/snippet}
</Autocomplete>
<pre class="status">Selected: {value ? JSON.stringify(value) : ''}</pre>
<Dialog
bind:open={dialogOpen}
aria-labelledby="autocomplete-dialog-title"
aria-describedby="autocomplete-dialog-content"
>
<!-- Title cannot contain leading whitespace due to mdc-typography-baseline-top() -->
<Title id="autocomplete-dialog-title">New Item</Title>
<Content id="autocomplete-dialog-content">
<Textfield bind:value={newLabel} label="Label" />
</Content>
<Actions>
<Button>
<Label>Cancel</Label>
</Button>
<Button onclick={addObject}>
<Label>Add</Label>
</Button>
</Actions>
</Dialog>
</div>
<script lang="ts">
import Autocomplete from '@smui-extra/autocomplete';
import { Text } from '@smui/list';
import Button, { Label } from '@smui/button';
import Dialog, { Title, Content, Actions } from '@smui/dialog';
import Textfield from '@smui/textfield';
type Item = {
id: number;
label: string;
};
let dialogOpen = $state(false);
// When options are objects, you need to wrap them in a $state rune, so that
// Svelte can compare the objects properly.
let options: Item[] = $state([
{
id: 0,
label: 'One',
},
{
id: 1,
label: 'Two',
},
{
id: 2,
label: 'Three',
},
{
id: 3,
label: 'Four',
},
{
id: 4,
label: 'Five',
},
]);
let newLabel = $state('');
let value: Item | undefined = $state();
let text = $state('');
function addObject() {
const newObject = {
id: options[options.length - 1].id + 1,
label: newLabel,
};
options = [...options, newObject];
value = newObject;
dialogOpen = false;
}
</script>