-
-
Notifications
You must be signed in to change notification settings - Fork 285
/
_Async.svelte
62 lines (54 loc) · 1.56 KB
/
_Async.svelte
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
<div>
<Autocomplete
search={searchItems}
bind:value
showMenuWithNoInput={false}
label="Fruit"
>
<Text
slot="loading"
style="display: flex; width: 100%; justify-content: center; align-items: center;"
>
<CircularProgress style="height: 24px; width: 24px;" indeterminate />
</Text>
</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 = undefined;
let counter = 0;
async function searchItems(input: string) {
if (input === '') {
return [];
}
// 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>