-
Notifications
You must be signed in to change notification settings - Fork 47.1k
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
Add a regression test for #12200 #12242
Add a regression test for #12200 #12242
Conversation
@aweary thoughts? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short and sweet 👍
@aweary Would you mind taking a look at this? I just ran into this bug and this seems like a good fix. I tested it locally (after manually copy/pasting to an up-to-date fork) and everything worked as expected :) |
@@ -182,6 +182,8 @@ export function initWrapperState(element: Element, props: Object) { | |||
|
|||
export function postMountWrapper(element: Element, props: Object) { | |||
const node = ((element: any): SelectWithWrapperState); | |||
//Set selected index to -1 before doing anything else to prevent first option from being selected by default | |||
node.selectedIndex = -1; | |||
node.multiple = !!props.multiple; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly chasing ghosts, but I'm worried about the side-effects of always assigning selectedIndex to -1. Do you think it makes sense to only apply this for the multiple input case?
const multiple = !!props.multiple;
if (multiple) {
node.selectedIndex = -1
node.multiple = true
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion, since this only occurs in the event of multiple this makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for sending this in!
Additionally, would it be possible to add a test case to ensure that this behavior is consistent when multiple is set from false
to true
and back? I want to make sure we cover the update case too.
Sounds good, ill add them this evening. |
70b50f0
to
43a7d6d
Compare
We merged a different fix in #13270. I'm taking this for the regression unit test though. Thank you so much! |
Problem
When creating a select element and setting multiple the first element would be selected. This was due to the multiple prop being set after the children we're appended.
An non-react example case created by @aweary can been seen here - https://jsfiddle.net/dm6vkq9q/
Tradeoffs / Misc
In my original solution I added a new hook to set props for just the select element before the children we're appended. This worked only because it was setting just the select props first. An alternative idea proposed was to switch the order of appendChildren > setProps to setProps > appendChildren. this will not work in the instance that we have a defualtValue set for our select input. When we change this order a defaultValue is ignored. There we're also many other tests that broke in this process. Although this is bit of a hacky solution this will not require us to make any dangerous changes to the code base.
Solution
In postMountWrapper in ReactDOMFiberSelect.js I set selectedIndex for the select element to -1
Issue
#12200
Related closed pull requests
#12240
@aweary @gaearon