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: input maxlength constraint should respect selection #1254

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

marshcat0
Copy link

@marshcat0 marshcat0 commented Jan 16, 2025

What:

For a text input / textarea that has maxlength, when the value reachs the maxlength limit, and the value is selected, new text should still be inserted. Becase the new text will replace the selected text.

The old code did not take the selection into consideration, causing new text not inserted.

Why:

To match how browsers behave

How:

When calulate the space remaining for maxlength, add the selected range length to the space.

Checklist:

  • Documentation
  • Tests
  • Ready to be merged
  • According to the html spec, the selectionStart is less than or equal to selectionEnd, so inputRange.endOffset - inputRange.startOffset must greater than or equal 0, which meets our expectation.
  • I want to reuse the maxlength test case, but the ternary expression added looks terrible. So feel free if you wannt to edit the test case :)

Copy link

codesandbox-ci bot commented Jan 16, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@ph-fritsche ph-fritsche self-assigned this Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants