update react
This commit is contained in:
parent
804ce323bf
commit
7d4f1151fa
40 changed files with 3573 additions and 14078 deletions
41
src/app/validating-input-number-field.tsx
Normal file
41
src/app/validating-input-number-field.tsx
Normal file
|
@ -0,0 +1,41 @@
|
|||
import React, {useState} from 'react';
|
||||
|
||||
export default function ValidatingInputNumberField({
|
||||
id,
|
||||
label,
|
||||
value = 0,
|
||||
constraints = {},
|
||||
validatorFn = (value) => {
|
||||
return {valid: true, value};
|
||||
},
|
||||
disabled = false,
|
||||
onChange = _ => {
|
||||
}
|
||||
}) {
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
const handleValueChange = (e) => {
|
||||
const value = e.target.value;
|
||||
const validation = validatorFn(value);
|
||||
if (!validation.valid) {
|
||||
setError(validation.message);
|
||||
} else {
|
||||
setError(null);
|
||||
}
|
||||
onChange(validation.value);
|
||||
};
|
||||
return (
|
||||
<>
|
||||
<label htmlFor={id}>{label}: </label>
|
||||
<input id={id}
|
||||
type={"number"}
|
||||
onChange={handleValueChange}
|
||||
value={value || ""}
|
||||
min={constraints.min || null}
|
||||
max={constraints.max || null}
|
||||
disabled={disabled}
|
||||
/>
|
||||
<span>{error}</span>
|
||||
</>
|
||||
);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue