Skip to content

Commit

Permalink
Merge pull request #854 from mhl98/master
Browse files Browse the repository at this point in the history
Fix Number Formatting Order in Customization
  • Loading branch information
s-yadav authored Sep 5, 2024
2 parents a1fab6d + 763c894 commit 69a5ed7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
18 changes: 13 additions & 5 deletions documentation/v5/docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,30 +186,38 @@ 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);
};

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 (
Expand Down
21 changes: 14 additions & 7 deletions example/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 69a5ed7

Please sign in to comment.