Skip to content

Commit 10b8636

Browse files
committed
Corrects error message for select with props.multiple set to true and a null value.
1 parent 55b3172 commit 10b8636

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

packages/react-dom/src/__tests__/ReactDOMInput-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ describe('ReactDOMInput', () => {
796796
ReactTestUtils.renderIntoDocument(<input type="text" value={null} />);
797797
expectDev(console.error.calls.argsFor(0)[0]).toContain(
798798
'`value` prop on `input` should not be null. ' +
799-
'Consider using the empty string to clear the component or `undefined` ' +
799+
'Consider using an empty string to clear the component or `undefined` ' +
800800
'for uncontrolled components.',
801801
);
802802

packages/react-dom/src/__tests__/ReactDOMSelect-test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ describe('ReactDOMSelect', () => {
514514
);
515515
expectDev(console.error.calls.argsFor(0)[0]).toContain(
516516
'`value` prop on `select` should not be null. ' +
517-
'Consider using the empty string to clear the component or `undefined` ' +
517+
'Consider using an empty string to clear the component or `undefined` ' +
518518
'for uncontrolled components.',
519519
);
520520

@@ -524,6 +524,25 @@ describe('ReactDOMSelect', () => {
524524
expectDev(console.error.calls.count()).toBe(1);
525525
});
526526

527+
it('should warn if value is null and multiple is true', () => {
528+
spyOn(console, 'error');
529+
ReactTestUtils.renderIntoDocument(
530+
<select value={null} multiple={true}><option value="test" /></select>,
531+
);
532+
533+
expectDev(console.error.calls.argsFor(0)[0]).toContain(
534+
'`value` prop on `select` should not be null. ' +
535+
'Consider using an empty array when multiple is ' +
536+
'set to true to clear the component or `undefined` ' +
537+
'for uncontrolled components.',
538+
);
539+
540+
ReactTestUtils.renderIntoDocument(
541+
<select value={null} multiple={true}><option value="test" /></select>,
542+
);
543+
expectDev(console.error.calls.count()).toBe(1);
544+
});
545+
527546
it('should refresh state on change', () => {
528547
var stub = (
529548
<select value="giraffe" onChange={noop}>

packages/react-dom/src/__tests__/ReactDOMTextarea-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ describe('ReactDOMTextarea', () => {
357357
ReactTestUtils.renderIntoDocument(<textarea value={null} />);
358358
expectDev(console.error.calls.argsFor(0)[0]).toContain(
359359
'`value` prop on `textarea` should not be null. ' +
360-
'Consider using the empty string to clear the component or `undefined` ' +
360+
'Consider using an empty string to clear the component or `undefined` ' +
361361
'for uncontrolled components.',
362362
);
363363

packages/react-dom/src/shared/ReactDOMNullInputValuePropHook.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ if (__DEV__) {
1212
var warning = require('fbjs/lib/warning');
1313
}
1414

15-
var didWarnValueNull = false;
15+
var didWarnValueNull = {
16+
default: false,
17+
multiple: false,
18+
};
1619

1720
function getStackAddendum() {
1821
var stack = ReactDebugCurrentFrame.getStackAddendum();
@@ -23,17 +26,25 @@ function validateProperties(type, props) {
2326
if (type !== 'input' && type !== 'textarea' && type !== 'select') {
2427
return;
2528
}
26-
if (props != null && props.value === null && !didWarnValueNull) {
29+
30+
var isMultipleSelect = type === 'select' && props.multiple === true;
31+
var errorType = isMultipleSelect ? 'multiple' : 'default';
32+
var isFirstError = !didWarnValueNull[errorType];
33+
34+
if (props != null && props.value === null && isFirstError) {
2735
warning(
2836
false,
2937
'`value` prop on `%s` should not be null. ' +
30-
'Consider using the empty string to clear the component or `undefined` ' +
38+
'Consider using an empty %s to clear the component or `undefined` ' +
3139
'for uncontrolled components.%s',
3240
type,
41+
errorType === 'multiple'
42+
? 'array when multiple is set to true'
43+
: 'string',
3344
getStackAddendum(),
3445
);
3546

36-
didWarnValueNull = true;
47+
didWarnValueNull[errorType] = true;
3748
}
3849
}
3950

0 commit comments

Comments
 (0)