Skip to content
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

Merged

Conversation

GarethSmall
Copy link
Contributor

@GarethSmall GarethSmall commented Feb 17, 2018

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

@GarethSmall GarethSmall changed the title Fix/12200 regresssion select multiple 2 Regression: React 16 automatically marks first item of a mutliple select as checked Feb 18, 2018
@GarethSmall
Copy link
Contributor Author

@aweary thoughts?

Copy link

@ewolfe ewolfe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Short and sweet 👍

@ewolfe
Copy link

ewolfe commented Apr 7, 2018

@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;
Copy link
Contributor

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
}

Copy link
Contributor Author

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.

Copy link
Contributor

@nhunzaker nhunzaker left a 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.

@GarethSmall
Copy link
Contributor Author

Sounds good, ill add them this evening.

@gaearon gaearon force-pushed the fix/12200-regresssion-select-multiple-2 branch from 70b50f0 to 43a7d6d Compare August 3, 2018 17:09
@gaearon gaearon changed the title Regression: React 16 automatically marks first item of a mutliple select as checked Add a regression test for #12200 Aug 3, 2018
@gaearon
Copy link
Collaborator

gaearon commented Aug 3, 2018

We merged a different fix in #13270. I'm taking this for the regression unit test though. Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants