Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(usePhoneInput): add an option to allow the longer numbers than mask #224

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/hooks/usePhoneInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ export interface UsePhoneInputConfig {
*/
disableFormatting?: boolean;

/**
* @description Allow phone value to be longer than the mask.
* @default false
*/
enableLongNumbers?: boolean;

/**
* @description Callback that calls on phone change
* @param data - New phone data.
Expand Down Expand Up @@ -134,6 +140,7 @@ export const defaultConfig: Required<
forceDialCode: false,
disableDialCodeAndPrefix: false,
disableFormatting: false,
enableLongNumbers: false,
countries: defaultCountries,
preferredCountries: [],
};
Expand All @@ -151,6 +158,7 @@ export const usePhoneInput = ({
forceDialCode: forceDialCodeConfig = defaultConfig.forceDialCode,
disableDialCodeAndPrefix = defaultConfig.disableDialCodeAndPrefix,
disableFormatting = defaultConfig.disableFormatting,
enableLongNumbers = defaultConfig.enableLongNumbers,
onChange,
inputRef: inputRefProp,
}: UsePhoneInputConfig) => {
Expand All @@ -166,6 +174,7 @@ export const usePhoneInput = ({
defaultMask,
countryGuessingEnabled,
disableFormatting,
enableLongNumbers,
};

const ref = useRef<HTMLInputElement | null>(null);
Expand Down
13 changes: 13 additions & 0 deletions src/utils/common/__tests__/applyMask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,17 @@ describe('applyMask', () => {
'(1234',
);
});

test('should handle enableLongNumbers option', () => {
const maskConfig = {
value: '123456789',
mask: '(....) .. ..',
maskSymbol: '.',
};

expect(applyMask(maskConfig)).toBe('(1234) 56 78');
expect(applyMask({ ...maskConfig, enableLongNumbers: true })).toBe(
'123456789',
);
});
});
13 changes: 13 additions & 0 deletions src/utils/common/applyMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ interface ApplyMaskArgs {
* if false -> "(1234) "
*/
trimNonMaskCharsLeftover?: boolean;

/**
* @description Ignores masking for numbers longer than the mask
* @example
* value: "123456789"
* mask: "(....) ...."
* if true -> "123456789"
* if false -> "(1234) 5678"
*/
enableLongNumbers?: boolean;
}

export const applyMask = ({
Expand All @@ -30,8 +40,11 @@ export const applyMask = ({
maskSymbol,
offset = 0,
trimNonMaskCharsLeftover = false,
enableLongNumbers = false,
}: ApplyMaskArgs): string => {
if (value.length < offset) return value;
if (enableLongNumbers && value.length > mask.split('.').length - 1)
return value;

const savedValuePart = value.slice(0, offset);
const valueToMask = value.slice(offset);
Expand Down
1 change: 1 addition & 0 deletions src/utils/handlePhoneChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface PhoneFormattingConfig {
defaultMask: string;
countryGuessingEnabled: boolean;
disableFormatting: boolean;
enableLongNumbers: boolean;
}

interface HandlePhoneChangeProps extends PhoneFormattingConfig {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/handleUserInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const handleUserInput = (
countryGuessingEnabled,
defaultMask,
disableFormatting,
enableLongNumbers,
countries,
}: HandleUserInputOptions,
): {
Expand Down Expand Up @@ -130,6 +131,7 @@ export const handleUserInput = (
forceDialCode,
disableDialCodeAndPrefix,
disableFormatting,
enableLongNumbers,
defaultMask,
});

Expand Down
5 changes: 5 additions & 0 deletions src/utils/phoneUtils/formatPhone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export interface FormatPhoneConfig {
* Trim all non-digit values from the end of the result
*/
trimNonDigitsEnd?: boolean;
/**
* allow to handle long numbers in the input value.
*/
enableLongNumbers?: boolean;
}

export const formatPhone = (
Expand Down Expand Up @@ -137,6 +141,7 @@ export const formatPhone = (
trimNonMaskCharsLeftover:
config.trimNonDigitsEnd ||
(config.disableDialCodeAndPrefix && phoneRightSide.length === 0),
enableLongNumbers: config.enableLongNumbers,
});

if (config.disableDialCodeAndPrefix) {
Expand Down