react-formal
Version:
Classy HTML form management for React
32 lines (31 loc) • 1.69 kB
TypeScript
import React from 'react';
import { MapToValue } from './useBinding';
export type Mapper<TOut, TIn = any> = (input: TIn) => TOut;
type BindingValue = Record<PropertyKey, any> | unknown[];
export declare const formGetter: (path: string, model: any) => any;
export declare function formSetter<TValue extends BindingValue>(path: string, value: TValue | undefined, fieldValue: unknown): TValue;
type BindingContextValue = {
getValue<T>(path: Mapper<T> | keyof T): T;
getSchemaForPath: (path: string) => any;
updateBindingValue<T>(path: MapToValue<T>, args: any[]): void;
updateFormValue: (nextFormValue: any) => void;
formValue: any;
};
export declare const BindingContext: React.Context<BindingContextValue>;
export declare const useBindingContext: () => BindingContextValue;
type Setter<TValue extends BindingValue> = (path: string, value: TValue | undefined, fieldValue: unknown) => TValue;
type Props<TValue extends BindingValue> = {
formValue?: TValue;
onChange(value: TValue, paths: string[]): void;
getSchemaForPath: (path: string, value: TValue) => any;
getter?: (path: string, value?: TValue) => any;
setter?: (path: string, value: TValue | undefined, fieldValue: unknown, defaultSetter: Setter<TValue>) => TValue;
};
declare function useFormBindingContext<TValue extends BindingValue>({ formValue, onChange, setter, getter, getSchemaForPath, }: Props<TValue>): {
getValue: (pathOrAccessor: any) => any;
updateBindingValue: (mapValue: any, args: any) => void;
getSchemaForPath: (path: string) => any;
updateFormValue: (nextFormValue: TValue) => void;
formValue: TValue | undefined;
};
export default useFormBindingContext;