import React, {useState} from 'react';
export default function ValidatingInputNumberField({
id,
label,
defaultValue = 0,
constraints = {},
validatorFn = (value) => {
return {valid: true, value};
},
disabled = false,
onChange = _ => {
}
}) {
const [error, setError] = useState(null);
const [value, setValue] = useState(defaultValue);
const handleValueChange = (e) => {
const value = e.target.value;
const validation = validatorFn(value);
if (!validation.valid) {
setError(validation.message);
} else {
setError(null);
onChange(validation.value);
}
setValue(validation.value);
};
let errorComponent;
if (!!error) {
errorComponent = {error};
} else {
errorComponent = ;
}
return (
{errorComponent}
);
}