persian-number-input 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export interface PersianNumberInputProps extends React.InputHTMLAttributes<HTMLInputElement> {
|
|
3
|
+
initialValue?: string;
|
|
4
|
+
separatorCount?: number;
|
|
5
|
+
separatorChar?: string;
|
|
6
|
+
lang?: 'fa' | 'in' | 'en';
|
|
7
|
+
onChangeValue?: (englishNumber: string) => void;
|
|
8
|
+
}
|
|
9
|
+
declare const PersianNumberInput: React.FC<PersianNumberInputProps>;
|
|
10
|
+
export default PersianNumberInput;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import React, { useCallback } from 'react';
|
|
13
|
+
import { toLocalizedDigits, groupDigits, convertToEnglishDigits } from '../utils/digitUtils';
|
|
14
|
+
const PersianNumberInput = (_a) => {
|
|
15
|
+
var { initialValue = '', separatorCount = 0, separatorChar = ',', lang = 'fa', onChangeValue } = _a, rest = __rest(_a, ["initialValue", "separatorCount", "separatorChar", "lang", "onChangeValue"]);
|
|
16
|
+
const [value, setValue] = React.useState(() => convertToEnglishDigits(initialValue).replace(/\D/g, ''));
|
|
17
|
+
const handleChange = useCallback((e) => {
|
|
18
|
+
const input = convertToEnglishDigits(e.target.value).replace(/\D/g, '');
|
|
19
|
+
setValue(input);
|
|
20
|
+
if (onChangeValue)
|
|
21
|
+
onChangeValue(input);
|
|
22
|
+
}, [onChangeValue]);
|
|
23
|
+
const formattedValue = groupDigits(value, separatorCount, separatorChar);
|
|
24
|
+
const displayValue = lang === 'en' ? formattedValue : toLocalizedDigits(formattedValue, lang);
|
|
25
|
+
return React.createElement("input", Object.assign({}, rest, { value: displayValue, onChange: handleChange }));
|
|
26
|
+
};
|
|
27
|
+
export default PersianNumberInput;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as PersianNumberInput } from "./components/PersianNumberInput";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as PersianNumberInput } from "./components/PersianNumberInput";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const digitsMap: {
|
|
2
|
+
[key: string]: string[];
|
|
3
|
+
};
|
|
4
|
+
export declare const toLocalizedDigits: (numStr: string, locale: "fa" | "in") => string;
|
|
5
|
+
export declare const groupDigits: (numStr: string, separatorCount: number, separatorChar?: string) => string;
|
|
6
|
+
export declare const convertToEnglishDigits: (str: string) => string;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export const digitsMap = {
|
|
2
|
+
fa: ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"],
|
|
3
|
+
in: ["०", "१", "२", "३", "४", "५", "६", "७", "८", "९"],
|
|
4
|
+
};
|
|
5
|
+
export const toLocalizedDigits = (numStr, locale) => {
|
|
6
|
+
const digits = digitsMap[locale];
|
|
7
|
+
return numStr.replace(/\d/g, (digit) => digits[parseInt(digit)]);
|
|
8
|
+
};
|
|
9
|
+
export const groupDigits = (numStr, separatorCount, separatorChar = ",") => {
|
|
10
|
+
if (separatorCount && separatorCount > 0) {
|
|
11
|
+
return numStr.replace(new RegExp(`\\B(?=(\\d{${separatorCount}})+(?!\\d))`, "g"), separatorChar);
|
|
12
|
+
}
|
|
13
|
+
return numStr;
|
|
14
|
+
};
|
|
15
|
+
export const convertToEnglishDigits = (str) => {
|
|
16
|
+
const map = {
|
|
17
|
+
"۰": "0",
|
|
18
|
+
"۱": "1",
|
|
19
|
+
"۲": "2",
|
|
20
|
+
"۳": "3",
|
|
21
|
+
"۴": "4",
|
|
22
|
+
"۵": "5",
|
|
23
|
+
"۶": "6",
|
|
24
|
+
"۷": "7",
|
|
25
|
+
"۸": "8",
|
|
26
|
+
"۹": "9",
|
|
27
|
+
"०": "0",
|
|
28
|
+
"१": "1",
|
|
29
|
+
"२": "2",
|
|
30
|
+
"३": "3",
|
|
31
|
+
"४": "4",
|
|
32
|
+
"५": "5",
|
|
33
|
+
"६": "6",
|
|
34
|
+
"७": "7",
|
|
35
|
+
"८": "8",
|
|
36
|
+
"९": "9",
|
|
37
|
+
};
|
|
38
|
+
return str.replace(/[۰-۹०-९]/g, (char) => map[char]);
|
|
39
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "persian-number-input",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "React component for Persian, Indic, or English localized number input with customizable digit grouping",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"prepublishOnly": "npm run build",
|
|
13
|
+
"test": "jest"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"react",
|
|
17
|
+
"persian",
|
|
18
|
+
"indic",
|
|
19
|
+
"number",
|
|
20
|
+
"input",
|
|
21
|
+
"localization",
|
|
22
|
+
"digit-grouping"
|
|
23
|
+
],
|
|
24
|
+
"author": "Your Name",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/yourusername/persian-number-input.git"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/yourusername/persian-number-input/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/yourusername/persian-number-input#readme",
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": "^16.8 || ^17 || ^18 || ^19"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/react": "^19.0.0",
|
|
39
|
+
"@types/react-dom": "^19.0.0",
|
|
40
|
+
"typescript": "^5.0.0",
|
|
41
|
+
"jest": "^29.0.0",
|
|
42
|
+
"@types/jest": "^29.0.0",
|
|
43
|
+
"ts-jest": "^29.0.0"
|
|
44
|
+
}
|
|
45
|
+
}
|