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
Selectcomponent doesn't know theOptions(items in the internalListcomponent) afteronMount. So it can't tell whichOptions are added/removed.To fix the root cause, I created a new events to
List:SMUIList:mountItemandSMUIList:unmountItem, which tells the parent when some of its children are added or removed. Then, I implemented an event handler ofSelectfor the new events to update the internal list and calllayoutOptions.layoutOptionsshould 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 becauseListseems to want to hide itsaccessoras the implementation details. I'll rewrite to try if you prefer.