Skip to content

fix: dynamically setting options sets undefined to select value #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/list/src/List.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@

let element: SvelteComponent;
let instance: MDCListFoundation;
let accessor: SMUIListAccessor;
let items: SMUIListItemAccessor[] = [];
let role = getContext<string | undefined>('SMUI:list:role');
let nav = getContext<boolean | undefined>('SMUI:list:nav');
Expand Down Expand Up @@ -232,7 +233,7 @@
},
});

const accessor: SMUIListAccessor = {
accessor = {
get element() {
return getElement();
},
Expand Down Expand Up @@ -282,6 +283,7 @@
selectedIndex = getListItemIndex(event.detail.element);
}
event.stopPropagation();
dispatch(getElement(), 'SMUIList:mountItem', accessor);
}

function handleItemUnmount(event: CustomEvent<SMUIListItemAccessor>) {
Expand All @@ -292,6 +294,7 @@
itemAccessorMap.delete(event.detail.element);
}
event.stopPropagation();
dispatch(getElement(), 'SMUIList:unmountItem', accessor);
}

function handleKeydown(event: KeyboardEvent) {
Expand Down
16 changes: 13 additions & 3 deletions packages/select/src/Select.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@
{wrapFocus}
bind:selectedIndex
on:SMUIList:mount={(event) => (list = event.detail)}
on:SMUIList:mountItem={handleItemMountUnmount}
on:SMUIList:unmountItem={handleItemMountUnmount}
{...prefixFilter($$restProps, 'list$')}><slot /></List
>
</Menu>
Expand Down Expand Up @@ -473,10 +475,13 @@
},
getSelectedIndex: () => selectedIndex,
setSelectedIndex: (index) => {
// Don't update the instance again.
previousSelectedIndex = index;
selectedIndex = index;
value = getMenuItemValues()[selectedIndex];
selectedIndex = index === -1 ? 0 : index;
const menuItems = getMenuItemValues();
// Avoid setting undefined to the value
if (selectedIndex < menuItems.length) {
value = menuItems[selectedIndex];
}
},
focusMenuItemAtIndex: (index) => {
list.focusItemAtIndex(index);
Expand Down Expand Up @@ -659,4 +664,9 @@
export function getElement() {
return element;
}

function handleItemMountUnmount(event: CustomEvent<SMUIListAccessor>): void {
list = event.detail;
instance && instance.layoutOptions();
}
</script>