Skip to content

Commit

Permalink
Corrects error message for select with props.multiple set to true and…
Browse files Browse the repository at this point in the history
… a null value.
  • Loading branch information
nealwright committed Oct 27, 2017
1 parent cc54b6f commit 5e90c3e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 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
21 changes: 20 additions & 1 deletion packages/react-dom/src/__tests__/ReactDOMSelect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,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 +524,25 @@ 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.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
19 changes: 15 additions & 4 deletions packages/react-dom/src/shared/ReactDOMNullInputValuePropHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ if (__DEV__) {
var warning = require('fbjs/lib/warning');
}

var didWarnValueNull = false;
var didWarnValueNull = {
default: false,
multiple: false,
};

function getStackAddendum() {
var stack = ReactDebugCurrentFrame.getStackAddendum();
Expand All @@ -23,17 +26,25 @@ function validateProperties(type, props) {
if (type !== 'input' && type !== 'textarea' && type !== 'select') {
return;
}
if (props != null && props.value === null && !didWarnValueNull) {

var isMultipleSelect = type === 'select' && props.multiple === true;
var errorType = isMultipleSelect ? 'multiple' : 'default';
var isFirstError = !didWarnValueNull[errorType];

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

didWarnValueNull = true;
didWarnValueNull[errorType] = true;
}
}

Expand Down

0 comments on commit 5e90c3e

Please sign in to comment.