diff --git a/packages/demo/src/material-ui/material-ui-custom.tsx b/packages/demo/src/material-ui/material-ui-custom.tsx index 8c563028..5022f533 100644 --- a/packages/demo/src/material-ui/material-ui-custom.tsx +++ b/packages/demo/src/material-ui/material-ui-custom.tsx @@ -11,7 +11,7 @@ import { StoreKeys, StoreSchemaType, WidgetProps, UIMetaProvider, UIStoreProvider, useUIMeta, WithValue, extractValue, - WidgetsBindingFactory, WithScalarValue, + WidgetsBindingFactory, WithScalarValue, onChangeHandler, } from '@ui-schema/ui-schema' import { browserT } from '../t' import { UIApiProvider, loadSchemaUIApi } from '@ui-schema/ui-schema/UIApi' @@ -194,7 +194,7 @@ const FreeFormEditor = () => { const showValidity = true const [store, setStore] = React.useState(() => createStore(OrderedMap())) - const onChange = React.useCallback((actions) => { + const onChange: onChangeHandler = React.useCallback((actions) => { setStore(storeUpdater(actions)) }, [setStore]) diff --git a/packages/ui-schema/src/UIStore/UIStoreProvider.tsx b/packages/ui-schema/src/UIStore/UIStoreProvider.tsx index efd6869f..e6f73b25 100644 --- a/packages/ui-schema/src/UIStore/UIStoreProvider.tsx +++ b/packages/ui-schema/src/UIStore/UIStoreProvider.tsx @@ -63,19 +63,19 @@ export interface WithOnChange { export interface WithValue { value: any | undefined internalValue: any | undefined - showValidity: UIStoreContext['showValidity'] + showValidity?: UIStoreContext['showValidity'] onChange: onChangeHandler } export interface WithScalarValue { value: string | number | boolean | undefined | null internalValue: any - showValidity: UIStoreContext['showValidity'] + showValidity?: UIStoreContext['showValidity'] onChange: onChangeHandler } export interface WithValidity { validity: any onChange: onChangeHandler - showValidity: UIStoreContext['showValidity'] + showValidity?: UIStoreContext['showValidity'] } diff --git a/packages/ui-schema/src/UIStoreActions/UIStoreActions.ts b/packages/ui-schema/src/UIStoreActions/UIStoreActions.ts index 80a07d7f..31580239 100644 --- a/packages/ui-schema/src/UIStoreActions/UIStoreActions.ts +++ b/packages/ui-schema/src/UIStoreActions/UIStoreActions.ts @@ -13,49 +13,52 @@ export type UIStoreActionScoped void } -export interface UIStoreAction extends UIStoreActionScoped { +export interface UIStoreAction extends UIStoreActionScoped { type: string schema?: StoreSchemaType required?: boolean } -export type UIStoreActionListItemAdd = UIStoreActionListItemAddWithValue | UIStoreActionListItemAddWithSchema -export interface UIStoreActionListItemAddWithValue extends UIStoreAction { +export interface UIStoreActionListItemAddWithValue extends UIStoreAction { type: 'list-item-add' itemValue: any } -export interface UIStoreActionListItemAddWithSchema extends UIStoreAction { +export interface UIStoreActionListItemAddWithSchema extends UIStoreAction { type: 'list-item-add' schema: StoreSchemaType } -export interface UIStoreActionListItemDelete extends UIStoreAction { +export type UIStoreActionListItemAdd = + UIStoreActionListItemAddWithValue | + UIStoreActionListItemAddWithSchema + +export interface UIStoreActionListItemDelete extends UIStoreAction { type: 'list-item-delete' index: number } -export interface UIStoreActionListItemMove extends UIStoreAction { +export interface UIStoreActionListItemMove extends UIStoreAction { type: 'list-item-move' fromIndex: number toIndex: number } -export interface UIStoreActionUpdate extends UIStoreAction { +export interface UIStoreActionUpdate extends UIStoreAction { type: 'update' updater: UIStoreUpdaterFn } -export interface UIStoreActionSet extends UIStoreAction { +export interface UIStoreActionSet extends UIStoreAction { type: 'set' data: D } -export type UIStoreActions = - UIStoreActionListItemAdd | - UIStoreActionListItemDelete | - UIStoreActionListItemMove | - UIStoreActionUpdate | - UIStoreActionSet +export type UIStoreActions = + UIStoreActionListItemAdd | + UIStoreActionListItemDelete | + UIStoreActionListItemMove | + UIStoreActionUpdate | + UIStoreActionSet diff --git a/packages/ui-schema/src/storeUpdater/storeActionHandler.ts b/packages/ui-schema/src/storeUpdater/storeActionHandler.ts index 20eba9b3..071eaca7 100644 --- a/packages/ui-schema/src/storeUpdater/storeActionHandler.ts +++ b/packages/ui-schema/src/storeUpdater/storeActionHandler.ts @@ -1,8 +1,8 @@ -import { UIStoreUpdaterFn } from '@ui-schema/ui-schema/UIStore' +import { UIStoreType, UIStoreUpdaterFn } from '@ui-schema/ui-schema/UIStore' import { UIStoreActions, UIStoreUpdaterData } from '@ui-schema/ui-schema/UIStoreActions' import { storeActionReducers } from '@ui-schema/ui-schema/storeUpdater/storeActionReducers' /** * @deprecated use `storeActionReducers` instead */ -export const storeActionHandler = storeActionReducers as = UIStoreActions>(action: A) => UIStoreUpdaterFn | D +export const storeActionHandler = storeActionReducers as >(action: A) => UIStoreUpdaterFn | D diff --git a/packages/ui-schema/src/storeUpdater/storeActionReducers.ts b/packages/ui-schema/src/storeUpdater/storeActionReducers.ts index da391eea..3df47afd 100644 --- a/packages/ui-schema/src/storeUpdater/storeActionReducers.ts +++ b/packages/ui-schema/src/storeUpdater/storeActionReducers.ts @@ -1,10 +1,12 @@ -import { UIStoreUpdaterFn } from '@ui-schema/ui-schema/UIStore' +import { UIStoreType, UIStoreUpdaterFn } from '@ui-schema/ui-schema/UIStore' import { List, Map, OrderedMap } from 'immutable' import { moveItem } from '@ui-schema/ui-schema/Utils/moveItem' import { SchemaTypesType } from '@ui-schema/ui-schema/CommonTypings' import { UIStoreActions, UIStoreUpdaterData } from '@ui-schema/ui-schema/UIStoreActions' -export const storeActionReducers = = UIStoreActions>(action: A): UIStoreUpdaterFn | D => { +export const storeActionReducers = = UIStoreActions>( + action: A +): UIStoreUpdaterFn | D => { switch (action.type) { case 'list-item-add': return ({value = List(), internal = Map(), ...r}) => { diff --git a/packages/ui-schema/src/storeUpdater/storeUpdater.ts b/packages/ui-schema/src/storeUpdater/storeUpdater.ts index e476f397..38d40322 100644 --- a/packages/ui-schema/src/storeUpdater/storeUpdater.ts +++ b/packages/ui-schema/src/storeUpdater/storeUpdater.ts @@ -4,18 +4,18 @@ import { UIStoreStateData, UIStoreUpdaterFn, } from '@ui-schema/ui-schema/UIStore' import { scopeUpdaterValues, scopeUpdaterInternals, scopeUpdaterValidity } from '@ui-schema/ui-schema/storeScopeUpdater' -import { UIStoreActions, UIStoreUpdaterData } from '@ui-schema/ui-schema/UIStoreActions' +import { UIStoreAction, UIStoreActions, UIStoreUpdaterData } from '@ui-schema/ui-schema/UIStoreActions' import { storeActionReducers } from '@ui-schema/ui-schema/storeUpdater' // todo: unify this type and the `setter` in `ScopeUpdaterMapType` -export type ScopeOnChangeHandler = ( +export type ScopeOnChangeHandler> = ( store: S, storeKeys: StoreKeys, newValue: any, action?: A | undefined ) => S -export type ScopeUpdaterMapType = { +export type ScopeUpdaterMapType> = { [k in keyof D]: ({ setter: ( store: S, @@ -68,7 +68,7 @@ export const scopeUpdaterMapDefault: ScopeUpdaterMapType = { }, } -export const createStoreUpdater = = ScopeUpdaterMapType>( +export const createStoreUpdater = = UIStoreActions, SM extends ScopeUpdaterMapType = ScopeUpdaterMapType>( actionReducers: (action: A) => UIStoreUpdaterFn | D, scopeUpdaterMap: SM, ) => { @@ -129,8 +129,11 @@ export const createStoreUpdater = ( + = UIStoreActions>( actions: A[] | A ) => { - return createStoreUpdater(storeActionReducers, scopeUpdaterMapDefault)(actions) + return createStoreUpdater( + storeActionReducers as (action: A) => UIStoreUpdaterFn | D, + scopeUpdaterMapDefault as ScopeUpdaterMapType, + )(actions) }