Skip to content

Commit 965d735

Browse files
authored
fix(phone): Phone should accept incomplete phone number of selected country (even with strict=true) (#1982)
1 parent 06c42c2 commit 965d735

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import {DemoPath} from '@demo/constants';
2+
3+
describe('Phone [strict]=true', () => {
4+
describe('[countryIsoCode]=KZ', () => {
5+
beforeEach(() => {
6+
cy.visit(`/${DemoPath.PhonePackage}/API?countryIsoCode=KZ&strict=true`);
7+
cy.get('#demo-content input')
8+
.should('be.visible')
9+
.first()
10+
.focus()
11+
.should('have.value', '+7 ')
12+
.as('input');
13+
});
14+
15+
it('Paste +7 777 (+7 is treated as country prefix => no prefix duplication)', () => {
16+
cy.get('@input')
17+
.paste('+7 777')
18+
.should('have.value', '+7 777')
19+
.should('have.prop', 'selectionStart', '+7 777'.length)
20+
.should('have.prop', 'selectionEnd', '+7 777'.length);
21+
});
22+
23+
it('Paste +7777 (+7 is treated as country prefix => no prefix duplication)', () => {
24+
cy.get('@input')
25+
.paste('+7777')
26+
.should('have.value', '+7 777')
27+
.should('have.prop', 'selectionStart', '+7 777'.length)
28+
.should('have.prop', 'selectionEnd', '+7 777'.length);
29+
});
30+
31+
it('Paste 7777 (no plus sign => all 4 sevens are treated as of incomplete value (+7 is added automatically))', () => {
32+
cy.get('@input')
33+
.paste('7777')
34+
.should('have.value', '+7 777 7')
35+
.should('have.prop', 'selectionStart', '+7 777 7'.length)
36+
.should('have.prop', 'selectionEnd', '+7 777 7'.length);
37+
});
38+
});
39+
40+
describe('[countryIsoCode]=RU', () => {
41+
beforeEach(() => {
42+
cy.visit(`/${DemoPath.PhonePackage}/API?countryIsoCode=RU&strict=true`);
43+
cy.get('#demo-content input')
44+
.should('be.visible')
45+
.first()
46+
.focus()
47+
.should('have.value', '+7 ')
48+
.as('input');
49+
});
50+
51+
[
52+
'+7 912 345-67-89',
53+
'+79123456789',
54+
'79123456789', // even without plus sign => no country prefix duplication
55+
'89123456789', // 8 should be replaced by +7 automatically
56+
'9123456789',
57+
].forEach((value) => {
58+
it(`Paste ${value}`, () => {
59+
cy.get('@input')
60+
.paste(value)
61+
.should('have.value', '+7 912 345-67-89')
62+
.should('have.prop', 'selectionStart', '+7 912 345-67-89'.length)
63+
.should('have.prop', 'selectionEnd', '+7 912 345-67-89'.length);
64+
});
65+
});
66+
});
67+
});

projects/phone/src/lib/masks/phone/phone-mask-non-strict.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ export function maskitoPhoneNonStrictOptionsGenerator({
4141
? ['+', /\d/]
4242
: generatePhoneMask({value, template: currentTemplate, prefix});
4343
},
44-
postprocessors: [phoneLengthPostprocessorGenerator(metadata)],
4544
preprocessors: [
4645
validatePhonePreprocessorGenerator({
4746
prefix,
4847
countryIsoCode: defaultIsoCode,
4948
metadata,
5049
}),
5150
],
51+
postprocessors: [phoneLengthPostprocessorGenerator(metadata)],
5252
};
5353
}

projects/phone/src/lib/masks/phone/phone-mask-strict.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ export function maskitoPhoneStrictOptionsGenerator({
4949
value.length,
5050
]),
5151
],
52-
postprocessors: [
53-
maskitoPrefixPostprocessorGenerator(prefix),
54-
phoneLengthPostprocessorGenerator(metadata),
55-
],
5652
preprocessors: [
5753
cutInitCountryCodePreprocessor({countryIsoCode, metadata}),
5854
validatePhonePreprocessorGenerator({prefix, countryIsoCode, metadata}),
5955
],
56+
postprocessors: [
57+
maskitoPrefixPostprocessorGenerator(prefix),
58+
phoneLengthPostprocessorGenerator(metadata),
59+
],
6060
};
6161
}

projects/phone/src/lib/masks/phone/processors/validate-phone-preprocessor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function validatePhonePreprocessorGenerator({
4040
metadata,
4141
);
4242

43-
if (!validationError) {
43+
if (!validationError || validationError === 'TOO_SHORT') {
4444
// handle paste-event with different code, for example for 8 / +7
4545
const phone = countryIsoCode
4646
? parsePhoneNumber(data, countryIsoCode, metadata)

0 commit comments

Comments
 (0)