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

Make sure that select has multiple attribute set to appropriate state before appending options #13270

Merged
merged 7 commits into from Aug 3, 2018
19 changes: 19 additions & 0 deletions fixtures/dom/src/components/fixtures/selects/index.js
Expand Up @@ -159,6 +159,25 @@ class SelectFixture extends React.Component {
</div>
</TestCase>

<TestCase title="A multiple select being scrolled to first selected option">
<TestCase.ExpectedResult>
First selected option should be visible
</TestCase.ExpectedResult>

<div className="test-fixture">
<form>
<select multiple defaultValue={['tiger']}>
<option value="gorilla">gorilla</option>
<option value="giraffe">giraffe</option>
<option value="monkey">monkey</option>
<option value="lion">lion</option>
<option value="mongoose">mongoose</option>
<option value="tiger">tiget</option>
</select>
</form>
</div>
</TestCase>

<TestCase
title="An option which contains conditional render fails"
relatedIssues="11911">
Expand Down
9 changes: 9 additions & 0 deletions packages/react-dom/src/client/ReactDOMFiberComponent.js
Expand Up @@ -384,6 +384,15 @@ export function createElement(
// See discussion in https://github.com/facebook/react/pull/6896
// and discussion in https://bugzilla.mozilla.org/show_bug.cgi?id=1276240
domElement = ownerDocument.createElement(type);
// Normally attributes are assigned in `setInitialDOMProperties`, however the `multiple`
// attribute on `select`s needs to be added before `option`s are inserted. This prevents
// a bug where the `select` does not scroll to the correct option because singular
// `select` elements automatically pick the first item.
// See https://github.com/facebook/react/issues/13222
if (type === 'select' && props.multiple) {
const node = ((domElement: any): HTMLSelectElement);
node.multiple = true;
}
}
} else {
domElement = ownerDocument.createElementNS(namespaceURI, type);
Expand Down