mirror of
https://github.com/itflow-org/itflow
synced 2026-09-17 04:05:12 +00:00
Bump intl-tel-input
This commit is contained in:
32
libs/intl-tel-input/js/format/caret.ts
Normal file
32
libs/intl-tel-input/js/format/caret.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
//* Iterate through the formattedValue until hit the right number of relevant chars.
|
||||
export const computeNewCaretPosition = (
|
||||
relevantChars: number,
|
||||
formattedValue: string,
|
||||
prevCaretPos: number,
|
||||
isDeleteForwards: boolean,
|
||||
): number => {
|
||||
//* If the first char is a formatting char, and they backspace delete it:
|
||||
//* Cursor should stay at the start (pos 0), rather than stick to the first digit (pos 1).
|
||||
if (prevCaretPos === 0 && !isDeleteForwards) {
|
||||
return 0;
|
||||
}
|
||||
let relevantCharCount = 0;
|
||||
for (let i = 0; i < formattedValue.length; i++) {
|
||||
//* Count this as a relevant char if it's a + or a digit.
|
||||
if (/[+0-9]/.test(formattedValue[i])) {
|
||||
relevantCharCount++;
|
||||
}
|
||||
|
||||
//* Normal case: stop when you hit the right number of relevant chars
|
||||
//* (cursor will be just after the final relevant char).
|
||||
if (relevantCharCount === relevantChars && !isDeleteForwards) {
|
||||
return i + 1;
|
||||
}
|
||||
//* Special case: delete forwards (fn + delete on a mac):
|
||||
//* Wait until hit one extra relevant char, and put the cursor just before it (after any formatting chars).
|
||||
if (isDeleteForwards && relevantCharCount === relevantChars + 1) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return formattedValue.length;
|
||||
};
|
||||
48
libs/intl-tel-input/js/format/formatting.ts
Normal file
48
libs/intl-tel-input/js/format/formatting.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { ItiUtils, SelectedCountry } from "../types/public-api.js";
|
||||
|
||||
//* Remove the dial code if separateDialCode is enabled
|
||||
export const stripSeparateDialCode = (
|
||||
fullNumber: string,
|
||||
hasValidDialCode: boolean,
|
||||
separateDialCode: boolean,
|
||||
selectedCountry: SelectedCountry,
|
||||
): string => {
|
||||
if (!separateDialCode || !hasValidDialCode) {
|
||||
return fullNumber;
|
||||
}
|
||||
|
||||
//* In case getDialCode returned an area code as well.
|
||||
const dialCode = `+${selectedCountry!.dialCode}`;
|
||||
//* a lot of numbers will have a space separating the dial code and the main number, and
|
||||
//* some NANP numbers will have a hyphen e.g. +1 684-733-1234 - in both cases we want to get rid of it.
|
||||
//* NOTE: Don't just trim all non-numerics as may want to preserve an open parenthesis etc.
|
||||
const start =
|
||||
fullNumber[dialCode.length] === " " || fullNumber[dialCode.length] === "-"
|
||||
? dialCode.length + 1
|
||||
: dialCode.length;
|
||||
return fullNumber.substring(start);
|
||||
};
|
||||
|
||||
//* Format the number as the user types.
|
||||
export const formatNumberAsYouType = (
|
||||
fullNumber: string,
|
||||
telInputValue: string,
|
||||
utils: ItiUtils | undefined,
|
||||
selectedCountry: SelectedCountry,
|
||||
separateDialCode: boolean,
|
||||
): string => {
|
||||
const result = utils
|
||||
? utils.formatNumberAsYouType(fullNumber, selectedCountry?.iso2)
|
||||
: fullNumber;
|
||||
//* If separateDialCode and they haven't (re)typed the dial code in the input as well, then remove the dial code.
|
||||
const dialCode = selectedCountry?.dialCode;
|
||||
if (
|
||||
separateDialCode &&
|
||||
telInputValue.charAt(0) !== "+" &&
|
||||
result.includes(`+${dialCode}`)
|
||||
) {
|
||||
const afterDialCode = result.split(`+${dialCode}`)[1] || "";
|
||||
return afterDialCode.trim();
|
||||
}
|
||||
return result;
|
||||
};
|
||||
Reference in New Issue
Block a user