From 815cb5a0889a22f70ebac700db7bf7c4d5ffb2c2 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sat, 3 Jul 2021 13:45:27 +0200 Subject: [PATCH] add UIConfigContext & Provider into UIStoreProvider #76 --- .../src/PluginStack/PluginStack.d.ts | 2 ++ .../ui-schema/src/PluginStack/PluginStack.js | 9 +++++- .../ui-schema/src/UIStore/UIStoreProvider.tsx | 30 +++++++++++++++++-- .../src/UIStore/scopeUpdaterInternals.ts | 3 +- .../src/UIStore/scopeUpdaterValidity.ts | 3 +- .../ui-schema/src/UIStore/storeUpdater.ts | 8 ++++- 6 files changed, 49 insertions(+), 6 deletions(-) diff --git a/packages/ui-schema/src/PluginStack/PluginStack.d.ts b/packages/ui-schema/src/PluginStack/PluginStack.d.ts index a46d2cbf..df794ac9 100644 --- a/packages/ui-schema/src/PluginStack/PluginStack.d.ts +++ b/packages/ui-schema/src/PluginStack/PluginStack.d.ts @@ -26,6 +26,8 @@ export type PluginStackProps = AppliedPlu // [key: string]: any } +// - `WP` = extra supported/required widget props +// - `C` = custom `meta context` or additional `config context` export function PluginStack = PluginStackProps>( props: P & WP ): React.ReactElement diff --git a/packages/ui-schema/src/PluginStack/PluginStack.js b/packages/ui-schema/src/PluginStack/PluginStack.js index 21e7e8fb..7a902e12 100644 --- a/packages/ui-schema/src/PluginStack/PluginStack.js +++ b/packages/ui-schema/src/PluginStack/PluginStack.js @@ -3,6 +3,8 @@ import {List} from 'immutable'; import {memo} from '@ui-schema/ui-schema/Utils/memo'; import {useUIMeta} from '@ui-schema/ui-schema/UIMeta/UIMetaProvider'; import {createValidatorErrors} from '@ui-schema/ui-schema/ValidatorErrors'; +import {useUIConfig} from '@ui-schema/ui-schema/UIStore/UIStoreProvider'; +import {useImmutable} from '@ui-schema/ui-schema/Utils/useImmutable'; class PluginStackErrorBoundary extends React.Component { state = { @@ -33,12 +35,15 @@ class PluginStackErrorBoundary extends React.Component { // `withUIMeta` and `mema` are not needed for performance optimizing since `0.3.0` at this position export const PluginStack = (props) => { const {widgets, ...meta} = useUIMeta() + const config = useUIConfig() const { level = 0, parentSchema, storeKeys, schema, widgets: customWidgets, } = props; - const activeWidgets = customWidgets ? customWidgets : widgets + // central reference integrity of `storeKeys` for all plugins and the receiving widget, otherwise `useImmutable` is needed more times, e.g. 3 times in plugins + 1x time in widget + const currentStoreKeys = useImmutable(storeKeys) + const activeWidgets = customWidgets || widgets const isVirtual = Boolean(props.isVirtual || schema?.get('hidden')) let required = List([]); @@ -58,10 +63,12 @@ export const PluginStack = (props) => { > ({}) -export const UIStoreProvider: React.ComponentType> = ({children, ...props}) => { - return +// @ts-ignore +const UIConfigContextObj = React.createContext<{}>({}) + +export const UIConfigProvider: React.ComponentType> = ( + {children, ...props} +) => { + const value = React.useMemo(() => ({...props}), [...Object.values(props)]) + return {children} + +} + +export function UIStoreProvider( + { + children, + showValidity, onChange, store, + ...props + }: React.PropsWithChildren +): React.ReactElement { + return + + {children} + } + export const useUI = (): UIStoreContext => { return React.useContext(UIStoreContextObj) } +export function useUIConfig(): C { + // @ts-ignore + return React.useContext(UIConfigContextObj) +} + export interface WithValue { value: any internalValue: any diff --git a/packages/ui-schema/src/UIStore/scopeUpdaterInternals.ts b/packages/ui-schema/src/UIStore/scopeUpdaterInternals.ts index ff8b52e0..e694afcf 100644 --- a/packages/ui-schema/src/UIStore/scopeUpdaterInternals.ts +++ b/packages/ui-schema/src/UIStore/scopeUpdaterInternals.ts @@ -1,6 +1,7 @@ import { storeBuildScopeTree } from '@ui-schema/ui-schema/UIStore/storeBuildScopeTree' import { updateStoreScope } from '@ui-schema/ui-schema/UIStore/updateStoreScope' -import { addNestKey, ScopeOnChangeHandler } from '@ui-schema/ui-schema' +import { addNestKey } from '@ui-schema/ui-schema/UIStore/UIStore' +import { ScopeOnChangeHandler } from '@ui-schema/ui-schema/UIStore/storeUpdater' export const scopeUpdaterInternals: ScopeOnChangeHandler = (store, storeKeys, oldValue, newValue) => { if (typeof oldValue === 'undefined') { diff --git a/packages/ui-schema/src/UIStore/scopeUpdaterValidity.ts b/packages/ui-schema/src/UIStore/scopeUpdaterValidity.ts index 34633e66..d5adb1ce 100644 --- a/packages/ui-schema/src/UIStore/scopeUpdaterValidity.ts +++ b/packages/ui-schema/src/UIStore/scopeUpdaterValidity.ts @@ -1,4 +1,5 @@ -import { prependKey, ScopeOnChangeHandler } from '@ui-schema/ui-schema/UIStore' +import { prependKey } from '@ui-schema/ui-schema/UIStore/UIStore' +import { ScopeOnChangeHandler } from '@ui-schema/ui-schema/UIStore/storeUpdater' import { updateStoreScope } from '@ui-schema/ui-schema/UIStore/updateStoreScope' import { List } from 'immutable' diff --git a/packages/ui-schema/src/UIStore/storeUpdater.ts b/packages/ui-schema/src/UIStore/storeUpdater.ts index 0170bbe7..d6ec81f8 100644 --- a/packages/ui-schema/src/UIStore/storeUpdater.ts +++ b/packages/ui-schema/src/UIStore/storeUpdater.ts @@ -1,4 +1,10 @@ -import { StoreKeys, prependKey, UIStoreType, addNestKey, UIStoreStateData, UIStoreUpdaterFn, onChangeHandlerGeneric, UIStoreAction, UIStoreUpdaterData } from '@ui-schema/ui-schema/UIStore/UIStore' +import { + StoreKeys, UIStoreType, + prependKey, addNestKey, + UIStoreStateData, + UIStoreUpdaterFn, onChangeHandlerGeneric, + UIStoreAction, UIStoreUpdaterData, +} from '@ui-schema/ui-schema/UIStore/UIStore' import { scopeUpdaterValidity } from '@ui-schema/ui-schema/UIStore/scopeUpdaterValidity' import { scopeUpdaterInternals } from '@ui-schema/ui-schema/UIStore/scopeUpdaterInternals' import { scopeUpdaterValues } from '@ui-schema/ui-schema/UIStore/scopeUpdaterValues'