@@ -75,6 +75,7 @@ import {
75
75
OwnPropsOfLabel ,
76
76
LabelProps ,
77
77
mapStateToLabelProps ,
78
+ CoreActions ,
78
79
} from '@jsonforms/core' ;
79
80
import debounce from 'lodash/debounce' ;
80
81
import React , {
@@ -87,6 +88,7 @@ import React, {
87
88
useMemo ,
88
89
useReducer ,
89
90
useRef ,
91
+ useState ,
90
92
} from 'react' ;
91
93
92
94
const initialCoreState : JsonFormsCore = {
@@ -126,33 +128,56 @@ const useEffectAfterFirstRender = (
126
128
} , dependencies ) ;
127
129
} ;
128
130
131
+ export interface Middleware {
132
+ (
133
+ state : JsonFormsCore ,
134
+ action : CoreActions ,
135
+ defaultReducer : ( state : JsonFormsCore , action : CoreActions ) => JsonFormsCore
136
+ ) : JsonFormsCore ;
137
+ }
138
+
139
+ const defaultMiddleware : Middleware = ( state , action , defaultReducer ) =>
140
+ defaultReducer ( state , action ) ;
141
+
129
142
export const JsonFormsStateProvider = ( {
130
143
children,
131
144
initState,
132
145
onChange,
146
+ middleware,
133
147
} : any ) => {
134
148
const { data, schema, uischema, ajv, validationMode, additionalErrors } =
135
149
initState . core ;
136
150
137
- const [ core , coreDispatch ] = useReducer ( coreReducer , undefined , ( ) =>
138
- coreReducer (
151
+ const middlewareRef = useRef < Middleware > ( middleware ?? defaultMiddleware ) ;
152
+ middlewareRef . current = middleware ?? defaultMiddleware ;
153
+
154
+ const [ core , setCore ] = useState < JsonFormsCore > ( ( ) =>
155
+ middlewareRef . current (
139
156
initState . core ,
140
157
Actions . init ( data , schema , uischema , {
141
158
ajv,
142
159
validationMode,
143
160
additionalErrors,
144
- } )
161
+ } ) ,
162
+ coreReducer
145
163
)
146
164
) ;
147
- useEffect ( ( ) => {
148
- coreDispatch (
149
- Actions . updateCore ( data , schema , uischema , {
150
- ajv,
151
- validationMode,
152
- additionalErrors,
153
- } )
154
- ) ;
155
- } , [ data , schema , uischema , ajv , validationMode , additionalErrors ] ) ;
165
+
166
+ useEffect (
167
+ ( ) =>
168
+ setCore ( ( currentCore ) =>
169
+ middlewareRef . current (
170
+ currentCore ,
171
+ Actions . updateCore ( data , schema , uischema , {
172
+ ajv,
173
+ validationMode,
174
+ additionalErrors,
175
+ } ) ,
176
+ coreReducer
177
+ )
178
+ ) ,
179
+ [ data , schema , uischema , ajv , validationMode , additionalErrors ]
180
+ ) ;
156
181
157
182
const [ config , configDispatch ] = useReducer ( configReducer , undefined , ( ) =>
158
183
configReducer ( undefined , Actions . setConfig ( initState . config ) )
@@ -185,6 +210,12 @@ export const JsonFormsStateProvider = ({
185
210
initState . i18n ?. translateError ,
186
211
] ) ;
187
212
213
+ const dispatch = useCallback ( ( action : CoreActions ) => {
214
+ setCore ( ( currentCore ) =>
215
+ middlewareRef . current ( currentCore , action , coreReducer )
216
+ ) ;
217
+ } , [ ] ) ;
218
+
188
219
const contextValue = useMemo (
189
220
( ) => ( {
190
221
core,
@@ -194,8 +225,7 @@ export const JsonFormsStateProvider = ({
194
225
uischemas : initState . uischemas ,
195
226
readonly : initState . readonly ,
196
227
i18n : i18n ,
197
- // only core dispatch available
198
- dispatch : coreDispatch ,
228
+ dispatch : dispatch ,
199
229
} ) ,
200
230
[
201
231
core ,
0 commit comments