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 minLength and maxLength constraints on textareas in Chrome #27635

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 10 additions & 1 deletion packages/react-dom-bindings/src/client/ReactDOMTextarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,16 @@ export function updateTextarea(
}
// TOOO: This should respect disableInputAttributeSyncing flag.
if (defaultValue == null) {
if (node.defaultValue !== newValue) {
// Do not sync value and defaultValue if a minLength or maxLength constraint
// is set because this causes Chrome to skip validation.
// https://github.com/facebook/react/issues/19474
if (
node.defaultValue !== newValue &&
// $FlowFixMe[prop-missing]
node.minLength === -1 &&
// $FlowFixMe[prop-missing]
node.maxLength === -1
) {
node.defaultValue = newValue;
}
return;
Expand Down
26 changes: 26 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMTextarea-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,32 @@ describe('ReactDOMTextarea', () => {
expect(set.mock.calls.length).toBe(0);
});

it('does not update defaultValue of DOM node if minLength or maxLength are set', () => {
const container = document.createElement('div');
let node;
ReactDOM.render(
<textarea
ref={n => (node = n)}
value="foo"
onChange={emptyFunction}
minLength={10}
/>,
container,
);
expect(node.defaultValue).toBe('foo');

ReactDOM.render(
<textarea
ref={n => (node = n)}
value="food"
onChange={emptyFunction}
minLength={10}
/>,
container,
);
expect(node.defaultValue).toBe('foo');
});

describe('When given a Symbol value', () => {
it('treats initial Symbol value as an empty string', () => {
const container = document.createElement('div');
Expand Down