diff --git a/documentation/v5/docs/customization.md b/documentation/v5/docs/customization.md index 52f5ff42..00b96541 100644 --- a/documentation/v5/docs/customization.md +++ b/documentation/v5/docs/customization.md @@ -186,16 +186,19 @@ Another example for NumericFormat could be support for custom numerals. 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); + return _val.replace(/\d/g, ($1) => persianNumeral[Number($1)]); }; 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); @@ -203,13 +206,18 @@ function CustomNumeralNumericFormat(props) { 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; const newPersianChar = persianNumeral[Number(newChar)] ?? newChar; - return isCharSame || curPersianChar || newPersianChar; + return isCharSame || curPersianChar === newPersianChar; }; return ( diff --git a/example/src/index.js b/example/src/index.js index e8806f7a..81246103 100644 --- a/example/src/index.js +++ b/example/src/index.js @@ -10,30 +10,37 @@ 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); + return _val.replace(/\d/g, ($1) => persianNumeral[Number($1)]); }; - const _removeFormatting = (val, ...rest) => { - const _val = val.replace(new RegExp(persianNumeral.join('|'), 'g'), ($1) => - persianNumeral.indexOf($1), + const _removeFormatting = (val) => { + const _val = val.replace(new RegExp(persianNumeral.join("|"), "g"), ($1) => + persianNumeral.indexOf($1) ); - return removeFormatting(_val, ...rest); + 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; const newPersianChar = persianNumeral[Number(newChar)] ?? newChar; - return isCharSame || curPersianChar || newPersianChar; + return isCharSame || curPersianChar === newPersianChar; }; return (