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

Changed the error message displayed when a select element has props.multiple set to true and value set to null #11141

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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