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

Conversation

devongovett
Copy link
Contributor

Summary

Fixes #19474

This fixes an issue that causes <textarea> with the minLength and maxLength props not to validate when the textarea has a controlled value. This Codesandbox has a minimal reproduction of the issue. Typing into the <input> element updates the validity of the input element correctly as shown in the console logs, but not the <textarea>.

This is caused by React syncing the controlled value prop into the native textarea.defaultValue property, which is equivalent to textContent (i.e. the children of the <textarea> element). When the children change, Chrome marks the change as a programmatic update rather than a user update here. Then, when checking validity, it skips the length constraint for programmatically set changes here.

This issue is related to #11896. I'm not sure of the overall status of that effort, but I think since the minLength and maxLength props are currently broken entirely when a <textarea> is controlled, we could potentially consider changing the behavior for only that case for now without it being considered a breaking change. This PR disables updating the native defaultValue when a minLength or maxLength constraint is set. The idea is to make the minimal change possible to fix the issue until the larger refactor can be done in a major version. Open to feedback on this approach.

How did you test this change?

Added a unit test. Created a small fixture locally and tested the build in Chrome.

@react-sizebot
Copy link

react-sizebot commented Nov 1, 2023

Comparing: 77c4ac2...9ac0791

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js +0.02% 175.24 kB 175.28 kB +0.02% 54.53 kB 54.54 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js +0.02% 177.37 kB 177.40 kB +0.02% 55.19 kB 55.20 kB
facebook-www/ReactDOM-prod.classic.js +0.01% 567.70 kB 567.78 kB +0.01% 99.96 kB 99.97 kB
facebook-www/ReactDOM-prod.modern.js +0.01% 551.56 kB 551.63 kB +0.02% 97.06 kB 97.07 kB

Significant size changes

Includes any change greater than 0.2%:

(No significant changes)

Generated by 🚫 dangerJS against 9ac0791

@sebmarkbage
Copy link
Collaborator

This issue is related to #11896. I'm not sure of the overall status of that effort

The latest thinking is that React should allow defaultValue to be set even on controlled values to indicate what the "saved" value is. So it plays well with these assumptions as well as form resetting. That change is not planned yet and even so the current behavior would be still available for backwards compat so needs to be addressed somehow.

Copy link

github-actions bot commented Apr 5, 2024

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Apr 5, 2024
@devongovett
Copy link
Contributor Author

Any chance we can fix this for React 19?

@github-actions github-actions bot removed the Resolution: Stale Automatically closed due to inactivity label Apr 10, 2024
Copy link

github-actions bot commented Jul 9, 2024

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Jul 9, 2024
Copy link

Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you!

@github-actions github-actions bot closed this Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Resolution: Stale Automatically closed due to inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Constraint Validation API not reflected on Textarea's value attribute change
4 participants