|
1 | 1 | <div>
|
2 | 2 | <List
|
3 | 3 | class="demo-list"
|
| 4 | + bind:this={list} |
4 | 5 | twoLine
|
5 | 6 | avatarList
|
6 | 7 | singleSelection
|
7 | 8 | bind:selectedIndex={selectionIndex}
|
8 | 9 | >
|
9 |
| - {#each options as item} |
| 10 | + {#each options as item, i} |
10 | 11 | <Item
|
11 |
| - on:SMUI:action={() => (selection = item.name)} |
| 12 | + on:SMUI:action={() => console.log('Item selected:', item)} |
12 | 13 | disabled={item.disabled}
|
13 |
| - selected={selection === item.name} |
| 14 | + selected={selectionIndex === i} |
14 | 15 | >
|
15 | 16 | <Graphic
|
16 | 17 | style="background-image: url(https://place-hold.it/40x40?text={item.name
|
|
28 | 29 | </List>
|
29 | 30 | </div>
|
30 | 31 |
|
31 |
| -<pre |
32 |
| - class="status">Selected: {selection}, value of selectedIndex: {selectionIndex}</pre> |
| 32 | +<pre class="status">Selected: {selectionIndex} - {options[selectionIndex] |
| 33 | + .name}</pre> |
| 34 | + |
| 35 | +<div style="margin-top: 1em;"> |
| 36 | + <div>Programmatically select:</div> |
| 37 | + {#each options as option, i} |
| 38 | + <Button on:click={() => (selectionIndex = i)}> |
| 39 | + <Label>{option.name}</Label> |
| 40 | + </Button> |
| 41 | + {/each} |
| 42 | +</div> |
| 43 | + |
| 44 | +<div style="margin-top: 1em;"> |
| 45 | + <div>Programmatically focus:</div> |
| 46 | + {#each options as option, i} |
| 47 | + <Button on:click={() => list.focusItemAtIndex(i)}> |
| 48 | + <Label>{option.name}</Label> |
| 49 | + </Button> |
| 50 | + {/each} |
| 51 | +</div> |
33 | 52 |
|
34 | 53 | <script lang="ts">
|
35 | 54 | import List, {
|
|
40 | 59 | PrimaryText,
|
41 | 60 | SecondaryText,
|
42 | 61 | } from '@smui/list';
|
| 62 | + import Button, { Label } from '@smui/button'; |
| 63 | +
|
| 64 | + let list: InstanceType<typeof List>; |
43 | 65 |
|
44 | 66 | let options = [
|
45 | 67 | {
|
|
63 | 85 | disabled: false,
|
64 | 86 | },
|
65 | 87 | ];
|
66 |
| - let selection = 'Stephen Hawking'; |
67 |
| - // This value is updated when the component is initialized, based on the |
68 |
| - // selected Item's `selected` prop. |
69 |
| - let selectionIndex: number | undefined = undefined; |
| 88 | + // If this is not set, it is updated when the component is initialized, based |
| 89 | + // on the selected Item's `selected` prop. |
| 90 | + let selectionIndex = 3; |
70 | 91 | </script>
|
71 | 92 |
|
72 | 93 | <style>
|
|
0 commit comments