Skip to content

Commit

Permalink
chore: fix autocomplete, fix max length
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirpotekhin committed Sep 7, 2023
1 parent 5de11f1 commit d88c05d
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ describe('Phone', () => {
cy.get('@input').type('447488269');
cy.get('@input').should('have.value', '+375 44 748-82-69');
});

it('TR: +90 539 377-07-43', () => {
openCountry('TR');

cy.get('@input').type('5393770743');
cy.get('@input').should('have.value', '+90 539 377-07-43');
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,29 +173,36 @@ describe('Phone', () => {

describe('Some countries', () => {
it('US: +1 212 343-3355', () => {
openCounrty('US');
openCountry('US');

cy.get('@input').type('12123433355');
cy.get('@input').should('have.value', '+1 212 343-3355');
});

it('KZ: +7 771 931-1111', () => {
openCounrty('KZ');
openCountry('KZ');

cy.get('@input').type('77719311111');
cy.get('@input').should('have.value', '+7 771 931-1111');
});

it('BY: +375 44 748-82-69', () => {
openCounrty('BY');
openCountry('BY');

cy.get('@input').type('375447488269');
cy.get('@input').should('have.value', '+375 44 748-82-69');
});

it('TR: +90 539 377-07-43', () => {
openCountry('TR');

cy.get('@input').type('905393770743');
cy.get('@input').should('have.value', '+90 539 377-07-43');
});
});
});

function openCounrty(code: string): void {
function openCountry(code: string): void {
cy.visit(`/${DemoPath.PhonePackage}/API?strict=false&countryCode=${code}`);
cy.get('#demo-content input').should('be.visible').first().focus().as('input');
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ChangeDetectionStrategy, Component} from '@angular/core';
import {ChangeDetectionStrategy, Component, Inject} from '@angular/core';
import {maskitoGetCountryFromNumber} from '@maskito/phone';
import {TUI_IS_APPLE} from '@taiga-ui/cdk';
import metadata from 'libphonenumber-js/min/metadata';

import mask from './mask';
Expand All @@ -17,6 +18,7 @@ import mask from './mask';
autocomplete="tel"
inputmode="tel"
tuiTextfield
[attr.pattern]="pattern"
[maskito]="mask"
/>
</tui-input>
Expand All @@ -32,4 +34,10 @@ export class PhoneMaskDocExample3 {
get countryIsoCode(): string {
return maskitoGetCountryFromNumber(this.value, metadata) || '';
}

constructor(@Inject(TUI_IS_APPLE) private readonly isApple: boolean) {}

get pattern(): string {
return this.isApple ? '+{1}[0-9]{1,3} [0-9]{1,14}' : '';
}
}
9 changes: 8 additions & 1 deletion projects/demo/src/pages/phone/phone-doc.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {ChangeDetectionStrategy, Component} from '@angular/core';
import {ChangeDetectionStrategy, Component, Inject} from '@angular/core';
import {FormControl} from '@angular/forms';
import {DocExamplePrimaryTab} from '@demo/constants';
import {maskitoPhoneOptionsGenerator} from '@maskito/phone';
import {TuiDocExample} from '@taiga-ui/addon-doc';
import {TUI_IS_APPLE} from '@taiga-ui/cdk';
import {CountryCode, getCountries} from 'libphonenumber-js/core';
import metadata from 'libphonenumber-js/min/metadata';

Expand Down Expand Up @@ -55,6 +56,12 @@ export class PhoneDocComponent implements GeneratorOptions {

maskitoOptions = maskitoPhoneOptionsGenerator(this);

constructor(@Inject(TUI_IS_APPLE) private readonly isApple: boolean) {}

get pattern(): string {
return this.isApple ? '+{1}[0-9]{1,3} [0-9]{1,14}' : '';
}

updateOptions(): void {
this.maskitoOptions = maskitoPhoneOptionsGenerator(this);
}
Expand Down
1 change: 1 addition & 0 deletions projects/demo/src/pages/phone/phone-doc.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<tui-doc-example
id="lazy-metadata"
heading="Lazy metadata"
[attr.pattern]="pattern"
[content]="lazyMetadata"
[description]="lazyDescription"
>
Expand Down
11 changes: 10 additions & 1 deletion projects/phone/src/lib/masks/phone/phone-mask-free.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ export function maskitoPhoneFreeOptionsGenerator({
}): Required<MaskitoOptions> {
const formatter = new AsYouType(defaultIsoCode, metadata);
const prefix = '+';
let template = '';
let prevPhoneLength = 0;

return {
...MASKITO_DEFAULT_OPTIONS,
mask: ({value}) => {
const template = getPhoneTemplate(formatter, value);
const newTemplate = getPhoneTemplate(formatter, value);
const phoneLength = value.replace(/\D/g, '').length;

template =
newTemplate.length < template.length && phoneLength > prevPhoneLength
? template
: newTemplate;
prevPhoneLength = phoneLength;

const mask = generatePhoneMask({value, template, prefix});

Expand Down
12 changes: 11 additions & 1 deletion projects/phone/src/lib/masks/phone/phone-mask-strict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,20 @@ export function maskitoPhoneStrictOptionsGenerator({
const formatter = new AsYouType(countryIsoCode, metadata);
const prefix = `+${code} `;

let template = '';
let prevPhoneLength = 0;

return {
...MASKITO_DEFAULT_OPTIONS,
mask: ({value}) => {
const template = getPhoneTemplate(formatter, value);
const newTemplate = getPhoneTemplate(formatter, value);
const phoneLength = value.replace(/\D/g, '').length;

template =
newTemplate.length < template.length && phoneLength > prevPhoneLength
? template
: newTemplate;
prevPhoneLength = phoneLength;

return generatePhoneMask({value, template, prefix});
},
Expand Down
26 changes: 26 additions & 0 deletions projects/phone/src/lib/masks/phone/tests/phone-mask.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,30 @@ describe('Phone (maskitoTransform)', () => {
);
});
});

describe('non-strict', () => {
let options: MaskitoOptions = MASKITO_DEFAULT_OPTIONS;

beforeEach(() => {
options = maskitoPhoneOptionsGenerator({
metadata,
strict: false,
countryIsoCode: 'RU',
});
});

it('full number +7 code', () => {
expect(maskitoTransform('+79202800155', options)).toBe('+7 920 280-01-55');
});

it('full number 8 code', () => {
expect(maskitoTransform('89202800155', options)).toBe('+7 920 280-01-55');
});

it('full number with extra chars', () => {
expect(maskitoTransform('8 (920) 280-01-55', options)).toBe(
'+7 920 280-01-55',
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function cutPhoneByValidLength({
}): string {
const validationResult = validatePhoneNumberLength(phone, metadata);

if (validationResult === 'INVALID_LENGTH' || validationResult === 'TOO_LONG') {
if (validationResult === 'TOO_LONG') {
return cutPhoneByValidLength({
phone: phone.slice(0, phone.length - 1),
metadata,
Expand Down

0 comments on commit d88c05d

Please sign in to comment.