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

fix: select console error to not suggest to set readonly to true #27740

Merged
merged 4 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export function checkControlledValueProps(
console.error(
'You provided a `value` prop to a form field without an ' +
'`onChange` handler. This will render a read-only field. If ' +
'the field should be mutable use `defaultValue`. Otherwise, ' +
'set either `onChange` or `readOnly`.',
'the field should be mutable use `defaultValue`. Otherwise, set %s.',
tagName === 'select' ? '`onChange`' : 'either `onChange` or `readOnly`',
);
Biki-das marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
117 changes: 117 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMSelect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1289,5 +1289,122 @@ describe('ReactDOMSelect', () => {
' This value must be coerced to a string before using it here.',
);
});

it('should not warn about missing onChange if value is not set', () => {
expect(() => {
ReactTestUtils.renderIntoDocument(
<select>
<option value="monkey">A monkey!</option>
<option value="giraffe">A giraffe!</option>
<option value="gorilla">A gorilla!</option>
</select>,
);
}).not.toThrow();
});

it('should not throw an error about missing onChange if value is undefined', () => {
expect(() => {
ReactTestUtils.renderIntoDocument(
<select value={undefined}>
<option value="monkey">A monkey!</option>
<option value="giraffe">A giraffe!</option>
<option value="gorilla">A gorilla!</option>
</select>,
);
}).not.toThrow();
});

it('should not warn about missing onChange if onChange is set', () => {
const change = jest.fn();
expect(() => {
ReactTestUtils.renderIntoDocument(
<select value="monkey" onChange={change}>
<option value="monkey">A monkey!</option>
<option value="giraffe">A giraffe!</option>
<option value="gorilla">A gorilla!</option>
</select>,
);
}).not.toThrow();
});

it('should not warn about missing onChange if disabled is true', () => {
expect(() => {
ReactTestUtils.renderIntoDocument(
<select value="monkey" disabled={true}>
<option value="monkey">A monkey!</option>
<option value="giraffe">A giraffe!</option>
<option value="gorilla">A gorilla!</option>
</select>,
);
}).not.toThrow();
});

it('should warn about missing onChange if value is false', () => {
expect(() =>
ReactTestUtils.renderIntoDocument(
<select value={false}>
<option value="monkey">A monkey!</option>
<option value="giraffe">A giraffe!</option>
<option value="gorilla">A gorilla!</option>
</select>,
),
).toErrorDev(
'Warning: You provided a `value` prop to a form ' +
'field without an `onChange` handler. This will render a read-only ' +
'field. If the field should be mutable use `defaultValue`. ' +
'Otherwise, set `onChange`.',
);
});

it('should warn about missing onChange if value is 0', () => {
expect(() =>
ReactTestUtils.renderIntoDocument(
<select value={0}>
<option value="monkey">A monkey!</option>
<option value="giraffe">A giraffe!</option>
<option value="gorilla">A gorilla!</option>
</select>,
),
).toErrorDev(
'Warning: You provided a `value` prop to a form ' +
'field without an `onChange` handler. This will render a read-only ' +
'field. If the field should be mutable use `defaultValue`. ' +
'Otherwise, set `onChange`.',
);
});

it('should warn about missing onChange if value is "0"', () => {
expect(() =>
ReactTestUtils.renderIntoDocument(
<select value="0">
<option value="monkey">A monkey!</option>
<option value="giraffe">A giraffe!</option>
<option value="gorilla">A gorilla!</option>
</select>,
),
).toErrorDev(
'Warning: You provided a `value` prop to a form ' +
'field without an `onChange` handler. This will render a read-only ' +
'field. If the field should be mutable use `defaultValue`. ' +
'Otherwise, set `onChange`.',
);
});

it('should warn about missing onChange if value is ""', () => {
expect(() =>
ReactTestUtils.renderIntoDocument(
<select value="">
<option value="monkey">A monkey!</option>
<option value="giraffe">A giraffe!</option>
<option value="gorilla">A gorilla!</option>
</select>,
),
).toErrorDev(
'Warning: You provided a `value` prop to a form ' +
'field without an `onChange` handler. This will render a read-only ' +
'field. If the field should be mutable use `defaultValue`. ' +
'Otherwise, set `onChange`.',
);
});
Biki-das marked this conversation as resolved.
Show resolved Hide resolved
});
});