wisesplit/components/NumberEditBox.tsx

29 lines
632 B
TypeScript
Raw Normal View History

2022-10-25 18:26:48 +00:00
import { PrimitiveAtom } from "jotai";
2022-10-24 19:00:16 +00:00
import EditBox from "./EditBox";
export interface Props {
2022-10-25 18:26:48 +00:00
valueAtom: PrimitiveAtom<number>;
2022-10-24 19:00:16 +00:00
formatter?: (arg: number) => string;
}
export default function NumberEditBox({ valueAtom, formatter }: Props) {
2022-10-25 18:26:48 +00:00
const validator = (arg: string): number | null => {
2022-10-24 19:00:16 +00:00
try {
const n = parseFloat(arg);
2022-10-25 18:26:48 +00:00
if (isNaN(n) || !isFinite(n)) return null;
2022-10-24 19:00:16 +00:00
return n;
} catch (e) {
return null;
}
};
return (
<EditBox
valueAtom={valueAtom}
inputType="number"
formatter={formatter ?? ((n) => n.toString())}
validator={validator}
/>
);
}