import { AbsoluteTime, TranslatedString } from "@gnu-taler/taler-util"; import { ComponentChildren, VNode, createContext, h } from "preact"; import { MutableRef, StateUpdater, useEffect, useRef } from "preact/hooks"; export interface FormType { value: MutableRef>; initialValue?: Partial; onUpdate?: StateUpdater; computeFormState?: (v: T) => FormState; } //@ts-ignore export const FormContext = createContext>({}); export type FormState = { [field in keyof T]?: T[field] extends AbsoluteTime ? Partial : T[field] extends Array ? Partial> : T[field] extends object ? FormState : Partial; }; export interface InputFieldState { /* should show the error */ error?: TranslatedString; /* should not allow to edit */ readonly: boolean; /* should show as disable */ disabled: boolean; /* should not show */ hidden: boolean; } export interface InputArrayFieldState extends InputFieldState { elements: FormState[]; } export function FormProvider({ children, initialValue, onUpdate, computeFormState, }: { initialValue?: Partial; onUpdate?: (v: Partial) => void; computeFormState?: (v: T) => FormState; children: ComponentChildren; }): VNode { const value = useRef(initialValue ?? {}); useEffect(() => { return function onUnload() { value.current = initialValue ?? {}; }; }); return (
{children}
); }