mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Changed the error message displayed when a select element has props.multiple 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)
This commit is contained in:
+1
-1
@@ -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.',
|
||||
);
|
||||
|
||||
|
||||
+22
-1
@@ -18,6 +18,7 @@ describe('ReactDOMSelect', () => {
|
||||
var noop = function() {};
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
React = require('react');
|
||||
ReactDOM = require('react-dom');
|
||||
ReactDOMServer = require('react-dom/server');
|
||||
@@ -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.',
|
||||
);
|
||||
|
||||
@@ -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}>
|
||||
|
||||
@@ -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.',
|
||||
);
|
||||
|
||||
|
||||
@@ -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(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user