Skip to content

Commit a1a9fa7

Browse files
author
Daniel Yefet
committed
Fix cursor bugs
1 parent f118a53 commit a1a9fa7

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

.npmignore

-6
This file was deleted.

LICENCE

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Daniel Yefet
3+
Copyright (c) 2021 Daniel Yefet
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
21+
SOFTWARE.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-input-auto-format",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"author": "Daniel Yefet <[email protected]>",
55
"description": "A super simple input component that formats as you type.",
66
"keywords": [

src/index.js

+33-7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ function stripPatternCharacters(value) {
3535
return value.replace(/[^\dA-z]/g, '');
3636
}
3737

38+
function isUserCharacter(character) {
39+
return /[\dA-z]/.test(character);
40+
}
41+
3842
function Input({
3943
onChange = noop,
4044
onValueChange = noop,
@@ -56,9 +60,11 @@ function Input({
5660
let rawValue = stripPatternCharacters(inputValue);
5761

5862
if (didDelete) {
59-
const nonNumericWasDeleted = /[^\dA-z]/.test([...value][cursorPosition]);
63+
const patternCharacterDeleted = !isUserCharacter(
64+
[...value][cursorPosition]
65+
);
6066

61-
if (nonNumericWasDeleted) {
67+
if (patternCharacterDeleted) {
6268
const firstBit = inputValue.substr(0, cursorPosition);
6369
const rawFirstBit = stripPatternCharacters(firstBit);
6470

@@ -75,19 +81,39 @@ function Input({
7581

7682
const formattedValue = format(rawValue, pattern);
7783

78-
infoRef.current.formattingWasAdded =
79-
formattedValue.length - value.length > 1;
84+
infoRef.current.endOfSection = false;
85+
86+
if (!didDelete) {
87+
const formattedCharacters = [...formattedValue];
88+
const nextCharacter = formattedCharacters[cursorPosition];
89+
const nextCharacterIsPattern = !isUserCharacter(nextCharacter);
90+
const nextUserCharacterIndex = formattedValue
91+
.substr(cursorPosition)
92+
.search(/[\dA-z]/);
93+
const numbersAhead = nextUserCharacterIndex !== -1;
94+
95+
infoRef.current.endOfSection = nextCharacterIsPattern && !numbersAhead;
96+
97+
if (
98+
nextCharacterIsPattern &&
99+
!isUserCharacter(formattedCharacters[cursorPosition - 1]) &&
100+
numbersAhead
101+
)
102+
infoRef.current.cursorPosition =
103+
cursorPosition + nextUserCharacterIndex + 1;
104+
}
80105

81106
onValueChange(rawValue);
82107
onChange(event);
83108
setValue(formattedValue);
84109
}
85110

86111
useEffect(() => {
87-
const { cursorPosition, formattingWasAdded } = infoRef.current;
112+
const { cursorPosition, endOfSection } = infoRef.current;
113+
114+
if (endOfSection) return;
88115

89-
if (!formattingWasAdded)
90-
inputRef.current.setSelectionRange(cursorPosition, cursorPosition);
116+
inputRef.current.setSelectionRange(cursorPosition, cursorPosition);
91117
}, [value]);
92118

93119
return (

0 commit comments

Comments
 (0)