Skip to content

Commit

Permalink
fix(intl-phone-input): change insert rule (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorokFront authored Feb 8, 2023
1 parent ab0e887 commit 0b62fb9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-students-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@alfalab/core-components-intl-phone-input": patch
---

fix(intl-phone-input): исправлена вставка в пустое поле номера без "+"
1 change: 1 addition & 0 deletions packages/intl-phone-input/src/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ export const IntlPhoneInput = forwardRef<HTMLInputElement, IntlPhoneInputProps>(
text,
selectionStart || 0,
selectionEnd || 0,
ruNumberPriority && countryIso2 === 'ru',
);
const targetCountry = getCountryByNumber(preparedNumber);
const maxPhoneLength =
Expand Down
13 changes: 9 additions & 4 deletions packages/intl-phone-input/src/utils/preparePasteData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ describe('preparePasteData', () => {
expect(preparePasteData('', '89491234567', 0, 0)).toEqual('+79491234567');
});

it('should return number with +7 when paste number doesn\'t start with "7" or "8" in empty field', () => {
expect(preparePasteData('', '6491234567', 0, 0)).toEqual('+76491234567');
expect(preparePasteData('', '9491234567', 0, 0)).toEqual('+79491234567');
it('should return number with + when paste number doesn\'t start with "7" or "8" in empty field', () => {
expect(preparePasteData('', '6491234567', 0, 0)).toEqual('+6491234567');
expect(preparePasteData('', '9491234567', 0, 0)).toEqual('+9491234567');
});

it('should return number with +7 when paste number doesn\'t start with "7" or "8" in empty field with ruNumberPriority', () => {
expect(preparePasteData('', '6491234567', 0, 0, true)).toEqual('+76491234567');
expect(preparePasteData('', '9491234567', 0, 0, true)).toEqual('+79491234567');
});

it('should return number when paste number start with "+" in empty field', () => {
expect(preparePasteData('', '+79491234567', 0, 0)).toEqual('+79491234567');
});

it('should return number when paste number with letters in empty field', () => {
expect(preparePasteData('', '123aaa456', 0, 0)).toEqual('+7123456');
expect(preparePasteData('', '123aaa456', 0, 0)).toEqual('+123456');
});

it('should return number when paste number in field with "+"', () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/intl-phone-input/src/utils/preparePasteData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function preparePasteData(
phoneFromBuffer: string,
selectionStart?: number,
selectionEnd?: number,
ruNumberPriority?: boolean,
) {
const trimNuber = phoneFromBuffer.trim();
const cutNumberWithPlus = trimNuber.replace(/[^+\d]/g, '');
Expand Down Expand Up @@ -43,9 +44,12 @@ export function preparePasteData(
// вставка номера начинающегося с "7" или "8" в пустое поле
} else if (isRuNumberInBuffer) {
resultNumber = `+7${cutNumber.substring(1)}`;
// вставка номера начинающегося НЕ с "7", "8", "+" в пустое поле
} else {
// вставка номера начинающегося НЕ с "7", "8", "+" в пустое поле с российским флагом
} else if (ruNumberPriority) {
resultNumber = `+7${cutNumber}`;
// вставка номера начинающегося НЕ с "7", "8", "+" в пустое поле без российского флага
} else {
resultNumber = `+${cutNumber}`;
}
}

Expand Down

0 comments on commit 0b62fb9

Please sign in to comment.