-
Notifications
You must be signed in to change notification settings - Fork 142
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
VSCodeDropdown in React with dynamically rendered children doesn't auto select provided value property #433
Comments
Hey @LuukVaal! Perhaps I'm misunderstanding your ask here, but the reason (or at least one of the big reasons) the above code isn't working is because native HTML To set a default value, you must add a <VSCodeDropdown>
<VSCodeOption>option-1</VSCodeOption>
<VSCodeOption selected>option-2</VSCodeOption> // <-- this will be the selected value
<VSCodeOption>option-3</VSCodeOption>
</VSCodeDropdown> Further reference: https://stackoverflow.com/questions/5589629/value-attribute-on-select-tag-not-selecting-default-option |
Thanks for your reply. However, in the React docs it is mentioned that react offers a value property on the root of a select component, so i did expect this to work (and it does if the options are present when the component is rendered). Regardless, the issue still seems to persist if i use the selected prop on the element that should be selected. |
Seems like selected attribute doesn't work at all if VSCodeOption wrapped with VSCodeDropdown, even assigned directly:
Just checked in your example repository for React. Same issue with "checked" attribute for VSCodeRadio wrapped with VSCodeRadioGroup. |
It works if the Nevertheless, I think this behaviour should be reflected in the documentation. |
Thank you @web-devel! Your comment saved my life! |
I got the same problem! |
+1 For this. I don't mind that the VS Code Components here diverge from the React behavior for |
`selectedIndex` is not how you are supposed to set the initial value (although there is no documentation about this, only microsoft/vscode-webview-ui-toolkit#433). Instead, use `value` just as you would for an HTML select element. This fixes an issue where in some cases (seen only in our storybooks), the dropdown would show the first item as selected, not the `selectedIndex`th item.
Describe the bug
When using VSCodeDropdown in React with a value property and dynamically rendered children where the children are rendered later than the value property is set, the dropdown selects the default option (which is the only option that is not dynamically rendered) and does not automatically selects the option corresponding to the value property.
To reproduce
StackBlitz example:
https://stackblitz.com/edit/react-ts-71etx8?file=App.tsx
Expected behavior
The dropdown automatically selects the dynamically rendered option based on the existing value property.
Current behavior
The dropdown will keep the default (first) option selected.
Note that conditionally rendering the dropdown list does solve this issue, however that should not be necessary.
Screenshots
See this StackBlitz demo: https://stackblitz.com/edit/react-ts-71etx8?file=App.tsx
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: