|
1 | | -import React, { forwardRef, ElementRef, ComponentPropsWithoutRef } from 'react'; |
2 | | -import { useControllableState } from '~/core/hooks/useControllableState'; |
3 | | -import NumberFieldContext from '../contexts/NumberFieldContext'; |
4 | | -import { customClassSwitcher } from '~/core'; |
5 | | -import clsx from 'clsx'; |
| 1 | +import React, { forwardRef, ElementRef, ComponentPropsWithoutRef } from "react"; |
| 2 | +import { useControllableState } from "~/core/hooks/useControllableState"; |
| 3 | +import NumberFieldContext from "../contexts/NumberFieldContext"; |
| 4 | +import { customClassSwitcher } from "~/core"; |
| 5 | +import clsx from "clsx"; |
6 | 6 |
|
7 | | -const COMPONENT_NAME = 'NumberField'; |
| 7 | +const COMPONENT_NAME = "NumberField"; |
8 | 8 |
|
9 | | -export type NumberFieldRootElement = ElementRef<'div'>; |
| 9 | +export type NumberFieldRootElement = ElementRef<"div">; |
10 | 10 | export type NumberFieldRootProps = { |
11 | | - name?: string |
12 | | - defaultValue?: number | '' |
13 | | - value?: number | '' |
14 | | - onValueChange?: (value: number | '') => void |
15 | | - step?: number |
16 | | - largeStep?: number |
17 | | - min?: number |
18 | | - max?: number |
19 | | - disabled?: boolean |
20 | | - readOnly?: boolean |
21 | | - required?: boolean |
22 | | -} & ComponentPropsWithoutRef<'div'>; |
23 | | - |
24 | | -const NumberFieldRoot = forwardRef<NumberFieldRootElement, NumberFieldRootProps>(({ children, name, defaultValue = '', value, onValueChange, largeStep, step, min, max, disabled, readOnly, required, id, className, ...props }, ref) => { |
| 11 | + name?: string; |
| 12 | + defaultValue?: number | ""; |
| 13 | + value?: number | ""; |
| 14 | + onValueChange?: (value: number | "") => void; |
| 15 | + step?: number; |
| 16 | + largeStep?: number; |
| 17 | + min?: number; |
| 18 | + max?: number; |
| 19 | + disabled?: boolean; |
| 20 | + readOnly?: boolean; |
| 21 | + required?: boolean; |
| 22 | + locale?: string; |
| 23 | +} & ComponentPropsWithoutRef<"div">; |
| 24 | + |
| 25 | +const NumberFieldRoot = forwardRef< |
| 26 | + NumberFieldRootElement, |
| 27 | + NumberFieldRootProps |
| 28 | +>( |
| 29 | + ( |
| 30 | + { |
| 31 | + children, |
| 32 | + name, |
| 33 | + defaultValue = "", |
| 34 | + value, |
| 35 | + onValueChange, |
| 36 | + largeStep, |
| 37 | + step = 1, |
| 38 | + min, |
| 39 | + max, |
| 40 | + disabled, |
| 41 | + readOnly, |
| 42 | + required, |
| 43 | + id, |
| 44 | + className, |
| 45 | + locale, |
| 46 | + ...props |
| 47 | + }, |
| 48 | + ref, |
| 49 | + ) => { |
25 | 50 | const rootClass = customClassSwitcher(className, COMPONENT_NAME); |
26 | | - const [inputValue, setInputValue] = useControllableState<number | ''>( |
27 | | - value, |
28 | | - defaultValue, |
29 | | - onValueChange); |
30 | | - |
31 | | - const handleOnChange = (input: number| '') => { |
32 | | - if (input === '') { |
33 | | - setInputValue(''); |
34 | | - return; |
35 | | - } |
36 | | - if (max !== undefined && input > max) { |
37 | | - setInputValue(max); |
38 | | - return; |
39 | | - } |
| 51 | + const [inputValue, setInputValue] = useControllableState<number | "">( |
| 52 | + value, |
| 53 | + defaultValue, |
| 54 | + onValueChange, |
| 55 | + ); |
40 | 56 |
|
41 | | - if (min !== undefined && input < min) { |
42 | | - setInputValue(min); |
43 | | - return; |
44 | | - } |
| 57 | + const getDecimalSeparator = (locale: string) => { |
| 58 | + const parts = new Intl.NumberFormat(locale).formatToParts(1234.5); |
| 59 | + return parts.find((part) => part.type === "decimal")?.value || "."; |
| 60 | + }; |
45 | 61 |
|
46 | | - setInputValue(input); |
| 62 | + const handleOnChange = (val: string) => { |
| 63 | + if (val === "") { |
| 64 | + setInputValue(""); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + const decimal = getDecimalSeparator(locale || "en-US"); |
| 69 | + const regex = new RegExp(`[^0-9${decimal}]`, "g"); |
| 70 | + const cleaned = val.replace(regex, ""); |
| 71 | + const normalized = cleaned.replace(decimal, "."); |
| 72 | + const numericValue = parseFloat(normalized); |
| 73 | + |
| 74 | + if (isNaN(numericValue)) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (max !== undefined && numericValue > max) { |
| 79 | + setInputValue(max); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (min !== undefined && numericValue < min) { |
| 84 | + setInputValue(min); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + setInputValue(numericValue); |
47 | 89 | }; |
48 | 90 | const applyStep = (amount: number) => { |
49 | | - setInputValue((prev) => { |
50 | | - let temp = prev; |
51 | | - if (temp === '') { |
52 | | - if (min !== undefined) { |
53 | | - temp = min; |
54 | | - } else { |
55 | | - temp = -1; |
56 | | - } |
57 | | - } |
58 | | - const nextValue = temp + amount; |
59 | | - |
60 | | - if (max !== undefined && nextValue > max) { |
61 | | - return max; |
62 | | - } |
63 | | - |
64 | | - if (min !== undefined && nextValue < min) { |
65 | | - return min; |
66 | | - } |
67 | | - |
68 | | - return nextValue; |
69 | | - }); |
70 | | - }; |
| 91 | + setInputValue((prev) => { |
| 92 | + let temp = prev; |
| 93 | + if (temp === "") { |
| 94 | + if (min !== undefined) { |
| 95 | + temp = min; |
| 96 | + } else { |
| 97 | + temp = 0; |
| 98 | + } |
| 99 | + } |
| 100 | + const nextValue = temp + amount; |
71 | 101 |
|
72 | | - const handleStep = ({ type, direction } : {type: 'small'| 'large', direction: 'increment' | 'decrement' }) => { |
73 | | - let amount = 0; |
74 | | - |
75 | | - switch (type) { |
76 | | - case 'small': |
77 | | - if (!step) return; |
78 | | - amount = step; |
79 | | - break; |
80 | | - case 'large': |
81 | | - if (!largeStep) return; |
82 | | - amount = largeStep; |
83 | | - break; |
| 102 | + if (max !== undefined && nextValue > max) { |
| 103 | + return max; |
84 | 104 | } |
85 | 105 |
|
86 | | - if (direction === 'decrement') { |
87 | | - amount *= -1; |
| 106 | + if (min !== undefined && nextValue < min) { |
| 107 | + return min; |
88 | 108 | } |
89 | 109 |
|
90 | | - applyStep(amount); |
| 110 | + return nextValue; |
| 111 | + }); |
91 | 112 | }; |
92 | 113 |
|
| 114 | + const handleStep = ({ |
| 115 | + type, |
| 116 | + direction, |
| 117 | + }: { |
| 118 | + type: "small" | "large"; |
| 119 | + direction: "increment" | "decrement"; |
| 120 | + }) => { |
| 121 | + let amount = 0; |
| 122 | + |
| 123 | + switch (type) { |
| 124 | + case "small": |
| 125 | + if (!step) { |
| 126 | + return; |
| 127 | + } |
| 128 | + amount = step; |
| 129 | + break; |
| 130 | + case "large": |
| 131 | + if (!largeStep) return; |
| 132 | + amount = largeStep; |
| 133 | + break; |
| 134 | + } |
| 135 | + |
| 136 | + if (direction === "decrement") { |
| 137 | + amount *= -1; |
| 138 | + } |
| 139 | + |
| 140 | + applyStep(amount); |
| 141 | + }; |
| 142 | + |
| 143 | + const formattedValue = new Intl.NumberFormat(locale ? locale : "en-US", { |
| 144 | + maximumFractionDigits: 20, |
| 145 | + }).format(inputValue === "" ? 0 : inputValue); |
| 146 | + |
93 | 147 | const contextValues = { |
94 | | - inputValue, |
95 | | - handleOnChange, |
96 | | - handleStep, |
97 | | - id, |
98 | | - name, |
99 | | - disabled, |
100 | | - readOnly, |
101 | | - required, |
102 | | - rootClass |
| 148 | + inputValue, |
| 149 | + formattedValue, |
| 150 | + locale, |
| 151 | + handleOnChange, |
| 152 | + handleStep, |
| 153 | + id, |
| 154 | + name, |
| 155 | + disabled, |
| 156 | + readOnly, |
| 157 | + required, |
| 158 | + rootClass, |
103 | 159 | }; |
104 | 160 |
|
105 | 161 | return ( |
106 | | - <div ref={ref} className={clsx(`${rootClass}-root`, className)} {...props}> |
107 | | - <NumberFieldContext.Provider value={contextValues}> |
108 | | - {children} |
109 | | - </NumberFieldContext.Provider> |
110 | | - </div> |
| 162 | + <div |
| 163 | + ref={ref} |
| 164 | + className={clsx(`${rootClass}-root`, className)} |
| 165 | + {...props} |
| 166 | + > |
| 167 | + <NumberFieldContext.Provider value={contextValues}> |
| 168 | + {children} |
| 169 | + </NumberFieldContext.Provider> |
| 170 | + </div> |
111 | 171 | ); |
112 | | -}); |
| 172 | + }, |
| 173 | +); |
113 | 174 |
|
114 | 175 | NumberFieldRoot.displayName = COMPONENT_NAME; |
115 | 176 |
|
116 | 177 | export default NumberFieldRoot; |
| 178 | + |
0 commit comments