Skip to content

Commit 28a6063

Browse files
committed
Corrects error message for select with props.multiple set to true and a null value.
1 parent 4131af3 commit 28a6063

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

src/renderers/dom/shared/hooks/ReactDOMNullInputValuePropHook.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ if (__DEV__) {
1414
var {ReactDebugCurrentFrame} = require('ReactGlobalSharedState');
1515
}
1616

17-
var didWarnValueNull = false;
17+
var didWarnValueNull = {
18+
default: false,
19+
multiple: false,
20+
};
1821

1922
function getStackAddendum() {
2023
var stack = ReactDebugCurrentFrame.getStackAddendum();
@@ -25,17 +28,25 @@ function validateProperties(type, props) {
2528
if (type !== 'input' && type !== 'textarea' && type !== 'select') {
2629
return;
2730
}
28-
if (props != null && props.value === null && !didWarnValueNull) {
31+
32+
var isMultipleSelect = type === 'select' && props.multiple === true;
33+
var errorType = isMultipleSelect ? 'multiple' : 'default';
34+
var isFirstError = !didWarnValueNull[errorType];
35+
36+
if (props != null && props.value === null && isFirstError) {
2937
warning(
3038
false,
3139
'`value` prop on `%s` should not be null. ' +
32-
'Consider using the empty string to clear the component or `undefined` ' +
40+
'Consider using an empty %s to clear the component or `undefined` ' +
3341
'for uncontrolled components.%s',
3442
type,
43+
errorType === 'multiple'
44+
? 'array when multiple is set to true'
45+
: 'string',
3546
getStackAddendum(),
3647
);
3748

38-
didWarnValueNull = true;
49+
didWarnValueNull[errorType] = true;
3950
}
4051
}
4152

src/renderers/dom/shared/wrappers/__tests__/ReactDOMInput-test.js

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

src/renderers/dom/shared/wrappers/__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}>

src/renderers/dom/shared/wrappers/__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

0 commit comments

Comments
 (0)