Skip to content

Commit

Permalink
Changed the error message displayed when a select element has props.m…
Browse files Browse the repository at this point in the history
…ultiple set to true and value set to null (#11141)

* Corrects error message for select with props.multiple set to true and a null value.

* Don't bother deduplicating based on type

* Make the code a bit simpler (and more verbose)
  • Loading branch information
nealwright authored and gaearon committed Oct 31, 2017
1 parent 4ce5da7 commit 4a43cf6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/react-dom/src/__tests__/ReactDOMInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ describe('ReactDOMInput', () => {
ReactTestUtils.renderIntoDocument(<input type="text" value={null} />);
expectDev(console.error.calls.argsFor(0)[0]).toContain(
'`value` prop on `input` should not be null. ' +
'Consider using the empty string to clear the component or `undefined` ' +
'Consider using an empty string to clear the component or `undefined` ' +
'for uncontrolled components.',
);

Expand Down
23 changes: 22 additions & 1 deletion packages/react-dom/src/__tests__/ReactDOMSelect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('ReactDOMSelect', () => {
var noop = function() {};

beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMServer = require('react-dom/server');
Expand Down Expand Up @@ -514,7 +515,7 @@ describe('ReactDOMSelect', () => {
);
expectDev(console.error.calls.argsFor(0)[0]).toContain(
'`value` prop on `select` should not be null. ' +
'Consider using the empty string to clear the component or `undefined` ' +
'Consider using an empty string to clear the component or `undefined` ' +
'for uncontrolled components.',
);

Expand All @@ -524,6 +525,26 @@ describe('ReactDOMSelect', () => {
expectDev(console.error.calls.count()).toBe(1);
});

it('should warn if value is null and multiple is true', () => {
spyOn(console, 'error');
ReactTestUtils.renderIntoDocument(
<select value={null} multiple={true}><option value="test" /></select>,
);

expectDev(console.error.calls.count()).toBe(1);
expectDev(console.error.calls.argsFor(0)[0]).toContain(
'`value` prop on `select` should not be null. ' +
'Consider using an empty array when `multiple` is ' +
'set to `true` to clear the component or `undefined` ' +
'for uncontrolled components.',
);

ReactTestUtils.renderIntoDocument(
<select value={null} multiple={true}><option value="test" /></select>,
);
expectDev(console.error.calls.count()).toBe(1);
});

it('should refresh state on change', () => {
var stub = (
<select value="giraffe" onChange={noop}>
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/src/__tests__/ReactDOMTextarea-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ describe('ReactDOMTextarea', () => {
ReactTestUtils.renderIntoDocument(<textarea value={null} />);
expectDev(console.error.calls.argsFor(0)[0]).toContain(
'`value` prop on `textarea` should not be null. ' +
'Consider using the empty string to clear the component or `undefined` ' +
'Consider using an empty string to clear the component or `undefined` ' +
'for uncontrolled components.',
);

Expand Down
29 changes: 20 additions & 9 deletions packages/react-dom/src/shared/ReactDOMNullInputValuePropHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,28 @@ function validateProperties(type, props) {
if (type !== 'input' && type !== 'textarea' && type !== 'select') {
return;
}
if (props != null && props.value === null && !didWarnValueNull) {
warning(
false,
'`value` prop on `%s` should not be null. ' +
'Consider using the empty string to clear the component or `undefined` ' +
'for uncontrolled components.%s',
type,
getStackAddendum(),
);

if (props != null && props.value === null && !didWarnValueNull) {
didWarnValueNull = true;
if (type === 'select' && props.multiple) {
warning(
false,
'`value` prop on `%s` should not be null. ' +
'Consider using an empty array when `multiple` is set to `true` ' +
'to clear the component or `undefined` for uncontrolled components.%s',
type,
getStackAddendum(),
);
} else {
warning(
false,
'`value` prop on `%s` should not be null. ' +
'Consider using an empty string to clear the component or `undefined` ' +
'for uncontrolled components.%s',
type,
getStackAddendum(),
);
}
}
}

Expand Down

0 comments on commit 4a43cf6

Please sign in to comment.