UNPKG

@vaadin/hilla-react-form

Version:

Hilla form utils for React

53 lines (52 loc) 2.19 kB
import { type AbstractModel, type BinderConfiguration, type DetachedModelConstructor, type Validator, type Value, type ValueError, type ArrayModel, type ArrayItemModel } from "@vaadin/hilla-lit-form"; export type FieldDirectiveResult = Readonly<{ name: string onBlur(): void onChange(): void onInput(): void ref(element: HTMLElement | null): void }>; export type FieldDirective = (model: AbstractModel) => FieldDirectiveResult; export type UseFormPartResult<M extends AbstractModel> = Readonly<{ defaultValue?: Value<M> dirty: boolean errors: readonly ValueError[] invalid: boolean model: M name: string field: FieldDirective ownErrors: ReadonlyArray<ValueError<Value<M>>> required: boolean validators: ReadonlyArray<Validator<Value<M>>> value?: Value<M> visited: boolean addValidator(validator: Validator<Value<M>>): void setValidators(validators: ReadonlyArray<Validator<Value<M>>>): void setValue(value: Value<M> | undefined): void setVisited(visited: boolean): void validate(): Promise<readonly ValueError[]> }>; export type UseFormResult<M extends AbstractModel> = Omit<UseFormPartResult<M>, "setValue" | "value"> & Readonly<{ value: Value<M> submitting: boolean setDefaultValue(value: Value<M>): void setValue(value: Value<M>): void submit(): Promise<Value<M> | undefined | void> reset(): void clear(): void read(value: Value<M> | null | undefined): void update(): void }>; export type UseFormArrayPartResult<M extends ArrayModel> = Omit<UseFormPartResult<M>, "field"> & { items: ReadonlyArray<ArrayItemModel<M>> }; export declare function useForm<M extends AbstractModel>(Model: DetachedModelConstructor<M>, config?: BinderConfiguration<Value<M>>): UseFormResult<M>; export declare function useFormPart<M extends AbstractModel>(model: M): UseFormPartResult<M>; /** * Hook to access an array model part of a form. It provides the same API as `useFormPart`, * but adds an `items` property that allows to iterate over the items in form of an array of models. * * @param model - The array model to access * @returns The array model part of the form */ export declare function useFormArrayPart<M extends ArrayModel>(model: M): UseFormArrayPartResult<M>;