-
-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy path_Async.svelte
More file actions
70 lines (61 loc) · 1.83 KB
/
_Async.svelte
File metadata and controls
70 lines (61 loc) · 1.83 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
<div>
<Autocomplete
search={searchItems}
bind:value
showMenuWithNoInput={false}
label="Fruit"
>
{#snippet loading()}
<Text
style="display: flex; width: 100%; justify-content: center; align-items: center;"
>
<CircularProgress style="height: 24px; width: 24px;" indeterminate />
</Text>
{/snippet}
</Autocomplete>
<pre class="status">Selected: {value || ''}</pre>
</div>
<script lang="ts">
import Autocomplete from '@smui-extra/autocomplete';
import { Text } from '@smui/list';
import CircularProgress from '@smui/circular-progress';
let fruits = [
'Apple',
'Orange',
'Banana',
'Mango',
'Lemon',
'Cherry',
'Blueberry',
'Grape',
'Strawberry',
];
let value: string | undefined = $state();
let counter = 0;
async function searchItems(input: string) {
if (input === '') {
return [];
}
if (value != null) {
// Return an array with just the already selected value to hide the menu.
// As soon as the user changes the text field, the value is unselected, so
// the search should run again.
return [value];
}
// Pretend to have some sort of canceling mechanism.
const myCounter = ++counter;
// Pretend to be loading something...
await new Promise((resolve) => setTimeout(resolve, 1000));
// This means the function was called again, so we should cancel.
if (myCounter !== counter) {
// `return false` (or, more accurately, resolving the Promise object to
// `false`) is how you tell Autocomplete to cancel this search. It won't
// replace the results of any subsequent search that has already finished.
return false;
}
// Return a list of matches.
return fruits.filter((item) =>
item.toLowerCase().includes(input.toLowerCase()),
);
}
</script>