fix: dynamically setting options sets undefined to select value #655
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Approach
Without this change, the
Select
component doesn't know theOptions
(items in the internalList
component) afteronMount
. So it can't tell whichOption
s are added/removed.To fix the root cause, I created a new events to
List
:SMUIList:mountItem
andSMUIList:unmountItem
, which tells the parent when some of its children are added or removed. Then, I implemented an event handler ofSelect
for the new events to update the internal list and calllayoutOptions
.layoutOptions
should always be called whenever the options are updated (Ref. https://github.com/material-components/material-components-web/blob/f80ac92b08dfa1b59cd9faf74f3d19a4b134993e/packages/mdc-select/foundation.ts#L235-L239.Another Option
The change might be simpler adding
bind:accessor={list}
to theList
. But I didn't choose it becauseList
seems to want to hide itsaccessor
as the implementation details. I'll rewrite to try if you prefer.