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

Fix a crash when using dynamic children in <option> tag #13261

Merged
merged 10 commits into from
Aug 1, 2018
25 changes: 25 additions & 0 deletions fixtures/dom/src/components/fixtures/selects/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,31 @@ class SelectFixture extends React.Component {
</form>
</div>
</TestCase>

<TestCase
title="An option which contains conditional render fails"
relatedIssues="11911">
<TestCase.Steps>
<li>Select any option</li>
</TestCase.Steps>
<TestCase.ExpectedResult>
Option should be set
</TestCase.ExpectedResult>

<div className="test-fixture">
<select value={this.state.value} onChange={this.onChange}>
<option value="red">
red {this.state.value === 'red' && 'is chosen '} TextNode
</option>
<option value="blue">
blue {this.state.value === 'blue' && 'is chosen '} TextNode
</option>
<option value="green">
green {this.state.value === 'green' && 'is chosen '} TextNode
</option>
</select>
</div>
</TestCase>
</FixtureSet>
);
}
Expand Down
4 changes: 1 addition & 3 deletions packages/react-dom/src/__tests__/ReactDOMOption-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ describe('ReactDOMOption', () => {
expect(() => {
node = ReactTestUtils.renderIntoDocument(el);
}).toWarnDev(
'<div> cannot appear as a child of <option>.\n' +
' in div (at **)\n' +
' in option (at **)',
'<div> cannot appear as a child of <option>.\n' + ' in option (at **)',
Copy link
Contributor Author

@Slowyn Slowyn Jul 26, 2018

Choose a reason for hiding this comment

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

This is a behavior change and I'm not sure how to avoid this with this solution. But it's only warning, so shouldn't be critical.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is okay. Particularly since the problem is still identified in the message (even if the nest doesn't show up).

Copy link
Contributor

Choose a reason for hiding this comment

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

@aweary or @gaearon Do you anticipate any problems with changing this warning text?

);
expect(node.innerHTML).toBe('1 2');
ReactTestUtils.renderIntoDocument(el);
Expand Down
42 changes: 42 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMSelect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,48 @@ describe('ReactDOMSelect', () => {
expect(node.options[2].selected).toBe(false); // gorilla
});

it('should not fail', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is very vague.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, It's some temporary "name" until we get a full working version.
Going to change it to some concrete form 😄

Have you taken a look on anccestorInfo in current case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What about this description: "Options with stateful children; it should change its state properly and shouldn't fail"
What do you think?

const container = document.createElement('div');

let node;

function App({value}) {
return (
<select value={value} ref={n => (node = n)} onChange={noop}>
<option key="monkey" value="monkey">
A monkey {value === 'monkey' ? 'is chosen' : null}!
</option>
<option key="giraffe" value="giraffe">
A giraffe {value === 'giraffe' && 'is chosen'}!
</option>
<option key="gorilla" value="gorilla">
A gorilla {value === 'gorilla' && 'is chosen'}!
</option>
</select>
);
}

ReactDOM.render(
<App value={'monkey'} />,
container,
);

expect(node.options[0].selected).toBe(true); // monkey
expect(node.options[1].selected).toBe(false); // giraffe
expect(node.options[2].selected).toBe(false); // gorilla


ReactDOM.render(
<App value={'giraffe'} />,
container,
);

expect(node.options[0].selected).toBe(false); // monkey
expect(node.options[1].selected).toBe(true); // giraffe
expect(node.options[2].selected).toBe(false); // gorilla

});

it('should warn if value is null', () => {
expect(() =>
ReactTestUtils.renderIntoDocument(
Expand Down
3 changes: 2 additions & 1 deletion packages/react-dom/src/client/ReactDOMFiberComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ export function setInitialProperties(
tag: string,
rawProps: Object,
rootContainerElement: Element | Document,
hostContext: Object,
): void {
const isCustomComponentTag = isCustomComponent(tag, rawProps);
if (__DEV__) {
Expand Down Expand Up @@ -485,7 +486,7 @@ export function setInitialProperties(
break;
case 'option':
ReactDOMFiberOption.validateProps(domElement, rawProps);
props = ReactDOMFiberOption.getHostProps(domElement, rawProps);
props = ReactDOMFiberOption.getHostProps(domElement, rawProps, hostContext);
break;
case 'select':
ReactDOMFiberSelect.initWrapperState(domElement, rawProps);
Expand Down
15 changes: 10 additions & 5 deletions packages/react-dom/src/client/ReactDOMFiberOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,27 @@

import React from 'react';
import warning from 'shared/warning';
import validateDOMNesting from './validateDOMNesting';

let didWarnSelectedSetOnOption = false;

function flattenChildren(children) {
function flattenChildren(children, hostContext = {}) {
let content = '';

// Flatten children and warn if they aren't strings or numbers;
// invalid types are ignored.
// We can silently skip them because invalid DOM nesting warning
// catches these cases in Fiber.
React.Children.forEach(children, function(child) {
if (child == null) {
return;
}
if (typeof child === 'string' || typeof child === 'number') {
content += child;
return;
}
if (__DEV__) {
// We do not have HostContext here, but we can at least
// put some parent information
validateDOMNesting(child.type, null, hostContext.ancestorInfo);
}
});

Expand Down Expand Up @@ -56,9 +61,9 @@ export function postMountWrapper(element: Element, props: Object) {
}
}

export function getHostProps(element: Element, props: Object) {
export function getHostProps(element: Element, props: Object, hostContext: Object = {}) {
const hostProps = {children: undefined, ...props};
const content = flattenChildren(props.children);
const content = flattenChildren(props.children, hostContext);

if (content) {
hostProps.children = content;
Expand Down
3 changes: 2 additions & 1 deletion packages/react-dom/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export function finalizeInitialChildren(
rootContainerInstance: Container,
hostContext: HostContext,
): boolean {
setInitialProperties(domElement, type, props, rootContainerInstance);
setInitialProperties(domElement, type, props, rootContainerInstance, hostContext);
return shouldAutoFocusHostComponent(type, props);
}

Expand Down Expand Up @@ -248,6 +248,7 @@ export function prepareUpdate(
export function shouldSetTextContent(type: string, props: Props): boolean {
return (
type === 'textarea' ||
type === 'option' ||
typeof props.children === 'string' ||
typeof props.children === 'number' ||
(typeof props.dangerouslySetInnerHTML === 'object' &&
Expand Down