diff options
Diffstat (limited to 'packages/aml-backoffice-ui/src/handlers/FormProvider.tsx')
-rw-r--r-- | packages/aml-backoffice-ui/src/handlers/FormProvider.tsx | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/packages/aml-backoffice-ui/src/handlers/FormProvider.tsx b/packages/aml-backoffice-ui/src/handlers/FormProvider.tsx index 3da2a4f07..310954bd0 100644 --- a/packages/aml-backoffice-ui/src/handlers/FormProvider.tsx +++ b/packages/aml-backoffice-ui/src/handlers/FormProvider.tsx @@ -7,14 +7,13 @@ import { ComponentChildren, VNode, createContext, h } from "preact"; import { MutableRef, StateUpdater, - useEffect, - useRef, - useState, + useState } from "preact/hooks"; export interface FormType<T> { value: MutableRef<Partial<T>>; initialValue?: Partial<T>; + readOnly?: boolean; onUpdate?: StateUpdater<T>; computeFormState?: (v: T) => FormState<T>; } @@ -24,14 +23,14 @@ export const FormContext = createContext<FormType<any>>({}); export type FormState<T> = { [field in keyof T]?: T[field] extends AbsoluteTime - ? Partial<InputFieldState> - : T[field] extends AmountJson - ? Partial<InputFieldState> - : T[field] extends Array<infer P> - ? Partial<InputArrayFieldState<P>> - : T[field] extends (object | undefined) - ? FormState<T[field]> - : Partial<InputFieldState>; + ? Partial<InputFieldState> + : T[field] extends AmountJson + ? Partial<InputFieldState> + : T[field] extends Array<infer P> + ? Partial<InputArrayFieldState<P>> + : T[field] extends (object | undefined) + ? FormState<T[field]> + : Partial<InputFieldState>; }; export interface InputFieldState { @@ -55,11 +54,13 @@ export function FormProvider<T>({ onUpdate: notify, onSubmit, computeFormState, + readOnly, }: { initialValue?: Partial<T>; onUpdate?: (v: Partial<T>) => void; onSubmit?: (v: Partial<T>, s: FormState<T> | undefined) => void; computeFormState?: (v: Partial<T>) => FormState<T>; + readOnly?: boolean; children: ComponentChildren; }): VNode { // const value = useRef(initialValue ?? {}); @@ -79,7 +80,7 @@ export function FormProvider<T>({ }; return ( <FormContext.Provider - value={{ initialValue, value, onUpdate, computeFormState }} + value={{ initialValue, value, onUpdate, computeFormState, readOnly }} > <form onSubmit={(e) => { |