Skip to content

Conversation

mejiaj
Copy link
Contributor

@mejiaj mejiaj commented May 4, 2023

Summary

Adds the following functionality:

  1. Validate on paste.
  2. Strip invalid characters from paste, but keep valid. This is still WIP ― submitting for now to share core functional changes.

Note
Unit tests haven't been added yet due to issues mocking paste event. See Mozilla note below:
It's possible to construct and dispatch a synthetic paste event, but this will not affect the document's contents.

Related pull requests

Related to #5227.

Preview link

Preview link:
Input mask

Problem statement

  1. Previously, no validation on paste.
  2. Pasting a mix of valid/invalid characters returns an empty string, instead of the valid characters.

Solution

  1. Removed handlingPaste from enhance() ― Enhance is only run on init and we shouldn't be assuming user is pasting. [35aff85 - line 1051-1058]
  2. Updated comment functions (formatting, spacing, line length).
  3. Renamed getPastedText() → getValidPastedText() for clarity and accuracy. [50808ab]
  4. Allow createStatusMessages() to accept a user set randomID ― helpful in unit testing.
  5. Removed getMaskedElements() because it wasn't returning anything useful. The passed inputEl was being returned as a newly named maskedEl. ⚠️ This was for the previous usa-input-mask wrapper that contained everything. A function like this might return as we continue to develop. [35aff85]
  6. Added SSN test [eb4fa24].
  7. Replace for loops in favor of removeFormatCharacters() [42e9fcb].

Major changes

  1. Refactored and reused more code in handlepaste().
  2. Removed unused code.
  3. Renamed getPastedText() to getValidPastedText().
  4. Brought back previous functionality where pasting invalid results in no input ― still working on this.

James Mejia added 11 commits May 3, 2023 14:02
Adjustments for readability.
- Allow randomID to be passed (useful in unit tests)
- Separate ID generation from string for readability
This function returned the input element, which made it redundant.
It _used_ to return the component container, but that was removed in 9a49a4e.
.
- Remove redundant vars in favor of default param pastedText
- Remove loop in favor of removeFormatCharacters()
- Pass cursor position to getValidPastedText()
- Create new const to track starting point from cursor position
@mejiaj mejiaj marked this pull request as ready for review May 10, 2023 12:11
@mejiaj mejiaj requested a review from amyleadem May 10, 2023 12:11
@mejiaj mejiaj changed the title Input mask: validate on paste Input mask: validate on paste cleanup May 10, 2023
@amyleadem amyleadem merged commit 24ca4f9 into al-input-mask May 15, 2023
@mejiaj mejiaj deleted the jm-input-mask-paste branch May 15, 2023 19:52
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