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

Fix Number Formatting Order in Customization #854

Merged
merged 1 commit into from
Sep 5, 2024
Merged
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
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
Loading