MCPcopy Create free account
hub / github.com/Athenodoros/TopHat / useNumericInputHandler

Function useNumericInputHandler

src/shared/hooks.ts:89–113  ·  view source on GitHub ↗
(
    initial: number | null,
    onChange: (value: number | null) => void,
    id: any = ""
)

Source from the content-addressed store, hash-verified

87const NumberRegex = /^-?\d*\.?\d?\d?$/;
88const FormatNumber = (value: number | null) => (value !== null ? formatNumber(value, { separator: "" }) : "");
89export const useNumericInputHandler = (
90 initial: number | null,
91 onChange: (value: number | null) => void,
92 id: any = ""
93) => {
94 const [text, setText] = useState(FormatNumber(initial));
95 const { onTextChange, setValue } = useMemo(
96 () => ({
97 onTextChange: handleTextFieldChange((raw) => {
98 const value = raw.replaceAll(/[^\-0-9\.]/g, "");
99 if (NumberRegex.test(value)) {
100 setText(value);
101 onChange(value === "" ? null : +value);
102 }
103 }),
104 setValue: (value: number | null) => setText(FormatNumber(value)),
105 }),
106 [setText, onChange]
107 );
108 useEffect(() => {
109 if (FormatNumber(initial) !== text) setText(FormatNumber(initial));
110 // eslint-disable-next-line react-hooks/exhaustive-deps
111 }, [id]);
112 return { text, setText, setValue, onTextChange };
113};
114
115export const useRefToValue = <T>(t: T) => {
116 const ref = useRef(t);

Callers 8

TransactionsTableHeaderFunction · 0.90
EditableCurrencyValueFunction · 0.90
BudgetTransferElementsFunction · 0.90
EditRuleViewFunction · 0.90
useNetWorthInputFunction · 0.90
useDebtInputFunction · 0.90
useTimeSeriesInputFunction · 0.90

Calls 3

handleTextFieldChangeFunction · 0.90
FormatNumberFunction · 0.85
onChangeFunction · 0.85

Tested by

no test coverage detected