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

added nonEnglish number foramt, Persian, Arabic #874

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
added nonEnglish number foramt, Persian, Arabic
baravak committed Dec 19, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 6b98bc0771000bd56b629f173cfccaa285b5d76f
5 changes: 4 additions & 1 deletion src/number_format_base.tsx
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ import {
caretUnknownFormatBoundary,
getCaretPosInBoundary,
findChangedRangeFromCaretPositions,
enNumber,
} from './utils';

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

@@ -163,7 +165,7 @@ 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);

@@ -240,6 +242,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
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -86,6 +86,7 @@ type NumberFormatBase = {
getCaretBoundary?: (formattedValue: string) => boolean[];
isValidInputCharacter?: (character: string) => boolean;
isCharacterSame?: IsCharacterSame;
nonEnglishFormat?: boolean;
};

export type NumberFormatBaseProps<BaseType = InputAttributes> = NumberFormatProps<
22 changes: 22 additions & 0 deletions src/utils.tsx
Original file line number Diff line number Diff line change
@@ -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
}