currency-fomatter 1.3.2 → 1.3.3
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 +201 -35
- package/dist/cjs/components/currency/index.d.ts +34 -107
- package/dist/cjs/components/currency/index.js +458 -505
- package/dist/cjs/components/currency/index.js.map +1 -1
- package/dist/cjs/components/currency/utils.d.ts +26 -21
- package/dist/cjs/components/currency/utils.js +73 -71
- package/dist/cjs/components/currency/utils.js.map +1 -1
- package/dist/cjs/components/test/index.d.ts +3 -6
- package/dist/cjs/components/test/index.js +121 -15
- package/dist/cjs/components/test/index.js.map +1 -1
- package/dist/cjs/index.d.ts +2 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/components/currency/index.d.ts +34 -107
- package/dist/esm/components/currency/index.js +461 -508
- package/dist/esm/components/currency/index.js.map +1 -1
- package/dist/esm/components/currency/utils.d.ts +26 -21
- package/dist/esm/components/currency/utils.js +71 -67
- package/dist/esm/components/currency/utils.js.map +1 -1
- package/dist/esm/components/test/index.d.ts +3 -6
- package/dist/esm/components/test/index.js +123 -16
- package/dist/esm/components/test/index.js.map +1 -1
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -1,46 +1,212 @@
|
|
|
1
1
|
# currency-fomatter
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
import { CurrencyFormat } from "currency-fomatter"
|
|
3
|
+
A React component for formatting currency, phone numbers, credit cards, and other numeric inputs with full TypeScript support.
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
## Screenshots
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install currency-fomatter
|
|
15
|
+
# or
|
|
16
|
+
yarn add currency-fomatter
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Features
|
|
20
|
+
|
|
21
|
+
- Currency formatting with thousand separators
|
|
22
|
+
- Phone number and credit card masking
|
|
23
|
+
- Custom format patterns
|
|
24
|
+
- Support for multiple number systems (US, Indian, etc.)
|
|
25
|
+
- Prefix and suffix support
|
|
26
|
+
- Decimal scale control
|
|
27
|
+
- Negative number support
|
|
28
|
+
- Custom input component support
|
|
29
|
+
- Full TypeScript support
|
|
30
|
+
|
|
31
|
+
## Basic Usage
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { CurrencyFormat } from "currency-fomatter";
|
|
35
|
+
|
|
36
|
+
function App() {
|
|
37
|
+
return (
|
|
38
|
+
<CurrencyFormat
|
|
39
|
+
value="1234567.89"
|
|
40
|
+
thousandSeparator=","
|
|
41
|
+
decimalSeparator="."
|
|
42
|
+
prefix="$"
|
|
43
|
+
decimalScale={2}
|
|
44
|
+
fixedDecimalScale
|
|
45
|
+
onValueChange={(values) => {
|
|
46
|
+
console.log(values.formattedValue); // "$1,234,567.89"
|
|
47
|
+
console.log(values.value); // "1234567.89"
|
|
48
|
+
console.log(values.floatValue); // 1234567.89
|
|
49
|
+
}}
|
|
50
|
+
/>
|
|
51
|
+
);
|
|
11
52
|
}
|
|
12
53
|
```
|
|
13
54
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
55
|
+
## Examples
|
|
56
|
+
|
|
57
|
+
### Currency Format
|
|
58
|
+
|
|
59
|
+
```tsx
|
|
60
|
+
<CurrencyFormat
|
|
61
|
+
value={1234.56}
|
|
62
|
+
thousandSeparator=","
|
|
63
|
+
prefix="$"
|
|
64
|
+
decimalScale={2}
|
|
65
|
+
fixedDecimalScale
|
|
66
|
+
/>
|
|
67
|
+
// Output: $1,234.56
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Phone Number Format
|
|
71
|
+
|
|
72
|
+
```tsx
|
|
73
|
+
<CurrencyFormat
|
|
74
|
+
format="+1 (###) ###-####"
|
|
75
|
+
mask="_"
|
|
76
|
+
placeholder="+1 (___) ___-____"
|
|
77
|
+
/>
|
|
78
|
+
// Output: +1 (555) 123-4567
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Credit Card Format
|
|
82
|
+
|
|
83
|
+
```tsx
|
|
84
|
+
<CurrencyFormat
|
|
85
|
+
format="#### #### #### ####"
|
|
86
|
+
mask="_"
|
|
87
|
+
/>
|
|
88
|
+
// Output: 4111 1111 1111 1111
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Percentage with Validation
|
|
92
|
+
|
|
93
|
+
```tsx
|
|
94
|
+
<CurrencyFormat
|
|
95
|
+
value="75.5"
|
|
96
|
+
suffix="%"
|
|
97
|
+
decimalScale={2}
|
|
98
|
+
isAllowed={(values) => values.floatValue <= 100}
|
|
99
|
+
/>
|
|
100
|
+
// Output: 75.50%
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Indian Number System
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
<CurrencyFormat
|
|
107
|
+
value="1234567"
|
|
108
|
+
thousandSeparator=","
|
|
109
|
+
thousandSpacing="2s"
|
|
110
|
+
prefix="₹"
|
|
111
|
+
/>
|
|
112
|
+
// Output: ₹12,34,567
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Display as Text (Read-only)
|
|
116
|
+
|
|
117
|
+
```tsx
|
|
118
|
+
<CurrencyFormat
|
|
119
|
+
value={9999.99}
|
|
120
|
+
displayType="text"
|
|
121
|
+
thousandSeparator=","
|
|
122
|
+
prefix="$"
|
|
123
|
+
/>
|
|
124
|
+
// Output: <span>$9,999.99</span>
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Custom Input Component
|
|
128
|
+
|
|
129
|
+
```tsx
|
|
130
|
+
const CustomInput = (props) => (
|
|
131
|
+
<input {...props} className="custom-input" />
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
<CurrencyFormat
|
|
135
|
+
value="1000"
|
|
136
|
+
customInput={CustomInput}
|
|
137
|
+
thousandSeparator=","
|
|
138
|
+
prefix="$"
|
|
139
|
+
/>
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Props
|
|
143
|
+
|
|
144
|
+
| Prop | Type | Default | Description |
|
|
145
|
+
|------|------|---------|-------------|
|
|
146
|
+
| `value` | `string \| number` | - | Input value |
|
|
147
|
+
| `format` | `string \| Function` | - | Format pattern (e.g., `"+1 (###) ###-####"`) or custom format function |
|
|
148
|
+
| `decimalScale` | `number` | - | Maximum decimal places |
|
|
149
|
+
| `decimalSeparator` | `string` | `"."` | Decimal separator character |
|
|
150
|
+
| `thousandSeparator` | `string \| boolean` | `","` | Thousand separator (`true` = `","`) |
|
|
151
|
+
| `thousandSpacing` | `"2" \| "2s" \| "3" \| "4"` | `"3"` | Thousand grouping pattern |
|
|
152
|
+
| `mask` | `string \| string[]` | `" "` | Mask character for empty format positions |
|
|
153
|
+
| `prefix` | `string` | `""` | Text before the number (e.g., `"$"`) |
|
|
154
|
+
| `suffix` | `string` | `""` | Text after the number (e.g., `"%"`) |
|
|
155
|
+
| `allowNegative` | `boolean` | `true` | Allow negative values |
|
|
156
|
+
| `fixedDecimalScale` | `boolean` | `false` | Always show decimal places |
|
|
157
|
+
| `isNumericString` | `boolean` | `false` | Treat value as numeric string |
|
|
158
|
+
| `isAllowed` | `(values: ValueObject) => boolean` | - | Custom validation function |
|
|
159
|
+
| `onValueChange` | `(values: ValueObject) => void` | - | Callback when value changes |
|
|
160
|
+
| `type` | `"text" \| "tel"` | `"text"` | Input type |
|
|
161
|
+
| `displayType` | `"input" \| "text"` | `"input"` | Render as input or text |
|
|
162
|
+
| `customInput` | `React.ComponentType` | - | Custom input component |
|
|
163
|
+
| `renderText` | `(value, props) => ReactNode` | - | Custom text renderer |
|
|
164
|
+
|
|
165
|
+
### Thousand Spacing Options
|
|
166
|
+
|
|
167
|
+
| Value | Description | Example |
|
|
168
|
+
|-------|-------------|---------|
|
|
169
|
+
| `"3"` | Groups of 3 (US/EU) | `1,234,567` |
|
|
170
|
+
| `"2"` | Groups of 2 | `12,34,56,7` |
|
|
171
|
+
| `"2s"` | Indian system (Lakhs/Crores) | `12,34,567` |
|
|
172
|
+
| `"4"` | Groups of 4 | `123,4567` |
|
|
173
|
+
|
|
174
|
+
## TypeScript
|
|
175
|
+
|
|
176
|
+
The package includes full TypeScript support with exported types:
|
|
177
|
+
|
|
178
|
+
```tsx
|
|
179
|
+
import CurrencyFormat, {
|
|
180
|
+
CurrencyFormatProps,
|
|
181
|
+
ValueObject,
|
|
182
|
+
ThousandSpacing,
|
|
183
|
+
FormatFunction,
|
|
184
|
+
IsAllowedFunction,
|
|
185
|
+
OnValueChangeFunction,
|
|
186
|
+
} from "currency-fomatter";
|
|
187
|
+
|
|
188
|
+
// ValueObject type
|
|
189
|
+
interface ValueObject {
|
|
190
|
+
formattedValue: string; // Formatted display value
|
|
191
|
+
value: string; // Unformatted numeric string
|
|
192
|
+
floatValue: number; // Parsed float value
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Event Handlers
|
|
197
|
+
|
|
198
|
+
All standard input event handlers are supported:
|
|
43
199
|
|
|
200
|
+
```tsx
|
|
201
|
+
<CurrencyFormat
|
|
202
|
+
onChange={(e) => console.log("onChange", e)}
|
|
203
|
+
onKeyDown={(e) => console.log("onKeyDown", e)}
|
|
204
|
+
onMouseUp={(e) => console.log("onMouseUp", e)}
|
|
205
|
+
onFocus={(e) => console.log("onFocus", e)}
|
|
206
|
+
onBlur={(e) => console.log("onBlur", e)}
|
|
207
|
+
/>
|
|
44
208
|
```
|
|
45
209
|
|
|
210
|
+
## License
|
|
46
211
|
|
|
212
|
+
MIT
|
|
@@ -1,109 +1,36 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
type
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
declare class CurrencyFormat extends Component<MyProps, MyState> {
|
|
35
|
-
state: {
|
|
36
|
-
value?: string;
|
|
37
|
-
numAsString?: any;
|
|
38
|
-
};
|
|
39
|
-
static defaultProps: Partial<MyProps>;
|
|
40
|
-
constructor(props: MyProps);
|
|
41
|
-
componentDidUpdate(prevProps: any): void;
|
|
42
|
-
updateValueIfRequired(prevProps: any): void;
|
|
43
|
-
/** Misc methods **/
|
|
44
|
-
getFloatString(num?: string): string;
|
|
45
|
-
getNumberRegex(g: boolean, ignoreDecimalSeparator?: boolean): RegExp;
|
|
46
|
-
getSeparators(): {
|
|
47
|
-
decimalSeparator: string;
|
|
48
|
-
thousandSeparator: string | false;
|
|
49
|
-
thousandSpacing: "2" | "2s" | "3" | "4";
|
|
50
|
-
};
|
|
51
|
-
getMaskAtIndex(index: number): string;
|
|
52
|
-
validateProps(): void;
|
|
53
|
-
splitDecimal(numStr: string): {
|
|
54
|
-
beforeDecimal: string;
|
|
55
|
-
afterDecimal: string;
|
|
56
|
-
hasNagation: boolean;
|
|
57
|
-
addNegation: boolean;
|
|
58
|
-
};
|
|
59
|
-
/** Misc methods end **/
|
|
60
|
-
/** caret specific methods **/
|
|
61
|
-
setPatchedCaretPosition(el: any, caretPos: number, currentValue: string): void;
|
|
62
|
-
correctCaretPosition(value: string, caretPos: number, direction?: string): any;
|
|
63
|
-
getCaretPosition(inputValue: string, formattedValue: string, caretPos: number): any;
|
|
64
|
-
/** caret specific methods ends **/
|
|
65
|
-
/** methods to remove formattting **/
|
|
66
|
-
removePrefixAndSuffix(val: string): string;
|
|
67
|
-
removePatternFormatting(val: string): string;
|
|
68
|
-
removeFormatting(val: string): string;
|
|
69
|
-
/** methods to remove formattting end **/
|
|
70
|
-
/*** format specific methods start ***/
|
|
71
|
-
/**
|
|
72
|
-
* Format when # based string is provided
|
|
73
|
-
* @param {string} numStr Numeric String
|
|
74
|
-
* @return {string} formatted Value
|
|
75
|
-
*/
|
|
76
|
-
formatWithPattern(numStr: string): string;
|
|
77
|
-
/**
|
|
78
|
-
* Format the given string according to thousand separator and thousand spacing
|
|
79
|
-
* @param {*} beforeDecimal
|
|
80
|
-
* @param {*} thousandSeparator
|
|
81
|
-
* @param {*} thousandSpacing
|
|
82
|
-
*/
|
|
83
|
-
formatThousand(beforeDecimal: any, thousandSeparator: string | boolean, thousandSpacing?: any): any;
|
|
84
|
-
/**
|
|
85
|
-
* @param {string} numStr Numeric string/floatString] It always have decimalSeparator as .
|
|
86
|
-
* @return {string} formatted Value
|
|
87
|
-
*/
|
|
88
|
-
formatAsNumber(numStr: string): string;
|
|
89
|
-
formatNumString(value?: string): string;
|
|
90
|
-
formatValueProp(): string;
|
|
91
|
-
formatNegation(value?: string): string;
|
|
92
|
-
formatInput(value?: string): string;
|
|
93
|
-
/*** format specific methods end ***/
|
|
94
|
-
isCharacterAFormat(caretPos: number, value: string): boolean;
|
|
95
|
-
checkIfFormatGotDeleted(start: number, end: number, value: string): boolean;
|
|
96
|
-
/**
|
|
97
|
-
* This will check if any formatting got removed by the delete or backspace and reset the value
|
|
98
|
-
* It will also work as fallback if android chome keyDown handler does not work
|
|
99
|
-
**/
|
|
100
|
-
correctInputValue(caretPos: number, lastValue: string, value: string): string;
|
|
101
|
-
onChange(e: React.FormEvent<HTMLInputElement>): void;
|
|
102
|
-
onBlur(e: React.FormEvent<HTMLInputElement>): void;
|
|
103
|
-
onKeyDown(e: any): void;
|
|
104
|
-
/** required to handle the caret position when click anywhere within the input **/
|
|
105
|
-
onMouseUp(e: React.FormEvent<HTMLInputElement>): void;
|
|
106
|
-
onFocus(e: React.FormEvent<HTMLInputElement>): void;
|
|
107
|
-
render(): string | React.JSX.Element | null;
|
|
1
|
+
import { ComponentType, InputHTMLAttributes, FocusEvent, KeyboardEvent, ChangeEvent, MouseEvent, ReactNode } from "react";
|
|
2
|
+
import { ThousandSpacing, ValueObject } from "./utils";
|
|
3
|
+
export type { ThousandSpacing, ValueObject } from "./utils";
|
|
4
|
+
export type FormatFunction = (value: string) => string;
|
|
5
|
+
export type RemoveFormattingFunction = (value: string) => string;
|
|
6
|
+
export type IsAllowedFunction = (values: ValueObject) => boolean;
|
|
7
|
+
export type OnValueChangeFunction = (values: ValueObject) => void;
|
|
8
|
+
export type RenderTextFunction = (value: string, otherProps: Record<string, unknown>) => ReactNode;
|
|
9
|
+
export interface CurrencyFormatProps extends Omit<InputHTMLAttributes<HTMLInputElement>, "value" | "onChange" | "onKeyDown" | "onMouseUp" | "onFocus" | "onBlur"> {
|
|
10
|
+
value?: string | number;
|
|
11
|
+
format?: string | FormatFunction;
|
|
12
|
+
decimalScale?: number;
|
|
13
|
+
decimalSeparator?: string;
|
|
14
|
+
thousandSpacing?: ThousandSpacing;
|
|
15
|
+
thousandSeparator?: string | boolean;
|
|
16
|
+
mask?: string | string[];
|
|
17
|
+
allowNegative?: boolean;
|
|
18
|
+
prefix?: string;
|
|
19
|
+
suffix?: string;
|
|
20
|
+
removeFormatting?: RemoveFormattingFunction;
|
|
21
|
+
fixedDecimalScale?: boolean;
|
|
22
|
+
isNumericString?: boolean;
|
|
23
|
+
isAllowed?: IsAllowedFunction;
|
|
24
|
+
onValueChange?: OnValueChangeFunction;
|
|
25
|
+
onChange?: (e: ChangeEvent<HTMLInputElement>) => void;
|
|
26
|
+
onKeyDown?: (e: KeyboardEvent<HTMLInputElement>) => void;
|
|
27
|
+
onMouseUp?: (e: MouseEvent<HTMLInputElement>) => void;
|
|
28
|
+
onFocus?: (e: FocusEvent<HTMLInputElement>) => void;
|
|
29
|
+
onBlur?: (e: FocusEvent<HTMLInputElement>) => void;
|
|
30
|
+
type?: "text" | "tel";
|
|
31
|
+
displayType?: "input" | "text";
|
|
32
|
+
customInput?: ComponentType<InputHTMLAttributes<HTMLInputElement>>;
|
|
33
|
+
renderText?: RenderTextFunction;
|
|
108
34
|
}
|
|
35
|
+
declare function CurrencyFormat(props: CurrencyFormatProps): JSX.Element | null;
|
|
109
36
|
export default CurrencyFormat;
|