import { CalculatorIcon } from '@heroicons/react/24/outline'; import * as React from 'react'; import { useEffect, useMemo } from 'react'; import { BaseFieldProps, NumberOptions } from '../../../models'; import { FieldTitle } from './FieldTitle'; import { FieldMessage } from './FieldMessage'; export interface INumberFieldProps extends BaseFieldProps { options?: NumberOptions; onChange: (nrValue: number | null) => void; } export const NumberField: React.FunctionComponent = ({ label, description, options, value, required, onChange }: React.PropsWithChildren) => { const [nrValue, setNrValue] = React.useState(value); const onValueChange = (txtValue: string) => { let newValue: number | null = options?.isDecimal ? parseFloat(txtValue) : parseInt(txtValue); if (isNaN(newValue)) { newValue = null; } setNrValue(newValue); onChange(newValue); }; const showRequiredState = useMemo(() => { return required && (nrValue === null || nrValue === undefined); }, [required, nrValue]); const stepValue = useMemo(() => { if (options?.isDecimal) { return options?.step ?? 0.1; } return options?.step ?? 1; }, [options?.step]); useEffect(() => { if (nrValue !== value) { setNrValue(value); } }, [value]); return (
} required={required} /> onValueChange(e.target.value)} />
); };