5
} from "@patternfly/react-core";
13
} from "react-hook-form";
14
import { FormLabel } from "./FormLabel";
16
export type NumberControlOption = {
21
export type NumberControlProps<
22
T extends FieldValues,
23
P extends FieldPath<T> = FieldPath<T>,
24
> = Omit<NumberInputProps, "name" | "isRequired" | "required"> &
25
UseControllerProps<T, P> & {
29
controller: Omit<ControllerProps, "name" | "render">;
32
export const NumberControl = <
33
T extends FieldValues,
34
P extends FieldPath<T> = FieldPath<T>,
41
}: NumberControlProps<T, P>) => {
44
formState: { errors },
50
isRequired={controller.rules?.required === true}
58
render={({ field }) => {
59
const required = !!controller.rules?.required;
60
const min = controller.rules?.min;
62
field.value === 0 ? controller.defaultValue : field.value;
63
const setValue = (newValue: number) =>
64
field.onChange(min ? Math.max(newValue, Number(min)) : newValue);
72
errors[name] ? ValidatedOptions.error : ValidatedOptions.default
76
max={Number(controller.rules?.max)}
77
onPlus={() => setValue(value + 1)}
78
onMinus={() => setValue(value - 1)}
79
onChange={(event) => {
80
const newValue = Number(event.currentTarget.value);
81
setValue(!isNaN(newValue) ? newValue : controller.defaultValue);