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.

82 lines 2.35 kB
import { List } from 'immutable'; import { useUIMeta } from '@ui-schema/react/UIMeta'; import { useUIConfig, useUIStore } from '@ui-schema/react/UIStore'; import { useImmutable } from '@ui-schema/react/Utils/useImmutable'; import { NextPluginMemo } from './NextPlugin.js'; import { WidgetEngineErrorBoundary } from './WidgetEngineErrorBoundary.js'; import { useUIStoreActions } from '@ui-schema/react/UIStoreActions'; import { jsx as _jsx } from "react/jsx-runtime"; function isRootProps(props) { return props.isRoot === true; } export const WidgetEngine = props => { const { binding, Next, ...meta } = useUIMeta(); const config = useUIConfig(); const { store, showValidity } = useUIStore(); const { onChange } = useUIStoreActions(); const { binding: customBinding, StackWrapper, wrapperProps, isRoot, ...nestedProps } = props; let schema; let parentSchema; let storeKeys; if (isRootProps(props)) { storeKeys = List([]); schema = props.schema; parentSchema = undefined; } else { storeKeys = props.storeKeys || List([]); schema = props.schema; parentSchema = props.parentSchema; } const values = store?.extractValues(storeKeys); const currentStoreKeys = useImmutable(storeKeys); const activeWidgets = customBinding || binding; const isVirtual = Boolean(props.isVirtual || schema?.get('hidden')); if (!Next) { return null; } const stack = _jsx(NextPluginMemo, { ...meta, ...config, ...nestedProps, Next: Next, showValidity: props.showValidity || showValidity, onChange: onChange, ...(values || {}), value: values?.value, internalValue: values?.internalValue, valid: true, isVirtual: isVirtual, binding: activeWidgets, storeKeys: currentStoreKeys, parentSchema: parentSchema, schema: schema }); const wrappedStack = StackWrapper && !isVirtual ? _jsx(StackWrapper, { schema: schema, storeKeys: currentStoreKeys, ...(wrapperProps || {}), children: stack }) : stack; return schema ? activeWidgets?.ErrorFallback ? _jsx(WidgetEngineErrorBoundary, { FallbackComponent: activeWidgets.ErrorFallback, type: schema.get('type'), widget: schema.get('widget'), storeKeys: currentStoreKeys, children: wrappedStack }) : wrappedStack : null; };