Skip to content

Commit 5e7214c

Browse files
committed
Merge branch 'usedbytes-list-focusItemAtIndex'
2 parents c551452 + 5e69866 commit 5e7214c

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

MIGRATING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ https://github.com/material-components/material-components-web/blob/master/CHANG
3535
- List
3636
- New `disabledItemsFocusable` prop.
3737
- New `wrapper` prop on Item. This should be used for items that only act as the container of a nested list.
38+
- New `focusItemAtIndex` function.
3839
- Menu
3940
- New `SMUIMenu:closedProgrammatically` event.
4041
- Menu Surface

packages/list/src/List.svelte

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,6 @@
368368
) as SMUIListItemAccessor[];
369369
}
370370
371-
function focusItemAtIndex(index: number) {
372-
const accessor = getOrderedList()[index];
373-
accessor &&
374-
'focus' in accessor.element &&
375-
(accessor.element as HTMLInputElement).focus();
376-
}
377-
378371
function listItemAtIndexHasClass(index: number, className: string) {
379372
const accessor = getOrderedList()[index];
380373
return (accessor && accessor.hasClass(className)) ?? false;
@@ -453,6 +446,13 @@
453446
return instance.getFocusedItemIndex();
454447
}
455448
449+
export function focusItemAtIndex(index: number) {
450+
const accessor = getOrderedList()[index];
451+
accessor &&
452+
'focus' in accessor.element &&
453+
(accessor.element as HTMLInputElement).focus();
454+
}
455+
456456
export function getElement(): HTMLElement {
457457
return element.getElement();
458458
}

packages/site/src/routes/demo/list/_TwoLineSelection.svelte

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<div>
22
<List
33
class="demo-list"
4+
bind:this={list}
45
twoLine
56
avatarList
67
singleSelection
78
bind:selectedIndex={selectionIndex}
89
>
9-
{#each options as item}
10+
{#each options as item, i}
1011
<Item
11-
on:SMUI:action={() => (selection = item.name)}
12+
on:SMUI:action={() => console.log('Item selected:', item)}
1213
disabled={item.disabled}
13-
selected={selection === item.name}
14+
selected={selectionIndex === i}
1415
>
1516
<Graphic
1617
style="background-image: url(https://place-hold.it/40x40?text={item.name
@@ -28,8 +29,26 @@
2829
</List>
2930
</div>
3031

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>
3352

3453
<script lang="ts">
3554
import List, {
@@ -40,6 +59,9 @@
4059
PrimaryText,
4160
SecondaryText,
4261
} from '@smui/list';
62+
import Button, { Label } from '@smui/button';
63+
64+
let list: InstanceType<typeof List>;
4365
4466
let options = [
4567
{
@@ -63,10 +85,9 @@
6385
disabled: false,
6486
},
6587
];
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;
7091
</script>
7192

7293
<style>

0 commit comments

Comments
 (0)