Skip to content

fix option update. Fixes #2620 #3817

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/browser/ui/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var CSSPropertyOperations = require('CSSPropertyOperations');
var DOMProperty = require('DOMProperty');
var DOMPropertyOperations = require('DOMPropertyOperations');
var ReactBrowserEventEmitter = require('ReactBrowserEventEmitter');
var ReactChildren = require('ReactChildren');
var ReactComponentBrowserEnvironment =
require('ReactComponentBrowserEnvironment');
var ReactMount = require('ReactMount');
Expand Down Expand Up @@ -232,6 +233,26 @@ function processChildContext(context, tagName) {
return context;
}

var onlyNestTextTags = {
'option': true
};

function getChildrenTextContent(tag, children) {
var childrenContent = '';
ReactChildren.forEach(children, function (c) {
var childType = typeof c;
if (childType !== 'string' && childType !== 'number') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This if statement would be clearer with the branches swapped.

// not null/undefined/boolean
if (c !== null) {
warning(false, '%s can not have complex children/content', tag);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you put the warning in an if (__DEV__) and just skip over object children silently in production mode?

Copy link
Contributor

Choose a reason for hiding this comment

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

@spicyj Could it not make sense to toString them instead of skipping, making it even more prominent that some invalid data has been passed to something that only accepts text.

}
} else {
childrenContent += c;
}
});
return childrenContent;
}

/**
* Creates a new React class that is idempotent and capable of containing other
* React components. It accepts event listeners and DOM properties that are
Expand Down Expand Up @@ -369,6 +390,10 @@ ReactDOMComponent.Mixin = {
ret = innerHTML.__html;
}
} else {
if (onlyNestTextTags[this._tag]) {
var childrenContent = getChildrenTextContent(this._tag, props.children);
return escapeTextContentForBrowser(childrenContent);
}
var contentToUse =
CONTENT_TYPES[typeof props.children] ? props.children : null;
var childrenToUse = contentToUse != null ? null : props.children;
Expand Down Expand Up @@ -567,6 +592,11 @@ ReactDOMComponent.Mixin = {
var nextContent =
CONTENT_TYPES[typeof nextProps.children] ? nextProps.children : null;

if (onlyNestTextTags[this._tag]) {
lastContent = getChildrenTextContent(this._tag, lastProps.children);
nextContent = getChildrenTextContent(this._tag, nextProps.children);
}

var lastHtml =
lastProps.dangerouslySetInnerHTML &&
lastProps.dangerouslySetInnerHTML.__html;
Expand Down
32 changes: 32 additions & 0 deletions src/browser/ui/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -775,4 +775,36 @@ describe('ReactDOMComponent', function() {
});
});

describe('option update', function(){
var container;

beforeEach(() => {
container = document.createElement('div');
React.render(<select><option>{10}$</option></select>, container);
});

it('supports number update', () => {
React.render(<select><option>{20}$</option></select>, container);
expect(container.firstChild.firstChild.innerHTML).toEqual('20$');
});

it('supports string update', () => {
React.render(<select><option>{'20'}$</option></select>, container);
expect(container.firstChild.firstChild.innerHTML).toEqual('20$');
});

it('supports boolean update', () => {
React.render(<select><option>{true && '20'}$</option></select>, container);
expect(container.firstChild.firstChild.innerHTML).toEqual('20$');
React.render(<select><option>{false && '20'}$</option></select>, container);
expect(container.firstChild.firstChild.innerHTML).toEqual('$');
});

it('supports null/undefined update', () => {
React.render(<select><option>{null}$</option></select>, container);
expect(container.firstChild.firstChild.innerHTML).toEqual('$');
React.render(<select><option>{undefined}$</option></select>, container);
expect(container.firstChild.firstChild.innerHTML).toEqual('$');
});
});
});