UNPKG

@ui-schema/react

Version:

Schema-driven UI generator for React using JSON Schema. Build powerful form and interface generators with headless components and hooks.

51 lines (50 loc) 2.78 kB
import { Record, Map, List, OrderedMap, RecordOf } from 'immutable'; import { UIStoreActions, UIStoreUpdaterData } from '@ui-schema/react/UIStoreActions'; import { StoreKeys as ValueStoreKeys } from '@ui-schema/ui-schema/ValueStore'; import { SchemaTypesType } from '@ui-schema/ui-schema/CommonTypings'; import { Validity } from './UIStoreProvider.js'; export type Values<V> = List<V> | string | number | boolean | Map<string, V> | OrderedMap<string, V>; export type ValuesJS = any[] | string | number | boolean | Object; export interface UIStoreStateData<D = any> { values: D; internals: UIStoreInternalsType | undefined; validity: UIStoreValidityType | undefined; meta: Map<string, any>; } export interface UIStoreState<D = any> extends UIStoreStateData<D> { valuesToJS: () => ValuesJS; getValues: () => D; getInternals: () => UIStoreInternalsType | undefined; getValidity: () => UIStoreValidityType | undefined; extractValues: <V>(storeKeys: StoreKeys) => { value: V | undefined; internalValue: UIStoreInternalsType | undefined; }; extractValidity: (storeKeys: StoreKeys) => Validity | undefined; } export type UIStoreTypeFactory<D = any> = Record.Factory<UIStoreState<D>>; export type UIStoreType<D = any> = RecordOf<UIStoreState<D>>; /** * Internals store tree. * * Nested with `.children` and a storeKeys value is scoped to `.self`. */ export type UIStoreInternalsType = Map<string, any>; export type UIStoreValidityType = Map<string, unknown>; export type UIStoreUpdaterFn<D extends UIStoreUpdaterData = UIStoreUpdaterData> = (data: D) => D; export type onChangeHandler<A = UIStoreActions> = (actions: A[] | A) => void; export type StoreKeyType = string | number; export type StoreKeys = ValueStoreKeys; export declare const UIStore: UIStoreTypeFactory; export declare const createStore: <D = any>(values: D) => UIStoreType<D>; /** * @todo support multiple types #68 * @todo include non-evaluating default handling here #118 ? * - would rely on the whole schema * - would work only for basic `default` without conditionals * - could reuse the resource system for walking the schema, instead of the validator */ export declare const createEmptyStore: (type?: SchemaTypesType) => UIStoreType; export declare const prependKey: <O extends StoreKeyType = StoreKeyType, S extends ValueStoreKeys<StoreKeyType> | StoreKeyType[] = ValueStoreKeys<StoreKeyType> | StoreKeyType[]>(storeKeys: S, key: O) => S; export declare const shouldDeleteOnEmpty: (value: any, force: boolean | undefined, type: string | string[] | List<string> | undefined) => boolean; export declare const addNestKey: (storeKeysNestedKey: string, storeKeys: (string | number)[] | List<string | number>) => List<string | number>;