currency-fomatter 1.3.3 → 1.4.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 +357 -0
- package/dist/cjs/components/currency/hooks.d.ts +52 -0
- package/dist/cjs/components/currency/hooks.js +130 -0
- package/dist/cjs/components/currency/hooks.js.map +1 -0
- package/dist/cjs/components/currency/index.d.ts +14 -4
- package/dist/cjs/components/currency/index.js +59 -8
- package/dist/cjs/components/currency/index.js.map +1 -1
- package/dist/cjs/components/currency/locales.d.ts +55 -0
- package/dist/cjs/components/currency/locales.js +382 -0
- package/dist/cjs/components/currency/locales.js.map +1 -0
- package/dist/cjs/components/currency/utils.d.ts +83 -0
- package/dist/cjs/components/currency/utils.js +273 -1
- package/dist/cjs/components/currency/utils.js.map +1 -1
- package/dist/cjs/index.d.ts +4 -3
- package/dist/cjs/index.js +19 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/components/currency/hooks.d.ts +52 -0
- package/dist/esm/components/currency/hooks.js +126 -0
- package/dist/esm/components/currency/hooks.js.map +1 -0
- package/dist/esm/components/currency/index.d.ts +14 -4
- package/dist/esm/components/currency/index.js +45 -9
- package/dist/esm/components/currency/index.js.map +1 -1
- package/dist/esm/components/currency/locales.d.ts +55 -0
- package/dist/esm/components/currency/locales.js +370 -0
- package/dist/esm/components/currency/locales.js.map +1 -0
- package/dist/esm/components/currency/utils.d.ts +83 -0
- package/dist/esm/components/currency/utils.js +268 -0
- package/dist/esm/components/currency/utils.js.map +1 -1
- package/dist/esm/index.d.ts +4 -3
- package/dist/esm/index.js +13 -2
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,6 +27,11 @@ yarn add currency-fomatter
|
|
|
27
27
|
- Negative number support
|
|
28
28
|
- Custom input component support
|
|
29
29
|
- Full TypeScript support
|
|
30
|
+
- **Standalone utility functions** (`formatCurrency`, `parseCurrency`)
|
|
31
|
+
- **Compact format** (`formatCompact` - 1K, 1M, 1B)
|
|
32
|
+
- **Locale presets** (en-US, vi-VN, de-DE, ja-JP, etc.)
|
|
33
|
+
- **React hook** (`useCurrencyFormat`) for easy state management
|
|
34
|
+
- **ref forwarding** for form library integration
|
|
30
35
|
|
|
31
36
|
## Basic Usage
|
|
32
37
|
|
|
@@ -161,6 +166,8 @@ const CustomInput = (props) => (
|
|
|
161
166
|
| `displayType` | `"input" \| "text"` | `"input"` | Render as input or text |
|
|
162
167
|
| `customInput` | `React.ComponentType` | - | Custom input component |
|
|
163
168
|
| `renderText` | `(value, props) => ReactNode` | - | Custom text renderer |
|
|
169
|
+
| `name` | `string` | - | Field name (included in ValueObject) |
|
|
170
|
+
| `getInputRef` | `(el: HTMLInputElement) => void` | - | Callback to get input element ref |
|
|
164
171
|
|
|
165
172
|
### Thousand Spacing Options
|
|
166
173
|
|
|
@@ -171,15 +178,341 @@ const CustomInput = (props) => (
|
|
|
171
178
|
| `"2s"` | Indian system (Lakhs/Crores) | `12,34,567` |
|
|
172
179
|
| `"4"` | Groups of 4 | `123,4567` |
|
|
173
180
|
|
|
181
|
+
## Utility Functions
|
|
182
|
+
|
|
183
|
+
### formatCurrency
|
|
184
|
+
|
|
185
|
+
Format a number/string without rendering a component:
|
|
186
|
+
|
|
187
|
+
```tsx
|
|
188
|
+
import { formatCurrency } from "currency-fomatter";
|
|
189
|
+
|
|
190
|
+
// Basic usage
|
|
191
|
+
formatCurrency(1234567.89);
|
|
192
|
+
// Output: "1,234,567.89"
|
|
193
|
+
|
|
194
|
+
// With options
|
|
195
|
+
formatCurrency(1234567.89, {
|
|
196
|
+
prefix: "$",
|
|
197
|
+
decimalScale: 2,
|
|
198
|
+
fixedDecimalScale: true,
|
|
199
|
+
thousandSeparator: ",",
|
|
200
|
+
});
|
|
201
|
+
// Output: "$1,234,567.89"
|
|
202
|
+
|
|
203
|
+
// Indian format
|
|
204
|
+
formatCurrency(1234567, {
|
|
205
|
+
prefix: "₹",
|
|
206
|
+
thousandSpacing: "2s",
|
|
207
|
+
});
|
|
208
|
+
// Output: "₹12,34,567"
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### parseCurrency
|
|
212
|
+
|
|
213
|
+
Parse a formatted currency string back to numeric values:
|
|
214
|
+
|
|
215
|
+
```tsx
|
|
216
|
+
import { parseCurrency } from "currency-fomatter";
|
|
217
|
+
|
|
218
|
+
const result = parseCurrency("$1,234.56", {
|
|
219
|
+
prefix: "$",
|
|
220
|
+
thousandSeparator: ",",
|
|
221
|
+
});
|
|
222
|
+
// result = {
|
|
223
|
+
// value: "1234.56",
|
|
224
|
+
// floatValue: 1234.56,
|
|
225
|
+
// formattedValue: "$1,234.56"
|
|
226
|
+
// }
|
|
227
|
+
|
|
228
|
+
// Euro format
|
|
229
|
+
const euroResult = parseCurrency("1.234,56€", {
|
|
230
|
+
suffix: "€",
|
|
231
|
+
thousandSeparator: ".",
|
|
232
|
+
decimalSeparator: ",",
|
|
233
|
+
});
|
|
234
|
+
// euroResult.floatValue = 1234.56
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### formatCompact
|
|
238
|
+
|
|
239
|
+
Format large numbers in compact notation (1K, 1M, 1B):
|
|
240
|
+
|
|
241
|
+
```tsx
|
|
242
|
+
import { formatCompact } from "currency-fomatter";
|
|
243
|
+
|
|
244
|
+
// Basic usage
|
|
245
|
+
formatCompact(1234567);
|
|
246
|
+
// Output: "1.23M"
|
|
247
|
+
|
|
248
|
+
formatCompact(2500000000);
|
|
249
|
+
// Output: "2.5B"
|
|
250
|
+
|
|
251
|
+
// With currency prefix
|
|
252
|
+
formatCompact(1500000, { prefix: "$", decimalScale: 1 });
|
|
253
|
+
// Output: "$1.5M"
|
|
254
|
+
|
|
255
|
+
// Vietnamese format
|
|
256
|
+
formatCompact(1500000000, {
|
|
257
|
+
compactDisplay: {
|
|
258
|
+
thousand: " nghìn",
|
|
259
|
+
million: " triệu",
|
|
260
|
+
billion: " tỷ",
|
|
261
|
+
},
|
|
262
|
+
suffix: " ₫",
|
|
263
|
+
});
|
|
264
|
+
// Output: "1.5 tỷ ₫"
|
|
265
|
+
|
|
266
|
+
// Parse compact format back to number
|
|
267
|
+
import { parseCompact } from "currency-fomatter";
|
|
268
|
+
|
|
269
|
+
parseCompact("2.5M");
|
|
270
|
+
// Output: { value: "2500000", floatValue: 2500000 }
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
## Locale Support
|
|
274
|
+
|
|
275
|
+
### Dynamic Locale Detection (Recommended)
|
|
276
|
+
|
|
277
|
+
Uses browser's `Intl.NumberFormat` to auto-detect format for ANY locale:
|
|
278
|
+
|
|
279
|
+
```tsx
|
|
280
|
+
import {
|
|
281
|
+
detectLocaleFormat,
|
|
282
|
+
createLocaleConfig,
|
|
283
|
+
getAutoLocaleConfig,
|
|
284
|
+
formatWithIntl,
|
|
285
|
+
} from "currency-fomatter";
|
|
286
|
+
|
|
287
|
+
// Auto-detect user's browser locale
|
|
288
|
+
const autoConfig = getAutoLocaleConfig("USD");
|
|
289
|
+
// Automatically detects separators, prefix/suffix based on user's browser
|
|
290
|
+
|
|
291
|
+
// Detect format for any locale (no hardcoding!)
|
|
292
|
+
const thaiFormat = detectLocaleFormat("th-TH", "THB");
|
|
293
|
+
// { thousandSeparator: ",", decimalSeparator: ".", prefix: "฿", ... }
|
|
294
|
+
|
|
295
|
+
const arabicFormat = detectLocaleFormat("ar-SA", "SAR");
|
|
296
|
+
// Correctly detects Arabic number formatting
|
|
297
|
+
|
|
298
|
+
// Create complete config dynamically
|
|
299
|
+
const config = createLocaleConfig("es-MX", "MXN");
|
|
300
|
+
// Full config with compactDisplay labels from Intl
|
|
301
|
+
|
|
302
|
+
// Direct Intl formatting (most accurate)
|
|
303
|
+
formatWithIntl(1234567.89, "de-DE", { style: "currency", currency: "EUR" });
|
|
304
|
+
// Output: "1.234.567,89 €"
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### Use with CurrencyFormat
|
|
308
|
+
|
|
309
|
+
```tsx
|
|
310
|
+
import { getFormatOptionsFromLocale, CurrencyFormat } from "currency-fomatter";
|
|
311
|
+
|
|
312
|
+
// Works with ANY locale - dynamic detection fallback
|
|
313
|
+
<CurrencyFormat
|
|
314
|
+
value={1234567}
|
|
315
|
+
{...getFormatOptionsFromLocale("th-TH", { currency: "THB" })}
|
|
316
|
+
/>
|
|
317
|
+
|
|
318
|
+
// Auto-detect from browser
|
|
319
|
+
import { getAutoLocaleConfig } from "currency-fomatter";
|
|
320
|
+
|
|
321
|
+
const { prefix, suffix, thousandSeparator, decimalSeparator } = getAutoLocaleConfig("USD");
|
|
322
|
+
<CurrencyFormat value={1234.56} prefix={prefix} suffix={suffix} ... />
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### Static Presets (Fallback)
|
|
326
|
+
|
|
327
|
+
Pre-configured locales for common countries:
|
|
328
|
+
|
|
329
|
+
```tsx
|
|
330
|
+
import { localePresets, getLocaleConfig } from "currency-fomatter";
|
|
331
|
+
|
|
332
|
+
// Available presets: en-US, vi-VN, de-DE, ja-JP, en-IN, fr-FR, zh-CN, ko-KR, pt-BR, en-GB
|
|
333
|
+
|
|
334
|
+
const vnConfig = getLocaleConfig("vi-VN");
|
|
335
|
+
// Uses preset if available, falls back to dynamic detection
|
|
336
|
+
|
|
337
|
+
<CurrencyFormat
|
|
338
|
+
value={1234567}
|
|
339
|
+
{...getFormatOptionsFromLocale("vi-VN")}
|
|
340
|
+
/>
|
|
341
|
+
// Output: 1.234.567 ₫
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### Custom Locale Registry
|
|
345
|
+
|
|
346
|
+
Register your own locale configurations:
|
|
347
|
+
|
|
348
|
+
```tsx
|
|
349
|
+
import { registerLocale, unregisterLocale, getLocaleConfig } from "currency-fomatter";
|
|
350
|
+
|
|
351
|
+
// Register custom locale
|
|
352
|
+
registerLocale("my-company", {
|
|
353
|
+
locale: "my-company",
|
|
354
|
+
prefix: "₿ ",
|
|
355
|
+
thousandSeparator: " ",
|
|
356
|
+
decimalSeparator: ",",
|
|
357
|
+
decimalScale: 8,
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
// Use it
|
|
361
|
+
const config = getLocaleConfig("my-company");
|
|
362
|
+
|
|
363
|
+
// Unregister when done
|
|
364
|
+
unregisterLocale("my-company");
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
## useCurrencyFormat Hook
|
|
368
|
+
|
|
369
|
+
A React hook for easy currency state management:
|
|
370
|
+
|
|
371
|
+
```tsx
|
|
372
|
+
import { useCurrencyFormat, CurrencyFormat } from "currency-fomatter";
|
|
373
|
+
|
|
374
|
+
function PriceInput() {
|
|
375
|
+
const {
|
|
376
|
+
value, // number: 1234.56
|
|
377
|
+
formattedValue, // string: "$1,234.56"
|
|
378
|
+
setValue, // (num: number | string) => void
|
|
379
|
+
inputProps, // Props to spread on CurrencyFormat
|
|
380
|
+
reset, // Reset to initial value
|
|
381
|
+
clear, // Clear value
|
|
382
|
+
} = useCurrencyFormat({
|
|
383
|
+
locale: "en-US",
|
|
384
|
+
initialValue: 1000,
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
return (
|
|
388
|
+
<div>
|
|
389
|
+
<CurrencyFormat {...inputProps} />
|
|
390
|
+
<p>Raw value: {value}</p>
|
|
391
|
+
<p>Formatted: {formattedValue}</p>
|
|
392
|
+
<button onClick={() => setValue(2000)}>Set $2000</button>
|
|
393
|
+
<button onClick={reset}>Reset</button>
|
|
394
|
+
<button onClick={clear}>Clear</button>
|
|
395
|
+
</div>
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
### Hook with Custom Options
|
|
401
|
+
|
|
402
|
+
```tsx
|
|
403
|
+
const { inputProps, formattedValue } = useCurrencyFormat({
|
|
404
|
+
locale: "vi-VN",
|
|
405
|
+
initialValue: 1000000,
|
|
406
|
+
decimalScale: 0,
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
// formattedValue = "1.000.000 ₫"
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
### Hook without Locale
|
|
413
|
+
|
|
414
|
+
```tsx
|
|
415
|
+
const { inputProps, value } = useCurrencyFormat({
|
|
416
|
+
initialValue: 99.99,
|
|
417
|
+
prefix: "$",
|
|
418
|
+
decimalScale: 2,
|
|
419
|
+
fixedDecimalScale: true,
|
|
420
|
+
});
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
## Using with Form Libraries
|
|
424
|
+
|
|
425
|
+
### react-hook-form
|
|
426
|
+
|
|
427
|
+
The component supports `ref` forwarding for seamless integration:
|
|
428
|
+
|
|
429
|
+
```tsx
|
|
430
|
+
import { useForm } from "react-hook-form";
|
|
431
|
+
import { CurrencyFormat } from "currency-fomatter";
|
|
432
|
+
|
|
433
|
+
function MyForm() {
|
|
434
|
+
const { register, handleSubmit, setValue } = useForm();
|
|
435
|
+
|
|
436
|
+
return (
|
|
437
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
438
|
+
<CurrencyFormat
|
|
439
|
+
{...register("price")}
|
|
440
|
+
name="price"
|
|
441
|
+
thousandSeparator=","
|
|
442
|
+
prefix="$"
|
|
443
|
+
onValueChange={(values, sourceInfo) => {
|
|
444
|
+
// Only update form when user interacts, not on prop changes
|
|
445
|
+
if (sourceInfo?.source === "event") {
|
|
446
|
+
setValue("price", values.floatValue);
|
|
447
|
+
}
|
|
448
|
+
}}
|
|
449
|
+
/>
|
|
450
|
+
</form>
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
### Multiple Fields
|
|
456
|
+
|
|
457
|
+
Use the `name` prop to identify fields in shared handlers:
|
|
458
|
+
|
|
459
|
+
```tsx
|
|
460
|
+
function PriceForm() {
|
|
461
|
+
const [prices, setPrices] = useState({ min: 0, max: 0 });
|
|
462
|
+
|
|
463
|
+
const handleValueChange = (values, sourceInfo) => {
|
|
464
|
+
if (values.name) {
|
|
465
|
+
setPrices(prev => ({
|
|
466
|
+
...prev,
|
|
467
|
+
[values.name]: values.floatValue
|
|
468
|
+
}));
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
return (
|
|
473
|
+
<>
|
|
474
|
+
<CurrencyFormat
|
|
475
|
+
name="min"
|
|
476
|
+
value={prices.min}
|
|
477
|
+
onValueChange={handleValueChange}
|
|
478
|
+
prefix="$"
|
|
479
|
+
/>
|
|
480
|
+
<CurrencyFormat
|
|
481
|
+
name="max"
|
|
482
|
+
value={prices.max}
|
|
483
|
+
onValueChange={handleValueChange}
|
|
484
|
+
prefix="$"
|
|
485
|
+
/>
|
|
486
|
+
</>
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
```
|
|
490
|
+
|
|
174
491
|
## TypeScript
|
|
175
492
|
|
|
176
493
|
The package includes full TypeScript support with exported types:
|
|
177
494
|
|
|
178
495
|
```tsx
|
|
179
496
|
import CurrencyFormat, {
|
|
497
|
+
// Component props
|
|
180
498
|
CurrencyFormatProps,
|
|
181
499
|
ValueObject,
|
|
182
500
|
ThousandSpacing,
|
|
501
|
+
|
|
502
|
+
// Utility function types
|
|
503
|
+
FormatCurrencyOptions,
|
|
504
|
+
ParseCurrencyOptions,
|
|
505
|
+
CompactDisplayOptions,
|
|
506
|
+
FormatCompactOptions,
|
|
507
|
+
|
|
508
|
+
// Hook types
|
|
509
|
+
UseCurrencyFormatOptions,
|
|
510
|
+
UseCurrencyFormatReturn,
|
|
511
|
+
|
|
512
|
+
// Locale types
|
|
513
|
+
LocaleConfig,
|
|
514
|
+
|
|
515
|
+
// Callback types
|
|
183
516
|
FormatFunction,
|
|
184
517
|
IsAllowedFunction,
|
|
185
518
|
OnValueChangeFunction,
|
|
@@ -190,6 +523,30 @@ interface ValueObject {
|
|
|
190
523
|
formattedValue: string; // Formatted display value
|
|
191
524
|
value: string; // Unformatted numeric string
|
|
192
525
|
floatValue: number; // Parsed float value
|
|
526
|
+
name?: string; // Field name (if provided)
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// onValueChange callback signature
|
|
530
|
+
type OnValueChangeFunction = (
|
|
531
|
+
values: ValueObject,
|
|
532
|
+
sourceInfo?: {
|
|
533
|
+
event?: ChangeEvent<HTMLInputElement>;
|
|
534
|
+
source: "event" | "prop"; // "event" = user input, "prop" = value prop changed
|
|
535
|
+
}
|
|
536
|
+
) => void;
|
|
537
|
+
|
|
538
|
+
// Locale config type
|
|
539
|
+
interface LocaleConfig {
|
|
540
|
+
locale: string;
|
|
541
|
+
currency?: string;
|
|
542
|
+
currencySymbol?: string;
|
|
543
|
+
prefix?: string;
|
|
544
|
+
suffix?: string;
|
|
545
|
+
thousandSeparator?: string | boolean;
|
|
546
|
+
decimalSeparator?: string;
|
|
547
|
+
thousandSpacing?: ThousandSpacing;
|
|
548
|
+
decimalScale?: number;
|
|
549
|
+
compactDisplay?: CompactDisplayOptions;
|
|
193
550
|
}
|
|
194
551
|
```
|
|
195
552
|
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { FormatCurrencyOptions, ValueObject } from "./utils";
|
|
2
|
+
import { LocaleConfig } from "./locales";
|
|
3
|
+
export interface UseCurrencyFormatOptions extends FormatCurrencyOptions {
|
|
4
|
+
locale?: string;
|
|
5
|
+
initialValue?: number | string;
|
|
6
|
+
}
|
|
7
|
+
export interface UseCurrencyFormatReturn {
|
|
8
|
+
/** Raw numeric value as number */
|
|
9
|
+
value: number;
|
|
10
|
+
/** Raw numeric value as string (preserves precision) */
|
|
11
|
+
valueAsString: string;
|
|
12
|
+
/** Formatted display value */
|
|
13
|
+
formattedValue: string;
|
|
14
|
+
/** Set value from number or string */
|
|
15
|
+
setValue: (value: number | string) => void;
|
|
16
|
+
/** Set value from formatted string (parses it first) */
|
|
17
|
+
setFormattedValue: (formatted: string) => void;
|
|
18
|
+
/** Format options derived from locale or props */
|
|
19
|
+
formatOptions: FormatCurrencyOptions;
|
|
20
|
+
/** Props to spread on CurrencyFormat component */
|
|
21
|
+
inputProps: {
|
|
22
|
+
value: number | string;
|
|
23
|
+
onValueChange: (values: ValueObject) => void;
|
|
24
|
+
decimalSeparator: string;
|
|
25
|
+
thousandSeparator: string | boolean;
|
|
26
|
+
prefix: string;
|
|
27
|
+
suffix: string;
|
|
28
|
+
decimalScale?: number;
|
|
29
|
+
fixedDecimalScale?: boolean;
|
|
30
|
+
allowNegative?: boolean;
|
|
31
|
+
};
|
|
32
|
+
/** Reset to initial value */
|
|
33
|
+
reset: () => void;
|
|
34
|
+
/** Clear value */
|
|
35
|
+
clear: () => void;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Hook for managing currency format state
|
|
39
|
+
* Makes it easy to use currency formatting in forms
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```tsx
|
|
43
|
+
* const { value, formattedValue, inputProps } = useCurrencyFormat({
|
|
44
|
+
* locale: "en-US",
|
|
45
|
+
* initialValue: 1000,
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* return <CurrencyFormat {...inputProps} />;
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function useCurrencyFormat(options?: UseCurrencyFormatOptions): UseCurrencyFormatReturn;
|
|
52
|
+
export type { LocaleConfig };
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useCurrencyFormat = void 0;
|
|
4
|
+
var react_1 = require("react");
|
|
5
|
+
var utils_1 = require("./utils");
|
|
6
|
+
var locales_1 = require("./locales");
|
|
7
|
+
/**
|
|
8
|
+
* Hook for managing currency format state
|
|
9
|
+
* Makes it easy to use currency formatting in forms
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* const { value, formattedValue, inputProps } = useCurrencyFormat({
|
|
14
|
+
* locale: "en-US",
|
|
15
|
+
* initialValue: 1000,
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* return <CurrencyFormat {...inputProps} />;
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
function useCurrencyFormat(options) {
|
|
22
|
+
if (options === void 0) { options = {}; }
|
|
23
|
+
var locale = options.locale, _a = options.initialValue, initialValue = _a === void 0 ? 0 : _a, decimalSeparatorProp = options.decimalSeparator, thousandSeparatorProp = options.thousandSeparator, prefixProp = options.prefix, suffixProp = options.suffix, decimalScaleProp = options.decimalScale, fixedDecimalScaleProp = options.fixedDecimalScale, allowNegativeProp = options.allowNegative, thousandSpacingProp = options.thousandSpacing;
|
|
24
|
+
// Get locale config if locale is provided
|
|
25
|
+
var localeOptions = (0, react_1.useMemo)(function () { return (locale ? (0, locales_1.getFormatOptionsFromLocale)(locale) : undefined); }, [locale]);
|
|
26
|
+
// Merge options: prop values override locale values
|
|
27
|
+
var formatOptions = (0, react_1.useMemo)(function () {
|
|
28
|
+
var _a, _b, _c, _d, _e, _f;
|
|
29
|
+
var base = localeOptions || {};
|
|
30
|
+
return {
|
|
31
|
+
decimalSeparator: (_a = decimalSeparatorProp !== null && decimalSeparatorProp !== void 0 ? decimalSeparatorProp : base.decimalSeparator) !== null && _a !== void 0 ? _a : ".",
|
|
32
|
+
thousandSeparator: (_b = thousandSeparatorProp !== null && thousandSeparatorProp !== void 0 ? thousandSeparatorProp : base.thousandSeparator) !== null && _b !== void 0 ? _b : ",",
|
|
33
|
+
prefix: (_c = prefixProp !== null && prefixProp !== void 0 ? prefixProp : base.prefix) !== null && _c !== void 0 ? _c : "",
|
|
34
|
+
suffix: (_d = suffixProp !== null && suffixProp !== void 0 ? suffixProp : base.suffix) !== null && _d !== void 0 ? _d : "",
|
|
35
|
+
decimalScale: decimalScaleProp !== null && decimalScaleProp !== void 0 ? decimalScaleProp : base.decimalScale,
|
|
36
|
+
fixedDecimalScale: fixedDecimalScaleProp !== null && fixedDecimalScaleProp !== void 0 ? fixedDecimalScaleProp : base.fixedDecimalScale,
|
|
37
|
+
allowNegative: (_e = allowNegativeProp !== null && allowNegativeProp !== void 0 ? allowNegativeProp : base.allowNegative) !== null && _e !== void 0 ? _e : true,
|
|
38
|
+
thousandSpacing: (_f = thousandSpacingProp !== null && thousandSpacingProp !== void 0 ? thousandSpacingProp : base.thousandSpacing) !== null && _f !== void 0 ? _f : "3",
|
|
39
|
+
};
|
|
40
|
+
}, [
|
|
41
|
+
localeOptions,
|
|
42
|
+
decimalSeparatorProp,
|
|
43
|
+
thousandSeparatorProp,
|
|
44
|
+
prefixProp,
|
|
45
|
+
suffixProp,
|
|
46
|
+
decimalScaleProp,
|
|
47
|
+
fixedDecimalScaleProp,
|
|
48
|
+
allowNegativeProp,
|
|
49
|
+
thousandSpacingProp,
|
|
50
|
+
]);
|
|
51
|
+
// Parse initial value
|
|
52
|
+
var parseInitialValue = (0, react_1.useCallback)(function (val) {
|
|
53
|
+
if (typeof val === "number") {
|
|
54
|
+
return isNaN(val) ? "" : val.toString();
|
|
55
|
+
}
|
|
56
|
+
return val;
|
|
57
|
+
}, []);
|
|
58
|
+
// State
|
|
59
|
+
var _b = (0, react_1.useState)(function () {
|
|
60
|
+
return parseInitialValue(initialValue);
|
|
61
|
+
}), valueAsString = _b[0], setValueAsString = _b[1];
|
|
62
|
+
// Derived values
|
|
63
|
+
var value = (0, react_1.useMemo)(function () { return (valueAsString === "" ? NaN : parseFloat(valueAsString)); }, [valueAsString]);
|
|
64
|
+
var formattedValue = (0, react_1.useMemo)(function () { return (0, utils_1.formatCurrency)(valueAsString, formatOptions); }, [valueAsString, formatOptions]);
|
|
65
|
+
// Setters
|
|
66
|
+
var setValue = (0, react_1.useCallback)(function (newValue) {
|
|
67
|
+
if (typeof newValue === "number") {
|
|
68
|
+
setValueAsString(isNaN(newValue) ? "" : newValue.toString());
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
// Parse if it looks formatted
|
|
72
|
+
var parseOptions = {
|
|
73
|
+
decimalSeparator: formatOptions.decimalSeparator,
|
|
74
|
+
thousandSeparator: formatOptions.thousandSeparator,
|
|
75
|
+
prefix: formatOptions.prefix,
|
|
76
|
+
suffix: formatOptions.suffix,
|
|
77
|
+
};
|
|
78
|
+
var parsed = (0, utils_1.parseCurrency)(newValue, parseOptions);
|
|
79
|
+
setValueAsString(parsed.value);
|
|
80
|
+
}
|
|
81
|
+
}, [formatOptions]);
|
|
82
|
+
var setFormattedValue = (0, react_1.useCallback)(function (formatted) {
|
|
83
|
+
var parseOptions = {
|
|
84
|
+
decimalSeparator: formatOptions.decimalSeparator,
|
|
85
|
+
thousandSeparator: formatOptions.thousandSeparator,
|
|
86
|
+
prefix: formatOptions.prefix,
|
|
87
|
+
suffix: formatOptions.suffix,
|
|
88
|
+
};
|
|
89
|
+
var parsed = (0, utils_1.parseCurrency)(formatted, parseOptions);
|
|
90
|
+
setValueAsString(parsed.value);
|
|
91
|
+
}, [formatOptions]);
|
|
92
|
+
var reset = (0, react_1.useCallback)(function () {
|
|
93
|
+
setValueAsString(parseInitialValue(initialValue));
|
|
94
|
+
}, [initialValue, parseInitialValue]);
|
|
95
|
+
var clear = (0, react_1.useCallback)(function () {
|
|
96
|
+
setValueAsString("");
|
|
97
|
+
}, []);
|
|
98
|
+
// Handle value change from CurrencyFormat component
|
|
99
|
+
var handleValueChange = (0, react_1.useCallback)(function (values) {
|
|
100
|
+
setValueAsString(values.value);
|
|
101
|
+
}, []);
|
|
102
|
+
// Props to spread on CurrencyFormat
|
|
103
|
+
var inputProps = (0, react_1.useMemo)(function () {
|
|
104
|
+
var _a;
|
|
105
|
+
return ({
|
|
106
|
+
value: valueAsString === "" ? "" : parseFloat(valueAsString),
|
|
107
|
+
onValueChange: handleValueChange,
|
|
108
|
+
decimalSeparator: formatOptions.decimalSeparator || ".",
|
|
109
|
+
thousandSeparator: (_a = formatOptions.thousandSeparator) !== null && _a !== void 0 ? _a : ",",
|
|
110
|
+
prefix: formatOptions.prefix || "",
|
|
111
|
+
suffix: formatOptions.suffix || "",
|
|
112
|
+
decimalScale: formatOptions.decimalScale,
|
|
113
|
+
fixedDecimalScale: formatOptions.fixedDecimalScale,
|
|
114
|
+
allowNegative: formatOptions.allowNegative,
|
|
115
|
+
});
|
|
116
|
+
}, [valueAsString, handleValueChange, formatOptions]);
|
|
117
|
+
return {
|
|
118
|
+
value: value,
|
|
119
|
+
valueAsString: valueAsString,
|
|
120
|
+
formattedValue: formattedValue,
|
|
121
|
+
setValue: setValue,
|
|
122
|
+
setFormattedValue: setFormattedValue,
|
|
123
|
+
formatOptions: formatOptions,
|
|
124
|
+
inputProps: inputProps,
|
|
125
|
+
reset: reset,
|
|
126
|
+
clear: clear,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
exports.useCurrencyFormat = useCurrencyFormat;
|
|
130
|
+
//# sourceMappingURL=hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.js","sourceRoot":"","sources":["../../../../src/components/currency/hooks.ts"],"names":[],"mappings":";;;AAAA,+BAAuD;AACvD,iCAMiB;AACjB,qCAAqE;AAsCrE;;;;;;;;;;;;;GAaG;AACH,SAAgB,iBAAiB,CAC/B,OAAsC;IAAtC,wBAAA,EAAA,YAAsC;IAGpC,IAAA,MAAM,GAUJ,OAAO,OAVH,EACN,KASE,OAAO,aATO,EAAhB,YAAY,mBAAG,CAAC,KAAA,EACE,oBAAoB,GAQpC,OAAO,iBAR6B,EACnB,qBAAqB,GAOtC,OAAO,kBAP+B,EAChC,UAAU,GAMhB,OAAO,OANS,EACV,UAAU,GAKhB,OAAO,OALS,EACJ,gBAAgB,GAI5B,OAAO,aAJqB,EACX,qBAAqB,GAGtC,OAAO,kBAH+B,EACzB,iBAAiB,GAE9B,OAAO,cAFuB,EACf,mBAAmB,GAClC,OAAO,gBAD2B,CAC1B;IAEZ,0CAA0C;IAC1C,IAAM,aAAa,GAAG,IAAA,eAAO,EAC3B,cAAM,OAAA,CAAC,MAAM,CAAC,CAAC,CAAC,IAAA,oCAA0B,EAAC,MAAM,CAAC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AAgB,IAAI,GAAG;YACvD,iBAAiB,EAAE,MAAA,aAAa,CAAC,iBAAiB,mCAAI,GAAG;YACzD,MAAM,EAAE,aAAa,CAAC,MAAM,IAAI,EAAE;YAClC,MAAM,EAAE,aAAa,CAAC,MAAM,IAAI,EAAE;YAClC,YAAY,EAAE,aAAa,CAAC,YAAY;YACxC,iBAAiB,EAAE,aAAa,CAAC,iBAAiB;YAClD,aAAa,EAAE,aAAa,CAAC,aAAa;SAC3C,CAAC,CAAA;KAAA,EACF,CAAC,aAAa,EAAE,iBAAiB,EAAE,aAAa,CAAC,CAClD,CAAC;IAEF,OAAO;QACL,KAAK,OAAA;QACL,aAAa,eAAA;QACb,cAAc,gBAAA;QACd,QAAQ,UAAA;QACR,iBAAiB,mBAAA;QACjB,aAAa,eAAA;QACb,UAAU,YAAA;QACV,KAAK,OAAA;QACL,KAAK,OAAA;KACN,CAAC;AACJ,CAAC;AApJD,8CAoJC"}
|
|
@@ -1,13 +1,22 @@
|
|
|
1
|
-
import { ComponentType, InputHTMLAttributes, FocusEvent, KeyboardEvent, ChangeEvent, MouseEvent, ReactNode } from "react";
|
|
1
|
+
import React, { ComponentType, InputHTMLAttributes, FocusEvent, KeyboardEvent, ChangeEvent, MouseEvent, ReactNode } from "react";
|
|
2
2
|
import { ThousandSpacing, ValueObject } from "./utils";
|
|
3
|
-
export type { ThousandSpacing, ValueObject } from "./utils";
|
|
3
|
+
export type { ThousandSpacing, ValueObject, FormatCurrencyOptions, ParseCurrencyOptions, CompactDisplayOptions, FormatCompactOptions, } from "./utils";
|
|
4
|
+
export { formatCurrency, parseCurrency, formatCompact, parseCompact, defaultCompactDisplay, } from "./utils";
|
|
5
|
+
export { useCurrencyFormat } from "./hooks";
|
|
6
|
+
export type { UseCurrencyFormatOptions, UseCurrencyFormatReturn } from "./hooks";
|
|
7
|
+
export { localePresets, getLocaleConfig, getFormatOptionsFromLocale, detectLocaleFormat, getCompactLabels, createLocaleConfig, getAutoLocaleConfig, formatWithIntl, registerLocale, unregisterLocale, } from "./locales";
|
|
8
|
+
export type { LocaleConfig } from "./locales";
|
|
4
9
|
export type FormatFunction = (value: string) => string;
|
|
5
10
|
export type RemoveFormattingFunction = (value: string) => string;
|
|
6
11
|
export type IsAllowedFunction = (values: ValueObject) => boolean;
|
|
7
|
-
export type OnValueChangeFunction = (values: ValueObject
|
|
12
|
+
export type OnValueChangeFunction = (values: ValueObject, sourceInfo?: {
|
|
13
|
+
event?: ChangeEvent<HTMLInputElement>;
|
|
14
|
+
source: "event" | "prop";
|
|
15
|
+
}) => void;
|
|
8
16
|
export type RenderTextFunction = (value: string, otherProps: Record<string, unknown>) => ReactNode;
|
|
9
17
|
export interface CurrencyFormatProps extends Omit<InputHTMLAttributes<HTMLInputElement>, "value" | "onChange" | "onKeyDown" | "onMouseUp" | "onFocus" | "onBlur"> {
|
|
10
18
|
value?: string | number;
|
|
19
|
+
name?: string;
|
|
11
20
|
format?: string | FormatFunction;
|
|
12
21
|
decimalScale?: number;
|
|
13
22
|
decimalSeparator?: string;
|
|
@@ -31,6 +40,7 @@ export interface CurrencyFormatProps extends Omit<InputHTMLAttributes<HTMLInputE
|
|
|
31
40
|
displayType?: "input" | "text";
|
|
32
41
|
customInput?: ComponentType<InputHTMLAttributes<HTMLInputElement>>;
|
|
33
42
|
renderText?: RenderTextFunction;
|
|
43
|
+
getInputRef?: (el: HTMLInputElement | null) => void;
|
|
34
44
|
}
|
|
35
|
-
declare
|
|
45
|
+
declare const CurrencyFormat: React.ForwardRefExoticComponent<CurrencyFormatProps & React.RefAttributes<HTMLInputElement>>;
|
|
36
46
|
export default CurrencyFormat;
|