Skip to content

Commit 98a6bb2

Browse files
Fix phone input handling of 11-digit autofilled numbers with leading 1
1 parent df67a98 commit 98a6bb2

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

apps/docs/src/remix-hook-form/phone-input.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ describe('PhoneInput Component', () => {
116116
});
117117
});
118118

119+
it('handles 11-digit US numbers with leading 1 (autofill case)', async () => {
120+
const user = userEvent.setup();
121+
render(<TestPhoneInputForm />);
122+
123+
const usaPhoneInput = screen.getByLabelText('USA Phone Number') as HTMLInputElement;
124+
125+
// Simulate autofill with 11 digits starting with 1
126+
await user.type(usaPhoneInput, '12025550123');
127+
128+
// Should format correctly by removing the leading 1
129+
await waitFor(() => {
130+
expect(usaPhoneInput.value).toBe('(202) 555-0123');
131+
});
132+
});
133+
119134
it('accepts international number with + and inserts spaces', async () => {
120135
const user = userEvent.setup();
121136
render(<TestPhoneInputForm />);

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
"name": "forms",
33
"version": "0.2.0",
44
"private": true,
5-
"workspaces": ["apps/*", "packages/*"],
5+
"workspaces": [
6+
"apps/*",
7+
"packages/*"
8+
],
69
"scripts": {
710
"start": "yarn dev",
811
"dev": "turbo run dev",

packages/components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lambdacurry/forms",
3-
"version": "0.19.4",
3+
"version": "0.19.5",
44
"type": "module",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

packages/components/src/ui/phone-input.tsx

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ function extractDigits(input: string): string {
2323
}
2424

2525
function formatUS(digits: string): string {
26-
const d = digits.slice(0, 10);
26+
// Handle case where an 11-digit number with leading "1" is provided (common in autofill)
27+
let d = digits;
28+
if (digits.length === 11 && digits.startsWith('1')) {
29+
d = digits.slice(1); // Remove the leading "1" country code
30+
} else {
31+
d = digits.slice(0, 10); // Otherwise just take first 10 digits as before
32+
}
33+
2734
if (d.length === 0) return '';
2835
if (d.length <= 3) return `(${d}`;
2936
if (d.length <= 6) return `(${d.slice(0, 3)}) ${d.slice(3)}`;
@@ -62,7 +69,8 @@ export const PhoneNumberInput = ({
6269
const formatted = typer.input(normalized);
6370
setDisplay(formatted);
6471
} else {
65-
const digits = extractDigits(String(value)).slice(0, 10);
72+
// Remove the slice(0, 10) to allow handling 11-digit numbers with leading 1
73+
const digits = extractDigits(String(value));
6674
setDisplay(formatUS(digits));
6775
}
6876
}, [value, isInternational]);
@@ -80,10 +88,19 @@ export const PhoneNumberInput = ({
8088
return;
8189
}
8290

83-
const digits = extractDigits(raw).slice(0, 10);
91+
// Remove the slice(0, 10) to allow handling 11-digit numbers with leading 1
92+
const digits = extractDigits(raw);
93+
// Handle case where an 11-digit number with leading "1" is provided
94+
let normalizedDigits = digits;
95+
if (digits.length === 11 && digits.startsWith('1')) {
96+
normalizedDigits = digits.slice(1); // Remove the leading "1" country code
97+
} else {
98+
normalizedDigits = digits.slice(0, 10); // Otherwise just take first 10 digits
99+
}
100+
84101
const formatted = formatUS(digits);
85102
setDisplay(formatted);
86-
onChange?.(digits || undefined);
103+
onChange?.(normalizedDigits || undefined);
87104
};
88105

89106
const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
@@ -92,13 +109,17 @@ export const PhoneNumberInput = ({
92109
const isNumberKey = NUMBER_KEY_REGEX.test(e.key);
93110
const isModifier = e.ctrlKey || e.metaKey || e.altKey;
94111
const allowed = ['Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', 'Tab', 'Home', 'End', 'Enter'];
95-
if (!isModifier && isNumberKey && currentDigits.length >= 10) {
112+
113+
// Allow typing if we have fewer than 10 digits or if we have 11 digits but the first is '1'
114+
const isComplete = currentDigits.length >= 10 && !(currentDigits.length === 11 && currentDigits.startsWith('1'));
115+
116+
if (!isModifier && isNumberKey && isComplete) {
96117
// Prevent adding more digits once 10-digit US number is complete
97118
e.preventDefault();
98119
return;
99120
}
100121
if (allowed.includes(e.key)) return;
101-
// Allow other typical keys; restriction handled by formatting and slice(0,10)
122+
// Allow other typical keys; restriction handled by formatting
102123
}
103124
};
104125

0 commit comments

Comments
 (0)