Fix minLength and maxLength constraints on textareas in Chrome #27635
+36
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #19474
This fixes an issue that causes
<textarea>
with theminLength
andmaxLength
props not to validate when the textarea has a controlledvalue
. This Codesandbox has a minimal reproduction of the issue. Typing into the<input>
element updates thevalidity
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 nativetextarea.defaultValue
property, which is equivalent totextContent
(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
andmaxLength
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 nativedefaultValue
when aminLength
ormaxLength
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.