forked from umbraco/Umbraco.UI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuui-combobox-async-example.ts
104 lines (92 loc) · 2.8 KB
/
uui-combobox-async-example.ts
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
95
96
97
98
99
100
101
102
103
104
import { css, html, LitElement, nothing } from 'lit';
import { customElement, state } from 'lit/decorators.js';
interface Fruit {
name: string;
value: string;
}
const data: Array<Fruit> = [
{ name: 'Apple', value: 'apple' },
{ name: 'Orange', value: 'orange' },
{ name: 'Banana', value: 'banana' },
{ name: 'Pear', value: 'pear' },
{ name: 'Grape', value: 'grape' },
{ name: 'Strawberry', value: 'strawberry' },
{ name: 'Watermelon', value: 'watermelon' },
{ name: 'Pineapple', value: 'pineapple' },
{ name: 'Coconut', value: 'coconut' },
{ name: 'Mango', value: 'mango' },
{ name: 'Papaya', value: 'papaya' },
{ name: 'Kiwi', value: 'kiwi' },
{ name: 'Avocado', value: 'avocado' },
{ name: 'Pomegranate', value: 'pomegranate' },
{ name: 'Cherry', value: 'cherry' },
{ name: 'Lemon', value: 'lemon' },
{ name: 'Lime', value: 'lime' },
];
@customElement('uui-combobox-async-example')
export class UUIComboboxAsyncExampleElement extends LitElement {
static styles = [
css`
#loader {
position: absolute;
}
.help {
padding: var(--uui-size-4);
}
`,
];
@state()
_options: any[] = [];
@state()
_loading: boolean = false;
@state()
_filterValue: string = '';
private _fetchData = async (searchParam: string): Promise<Array<Fruit>> => {
this._loading = true;
return await new Promise(res =>
setTimeout(() => {
const filteredData =
searchParam === ''
? []
: data.filter(item =>
item.name.toLowerCase().includes(searchParam.toLowerCase())
);
res(filteredData);
this._loading = false;
}, 500)
);
};
private _handleSearch = async (e: any) => {
this._filterValue = e.target.search;
const response = await this._fetchData(this._filterValue);
this._options = [...response];
};
render() {
return html`
<uui-combobox @search="${this._handleSearch}">
${this._loading ? html`<uui-loader id="loader"></uui-loader>` : nothing}
${this._filterValue === '' && this._loading === false
? html`<div class="help">Search for a fruit...</div>`
: ''}
${this._filterValue !== '' &&
this._options.length === 0 &&
this._loading === false
? html`<div class="help">No fruits matched</div>`
: ''}
<uui-combobox-list>
${this._options.map(
option =>
html`<uui-combobox-list-option value="${option.value}"
>${option.name}</uui-combobox-list-option
>`
)}
</uui-combobox-list>
</uui-combobox>
<div style="margin-top: var(--uui-size-4)">
<strong>Data:</strong> ${data.map(
fruit => html`<div>${fruit.name}</div>`
)}
</div>
`;
}
}