|
| 1 | +import { html, LitElement } from 'lit'; |
| 2 | +import { customElement, property, state } from 'lit/decorators.js'; |
| 3 | + |
| 4 | +interface Fruit { |
| 5 | + name: string; |
| 6 | + value: string; |
| 7 | +} |
| 8 | + |
| 9 | +const data: Array<Fruit> = [ |
| 10 | + { name: 'Apple', value: 'apple' }, |
| 11 | + { name: 'Orange', value: 'orange' }, |
| 12 | + { name: 'Banana', value: 'banana' }, |
| 13 | + { name: 'Pear', value: 'pear' }, |
| 14 | + { name: 'Grape', value: 'grape' }, |
| 15 | + { name: 'Strawberry', value: 'strawberry' }, |
| 16 | + { name: 'Watermelon', value: 'watermelon' }, |
| 17 | + { name: 'Pineapple', value: 'pineapple' }, |
| 18 | + { name: 'Coconut', value: 'coconut' }, |
| 19 | + { name: 'Mango', value: 'mango' }, |
| 20 | + { name: 'Papaya', value: 'papaya' }, |
| 21 | + { name: 'Kiwi', value: 'kiwi' }, |
| 22 | + { name: 'Avocado', value: 'avocado' }, |
| 23 | + { name: 'Pomegranate', value: 'pomegranate' }, |
| 24 | + { name: 'Cherry', value: 'cherry' }, |
| 25 | + { name: 'Lemon', value: 'lemon' }, |
| 26 | + { name: 'Lime', value: 'lime' }, |
| 27 | +]; |
| 28 | + |
| 29 | +async function getFruits() { |
| 30 | + return Promise.resolve(data); |
| 31 | +} |
| 32 | + |
| 33 | +@customElement('uui-combobox-async-options-example') |
| 34 | +export class UUIComboboxAsyncOptionsExampleElement extends LitElement { |
| 35 | + @state() |
| 36 | + _options: any[] = []; |
| 37 | + |
| 38 | + connectedCallback() { |
| 39 | + super.connectedCallback(); |
| 40 | + this._fetchData(); |
| 41 | + } |
| 42 | + |
| 43 | + private _fetchData = async () => { |
| 44 | + const response = await getFruits(); |
| 45 | + this._options = [...response]; |
| 46 | + }; |
| 47 | + |
| 48 | + @property() |
| 49 | + preselected = 'apple'; |
| 50 | + |
| 51 | + render() { |
| 52 | + return html` |
| 53 | + <uui-combobox value=${this.preselected}> |
| 54 | + <uui-combobox-list> |
| 55 | + ${this._options.map( |
| 56 | + option => |
| 57 | + html`<uui-combobox-list-option value="${option.value}" |
| 58 | + >${option.name}</uui-combobox-list-option |
| 59 | + >` |
| 60 | + )} |
| 61 | + </uui-combobox-list> |
| 62 | + </uui-combobox> |
| 63 | + `; |
| 64 | + } |
| 65 | +} |
0 commit comments