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 uncontrolled select not set value on update #907

Closed
wants to merge 2 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/browser/dom/components/ReactDOMSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ function selectValueType(props, propName, componentName) {

/**
* If `value` is supplied, updates <option> elements on mount and update.
* @param {ReactComponent} component Instance of ReactDOMSelect
* @param {?*} propValue For uncontrolled components, null/undefined. For
* controlled components, a string (or with `multiple`, a list of strings).
* @private
*/
function updateOptions() {
/*jshint validthis:true */
var multiple = this.props.multiple;
var propValue = LinkedValueUtils.getValue(this);
var value = propValue != null ? propValue : this.state.value;
var options = this.getDOMNode().options;
function updateOptions(component, propValue) {
var multiple = component.props.multiple;
var value = propValue != null ? propValue : component.state.value;
var options = component.getDOMNode().options;
var selectedValue, i, l;
if (multiple) {
selectedValue = {};
Expand Down Expand Up @@ -136,9 +137,17 @@ var ReactDOMSelect = ReactCompositeComponent.createClass({
return select(props, this.props.children);
},

componentDidMount: updateOptions,
componentDidMount: function() {
var value = LinkedValueUtils.getValue(this);
updateOptions(this, value);
},

componentDidUpdate: updateOptions,
componentDidUpdate: function() {
var value = LinkedValueUtils.getValue(this);
if (value != null) {
updateOptions(this, value);
}
},

_handleChange: function(event) {
var returnValue;
Expand Down
17 changes: 17 additions & 0 deletions src/browser/dom/components/__tests__/ReactDOMSelect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ describe('ReactDOMSelect', function() {
expect(node.value).toEqual('giraffe');
});

it('should not control when using `defaultValue`', function() {
var stub =
<select defaultValue="giraffe">
<option value="monkey">A monkey!</option>
<option value="giraffe">A giraffe!</option>
<option value="gorilla">A gorilla!</option>
</select>;
var node = renderSelect(stub);

expect(node.value).toBe('giraffe');

node.value = 'monkey';
stub.forceUpdate();
// Uncontrolled selects shouldn't change the value after first mounting
expect(node.value).toEqual('monkey');
});

it('should allow setting `defaultValue` with multiple', function() {
var stub =
<select multiple={true} defaultValue={['giraffe', 'gorilla']}>
Expand Down