Skip to content
Merged
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
21 changes: 20 additions & 1 deletion src/browser/ui/dom/components/ReactDOMOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'use strict';

var ReactBrowserComponentMixin = require('ReactBrowserComponentMixin');
var ReactChildren = require('ReactChildren');
var ReactClass = require('ReactClass');
var ReactDOMSelect = require('ReactDOMSelect');
var ReactElement = require('ReactElement');
Expand Down Expand Up @@ -86,7 +87,25 @@ var ReactDOMOption = ReactClass.createClass({
props = assign({}, props, {selected: this.state.selected});
}

return option(props, this.props.children);
var content = '';

// Flatten children and warn if they aren't strings or numbers;
// invalid types are ignored.
ReactChildren.forEach(this.props.children, function(child) {
if (child == null) {
return;
}
if (typeof child === 'string' || typeof child === 'number') {
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 make sure this skips over null/undefined children without a warning and add a test for that?

Copy link
Contributor

Choose a reason for hiding this comment

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

boolean too I assume?

if (type === 'undefined' || type === 'boolean') {
// All of the above are perceived as null.
children = null;
}
if (children === null ||
type === 'string' ||
type === 'number' ||
for reference

content += child;
} else {
warning(
false,
'Only strings and numbers are supported as <option> children.'
);
}
});

return option(props, content);
}

});
Expand Down
65 changes: 65 additions & 0 deletions src/browser/ui/dom/components/__tests__/ReactDOMOption-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails react-core
*/

'use strict';

/*jshint evil:true */

describe('ReactDOMOption', function() {
var React;
var ReactTestUtils;

beforeEach(function() {
React = require('React');
ReactTestUtils = require('ReactTestUtils');
});

it('should flatten children to a string', function() {
var stub = <option>{1} {'foo'}</option>;
stub = ReactTestUtils.renderIntoDocument(stub);
var node = React.findDOMNode(stub);

expect(node.innerHTML).toBe('1 foo');
});

it('should ignore invalid children types', function() {
var stub = <option>{1} <div /> {2}</option>;
stub = ReactTestUtils.renderIntoDocument(stub);
var node = React.findDOMNode(stub);

expect(node.innerHTML).toBe('1 2');
});

it('should warn when passing invalid children', function() {
var stub = <option>{1} <div /></option>;
spyOn(console, 'error');
stub = ReactTestUtils.renderIntoDocument(stub);

var node = React.findDOMNode(stub);

expect(console.error.calls.length).toBe(1);
expect(console.error.calls[0].args[0]).toContain(
'Only strings and numbers are supported as <option> children.'
);
});

it('should ignore null/undefined/false children without warning', function() {
var stub = <option>{1} {false}{true}{null}{undefined} {2}</option>;
spyOn(console, 'error');
stub = ReactTestUtils.renderIntoDocument(stub);

var node = React.findDOMNode(stub);

expect(console.error.calls.length).toBe(0);
expect(node.innerHTML).toBe('1 2');
});

});