1+ import { MuiBinding } from '@ui-schema/ds-material'
2+ import { requiredPlugin , validatorPlugin } from '@ui-schema/json-schema'
3+ import { DefaultHandler , ValidityReporter , WidgetProps } from '@ui-schema/react'
4+ import { schemaPluginsAdapterBuilder } from '@ui-schema/react/SchemaPluginsAdapter'
5+ import { StoreKeyType } from '@ui-schema/ui-schema/ValueStore'
6+ import { Map } from 'immutable'
17import React from 'react'
2- import {
3- WidgetsBindingFactory ,
4- WidgetProps , WithScalarValue , memo , WithValue , StoreKeyType ,
5- } from '@ui-schema/ui-schema'
6- import { MuiWidgetsBindingCustom , MuiWidgetsBindingTypes , widgets } from '@ui-schema/ds-material/widgetsBinding'
8+ import { baseComponents , typeWidgets } from '@ui-schema/ds-material/BindingDefault'
9+ import { bindingExtended } from '@ui-schema/ds-material/BindingExtended'
10+ import { SchemaGridHandler } from '@ui-schema/ds-material/Grid'
711import Button from '@mui/material/Button'
812import { json } from '@codemirror/lang-json'
913import { javascript } from '@codemirror/lang-javascript'
1014import { html } from '@codemirror/lang-html'
1115import { css } from '@codemirror/lang-css'
12- import { extractValue } from '@ui-schema/ui-schema/UIStore'
1316import { WidgetCode } from '@ui-schema/material-code'
1417import { WidgetCodeSelectable } from '@ui-schema/material-code/WidgetCodeSelectable'
1518import { CustomCodeMirror } from './CustomCodeMirror'
1619
17- export const CustomWidgetCode : React . ComponentType < WidgetProps & WithScalarValue > = ( props ) => {
20+ export const CustomWidgetCode : React . ComponentType < WidgetProps > = ( props ) => {
1821 const format = props . schema . get ( 'format' )
1922 // map the to-be-supported CodeMirror language, or add other extensions
2023 const extensions = React . useMemo ( ( ) => [
@@ -33,7 +36,7 @@ export const CustomWidgetCode: React.ComponentType<WidgetProps & WithScalarValue
3336 />
3437}
3538
36- const CustomWidgetCodeSelectableBase : React . ComponentType < WidgetProps & WithValue > = (
39+ const CustomWidgetCodeSelectable : React . ComponentType < WidgetProps > = (
3740 { value, ...props } ,
3841) => {
3942 const { schema, onChange, storeKeys} = props
@@ -43,8 +46,9 @@ const CustomWidgetCodeSelectableBase: React.ComponentType<WidgetProps & WithValu
4346 // - for objects: {lang, code}
4447 const formatKey : StoreKeyType = valueType === 'array' ? 0 : 'lang'
4548 const valueKey : StoreKeyType = valueType === 'array' ? 1 : 'code'
46- const format = value ?. get ( formatKey ) as string | undefined || schema . get ( 'formatDefault' ) as string | undefined
47- const codeValue = value ?. get ( valueKey ) as string | undefined
49+ const valueMap = Map . isMap ( value ) ? value : undefined
50+ const format = valueMap ?. get ( formatKey ) as string | undefined || schema . get ( 'formatDefault' ) as string | undefined
51+ const codeValue = valueMap ?. get ( valueKey ) as string | undefined
4852
4953 // map the to-be-supported CodeMirror language, or add other extensions
5054 const extensions = React . useMemo ( ( ) => [
@@ -88,16 +92,34 @@ const CustomWidgetCodeSelectableBase: React.ComponentType<WidgetProps & WithValu
8892 }
8993 />
9094}
91- const CustomWidgetCodeSelectable = extractValue ( memo ( CustomWidgetCodeSelectableBase ) )
9295
93- export type CustomWidgetsBinding = WidgetsBindingFactory < { } , MuiWidgetsBindingTypes < { } > , MuiWidgetsBindingCustom < { } > >
96+ export const customBinding : MuiBinding = {
97+ ...baseComponents ,
98+
99+ // Widget mapping by schema type or custom ID.
100+ widgets : {
101+ ...typeWidgets ,
102+ ...bindingExtended ,
94103
95- export const customWidgets : CustomWidgetsBinding = {
96- ...widgets ,
97- types : widgets . types ,
98- custom : {
99- ...widgets . custom ,
100104 Code : CustomWidgetCode ,
101105 CodeSelectable : CustomWidgetCodeSelectable ,
102106 } ,
107+
108+ // Plugins that wrap each rendered widget.
109+ widgetPlugins : [
110+ DefaultHandler , // handles `default` keyword
111+
112+ // Runs SchemaPlugins, connects to SchemaResource (if enabled)
113+ schemaPluginsAdapterBuilder ( [
114+ // runs `validate` and related schema postprocessing
115+ validatorPlugin ,
116+
117+ // injects the `required` prop
118+ requiredPlugin ,
119+ ] ) ,
120+
121+ SchemaGridHandler , // MUI v5/6 Grid item
122+
123+ ValidityReporter , // keeps `valid`/`errors` in sync with `store`
124+ ] ,
103125}
0 commit comments