Skip to content

Commit 06cc32a

Browse files
author
Caleb Doucet
committed
Add fix for an autocomplete case where there is no maskPlaceholder
1 parent fdb11de commit 06cc32a

File tree

3 files changed

+58
-17
lines changed

3 files changed

+58
-17
lines changed

.size-snapshot.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
22
"dist/react-input-mask.js": {
3-
"bundled": 81133,
4-
"minified": 26343,
5-
"gzipped": 8443
3+
"bundled": 81871,
4+
"minified": 26472,
5+
"gzipped": 8464
66
},
77
"lib/react-input-mask.development.js": {
8-
"bundled": 31686,
9-
"minified": 13230,
10-
"gzipped": 4373
8+
"bundled": 32398,
9+
"minified": 13359,
10+
"gzipped": 4396
1111
},
1212
"dist/react-input-mask.min.js": {
13-
"bundled": 45624,
14-
"minified": 15614,
15-
"gzipped": 5406
13+
"bundled": 46362,
14+
"minified": 15743,
15+
"gzipped": 5422
1616
},
1717
"lib/react-input-mask.production.min.js": {
18-
"bundled": 30355,
19-
"minified": 12153,
20-
"gzipped": 4035
18+
"bundled": 31067,
19+
"minified": 12282,
20+
"gzipped": 4057
2121
}
2222
}

src/utils/mask.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ export default class MaskUtils {
226226
{ value, selection },
227227
{ value: previousValue, selection: previousSelection }
228228
) => {
229+
const { maskPlaceholder } = this.maskOptions;
229230
if (
230231
// Autocomplete will set the previous selection to the length of the autocompleted value
231232
previousSelection.end < previousValue.length &&
@@ -243,9 +244,26 @@ export default class MaskUtils {
243244
// When both previous and current state have no selection length, the cursor index is less than it was before
244245
// and the cursor is at the end of the new value
245246
// Check each character to see if there are any changes which is only possible if the value was autocompleted.
246-
return value
247-
.split("")
248-
.some((char, index) => char !== previousValue[index]);
247+
return value.split("").some((char, index) => {
248+
return char !== previousValue[index];
249+
});
250+
}
251+
252+
if (
253+
!maskPlaceholder &&
254+
previousSelection.length === 0 &&
255+
previousValue.length < value.length
256+
) {
257+
// If there is no mask placeholder, the selection is 0 and the new value is longer than the previous value
258+
// (characters have been added)
259+
return value.split("").some((char, index) => {
260+
// Check each character before the selection to see if they have changed
261+
if (index < previousSelection.start) {
262+
// Any character before the previous selection that changes will be changed because of autofill
263+
return char !== previousValue[index];
264+
}
265+
return false;
266+
});
249267
}
250268

251269
return false;
@@ -264,8 +282,12 @@ export default class MaskUtils {
264282

265283
if (this.isAutoFilled(currentState, previousState)) {
266284
// If the value is autocompleted treat it as if the input started empty.
267-
previousValue = "";
268-
previousSelection = { start: 0, end: 0, length: 0 };
285+
previousValue = prefix;
286+
previousSelection = {
287+
start: 0,
288+
end: 0,
289+
length: 0
290+
};
269291
}
270292

271293
if (selection.end > previousSelection.start) {

tests/input/input.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,8 @@ describe("react-input-mask", () => {
563563
await simulateBackspacePress(input);
564564
expect(input.value).to.equal("+7 (495) 156 45 4");
565565

566+
await setCursorPosition(input, 17);
567+
566568
input.value = "+7 (";
567569
setCursorPosition(input, 4);
568570
TestUtils.Simulate.change(input);
@@ -1239,6 +1241,23 @@ describe("react-input-mask", () => {
12391241
expect(input.value).to.equal("1234-5678");
12401242
});
12411243

1244+
it("should handle autofill when there is a prefix and no mask placeholder", async () => {
1245+
const { input } = createInput(
1246+
<Input mask="(9999-9999)" defaultValue="(" maskPlaceholder={null} />
1247+
);
1248+
await simulateFocus(input);
1249+
1250+
setCursorPosition(input, 1);
1251+
TestUtils.Simulate.change(input);
1252+
expect(input.value).to.equal("(");
1253+
1254+
input.value = "12345678";
1255+
setCursorPosition(input, 8);
1256+
TestUtils.Simulate.change(input);
1257+
1258+
expect(input.value).to.equal("(1234-5678)");
1259+
});
1260+
12421261
it("should handle transition between masked and non-masked state", async () => {
12431262
const { input, setProps } = createInput(<Input />);
12441263
setProps({

0 commit comments

Comments
 (0)