Skip to content
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
14 changes: 4 additions & 10 deletions example/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import { cardExpiry } from '../../custom_formatters/card_expiry';
const persianNumeral = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];

function CustomNumeralNumericFormat(props) {
const { format, removeFormatting, isCharacterSame, ...rest } =
useNumericFormat(props);
const { format, removeFormatting, isCharacterSame, ...rest } = useNumericFormat(props);

const _format = (val) => {
const _val = format(val);
Expand All @@ -20,21 +19,16 @@ function CustomNumeralNumericFormat(props) {
};

const _removeFormatting = (val) => {
const _val = val.replace(new RegExp(persianNumeral.join("|"), "g"), ($1) =>
persianNumeral.indexOf($1)
const _val = val.replace(new RegExp(persianNumeral.join('|'), 'g'), ($1) =>
persianNumeral.indexOf($1),
);

return removeFormatting(_val);
};

const _isCharacterSame = (compareMeta) => {
const isCharSame = isCharacterSame(compareMeta);
const {
formattedValue,
currentValue,
formattedValueIndex,
currentValueIndex,
} = compareMeta;
const { formattedValue, currentValue, formattedValueIndex, currentValueIndex } = compareMeta;
const curChar = currentValue[currentValueIndex];
const newChar = formattedValue[formattedValueIndex];
const curPersianChar = persianNumeral[Number(curChar)] ?? curChar;
Expand Down
8 changes: 7 additions & 1 deletion src/number_format_base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
caretUnknownFormatBoundary,
getCaretPosInBoundary,
findChangedRangeFromCaretPositions,
enNumber,
} from './utils';

function defaultRemoveFormatting(value: string) {
Expand Down Expand Up @@ -52,6 +53,7 @@ export default function NumberFormatBase<BaseType = InputAttributes>(
getCaretBoundary = caretUnknownFormatBoundary,
isValidInputCharacter = charIsNumber,
isCharacterSame,
nonEnglishFormat,
...otherProps
} = props;

Expand Down Expand Up @@ -163,7 +165,10 @@ export default function NumberFormatBase<BaseType = InputAttributes>(
let caretPos;

if (input) {
const inputValue = params.inputValue || input.value;
const inputValue =
nonEnglishFormat === true
? enNumber(params.inputValue || input.value)
: params.inputValue || input.value;

const currentCaretPosition = geInputCaretPosition(input);

Expand Down Expand Up @@ -240,6 +245,7 @@ export default function NumberFormatBase<BaseType = InputAttributes>(
| React.KeyboardEvent<HTMLInputElement>,
source: SourceType,
) => {
inputValue = nonEnglishFormat === true ? enNumber(inputValue) : inputValue;
const input = event.target as HTMLInputElement;

const changeRange = caretPositionBeforeChange.current
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type NumberFormatBase = {
getCaretBoundary?: (formattedValue: string) => boolean[];
isValidInputCharacter?: (character: string) => boolean;
isCharacterSame?: IsCharacterSame;
nonEnglishFormat?: boolean;
};

export type NumberFormatBaseProps<BaseType = InputAttributes> = NumberFormatProps<
Expand Down
22 changes: 22 additions & 0 deletions src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,25 @@ export function useInternalValues(

return [values, _onValueChange];
}

function convertPatterns(nonEglish: string[], text: string): string {
const regExp = new RegExp(`[${nonEglish.join('')}]`, 'gi');
return text.replace(regExp, (char: string): string => {
const index = nonEglish.indexOf(char);
return index.toString();
});
}
function arToEnNumber(text: string): string {
return convertPatterns('٠١٢٣٤٥٦٧٨٩'.split(''), text);
}
function faToEnNumber(text: string): string {
return convertPatterns('۰۱۲۳۴۵۶۷۸۹'.split(''), text);
}

export function enNumber(text: string): string {
const langs = [faToEnNumber, arToEnNumber];
langs.forEach((lang) => {
text = lang(text);
});
return text;
}