Allow passing custom regex objects as props in order to handle more complex format strings #2
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.
Reason for changes
Because with an international phone format like
+1 (###) ##-##-##
, the current regex for stripping is insufficient:onChange
runs on the component, it duplicates all allowed characters in the format string (in this case,1
).An obvious fix would be to:
In order to do both of those, custom regex objects need to be passed into the
Input
component.I already fixed the problem while making a fork of this repo work in my own project, so instead of opening an issue I'm submitting fixed code as a pull request.
What has been done
strip: RegExp
andallow: RegExp
props into theInput
component.Reason: The former is used to find all pattern characters (the ones to be stripped), the latter is used to determine all allowed characters.
This should not break backwards compatibility, since I have added old regex values as default values for those props.
stripPatternCharacters
andisUserCharacter
functions accept regex as their second parameter.Reason: so that
Input
can pass customstrip
andallow
props into those functions.isUserCharacter
tomatchesRegex
.Reason: otherwise changes on line 95-97 (making patternCharacterDeleted check for all characters that must be stripped) result in counterintuitive code (since isUserCharacter would be called to check with stripRegex, not with allowRegex).
Introduced an updatedCursorPosition constant (line 85 onwards) to keep track of the updated cursor position.
Reason: otherwise the order in which characters are inserted breaks with complicated formats such as
+1 (###) ##-##-##
.Reason: accessing it from inside the Input component is part of the change 2.
To make my changes easier to track, I have included comments that start with
// CHANGE:
.Unfortunately, all of those changes are intertwined (changes 2-5 are necessary to make change 1 work properly), so I couldn't separate them into different pull requests.
Known issues with this pull request
How to replicate: in the last test in index.spec.js, you will see that changing line 89 to
input.setSelectionRange(0, 0);
makes jest input characters at the end of the string.I tested this manually, and this issue does not replicate in a live browser.
+44 (###) ##-##-##
, you move the cursor to the position such as(1, 1)
or(2, 2)
(after the + and before the last 4), every onChange still duplicates the numbers.I'm not sure why this happens: rubber-duck-debugging the code on lines 124-142 didn't produce any obvious answers.
Need feedback: