-
Notifications
You must be signed in to change notification settings - Fork 376
/
JsonFormsContext.tsx
870 lines (779 loc) · 25.9 KB
/
JsonFormsContext.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
/*
The MIT License
Copyright (c) 2017-2019 EclipseSource Munich
https://github.com/eclipsesource/jsonforms
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import {
Actions,
ArrayControlProps,
ArrayLayoutProps,
CellProps,
CombinatorRendererProps,
ControlProps,
defaultMapStateToEnumCellProps,
DispatchCellProps,
DispatchPropsOfControl,
EnumCellProps,
JsonFormsCore,
JsonFormsProps,
JsonFormsSubStates,
LayoutProps,
OwnPropsOfCell,
OwnPropsOfControl,
OwnPropsOfEnum,
OwnPropsOfEnumCell,
OwnPropsOfJsonFormsRenderer,
OwnPropsOfLayout,
OwnPropsOfMasterListItem,
StatePropsOfControlWithDetail,
StatePropsOfMasterItem,
configReducer,
coreReducer,
mapStateToAllOfProps,
mapStateToAnyOfProps,
mapStateToArrayControlProps,
mapStateToArrayLayoutProps,
mapStateToCellProps,
mapStateToControlProps,
mapStateToControlWithDetailProps,
mapStateToDispatchCellProps,
mapStateToEnumControlProps,
mapStateToJsonFormsRendererProps,
mapStateToLayoutProps,
mapStateToMasterListItemProps,
mapStateToOneOfProps,
mapStateToOneOfEnumControlProps,
mapStateToOneOfEnumCellProps,
mapDispatchToMultiEnumProps,
mapStateToMultiEnumControlProps,
DispatchPropsOfMultiEnumControl,
mapDispatchToControlProps,
mapDispatchToArrayControlProps,
i18nReducer,
Translator,
defaultJsonFormsI18nState,
OwnPropsOfLabel,
LabelProps,
mapStateToLabelProps,
} from '@jsonforms/core';
import debounce from 'lodash/debounce';
import React, {
ComponentType,
Dispatch,
ReducerAction,
useCallback,
useContext,
useEffect,
useMemo,
useReducer,
useRef,
} from 'react';
const initialCoreState: JsonFormsCore = {
data: {},
schema: {},
uischema: undefined,
errors: [],
additionalErrors: [],
validator: undefined,
ajv: undefined,
};
export interface JsonFormsStateContext extends JsonFormsSubStates {
dispatch?: Dispatch<ReducerAction<typeof coreReducer>>;
}
export const JsonFormsContext = React.createContext<JsonFormsStateContext>({
core: initialCoreState,
renderers: [],
});
/**
* Hook similar to `useEffect` with the difference that the effect
* is only executed from the second call onwards.
*/
const useEffectAfterFirstRender = (
effect: () => void,
dependencies: Array<any>
) => {
const firstExecution = useRef(true);
useEffect(() => {
if (firstExecution.current) {
firstExecution.current = false;
return;
}
effect();
}, dependencies);
};
export const JsonFormsStateProvider = ({
children,
initState,
onChange,
}: any) => {
const { data, schema, uischema, ajv, validationMode, additionalErrors } =
initState.core;
const [core, coreDispatch] = useReducer(coreReducer, undefined, () =>
coreReducer(
initState.core,
Actions.init(data, schema, uischema, {
ajv,
validationMode,
additionalErrors,
})
)
);
useEffect(() => {
coreDispatch(
Actions.updateCore(data, schema, uischema, {
ajv,
validationMode,
additionalErrors,
})
);
}, [data, schema, uischema, ajv, validationMode, additionalErrors]);
const [config, configDispatch] = useReducer(configReducer, undefined, () =>
configReducer(undefined, Actions.setConfig(initState.config))
);
useEffectAfterFirstRender(() => {
configDispatch(Actions.setConfig(initState.config));
}, [initState.config]);
const [i18n, i18nDispatch] = useReducer(i18nReducer, undefined, () =>
i18nReducer(
initState.i18n,
Actions.updateI18n(
initState.i18n?.locale,
initState.i18n?.translate,
initState.i18n?.translateError
)
)
);
useEffect(() => {
i18nDispatch(
Actions.updateI18n(
initState.i18n?.locale,
initState.i18n?.translate,
initState.i18n?.translateError
)
);
}, [
initState.i18n?.locale,
initState.i18n?.translate,
initState.i18n?.translateError,
]);
const contextValue = useMemo(
() => ({
core,
renderers: initState.renderers,
cells: initState.cells,
config: config,
uischemas: initState.uischemas,
readonly: initState.readonly,
i18n: i18n,
// only core dispatch available
dispatch: coreDispatch,
}),
[
core,
initState.renderers,
initState.cells,
config,
initState.uischemas,
initState.readonly,
i18n,
]
);
const onChangeRef = useRef(onChange);
useEffect(() => {
onChangeRef.current = onChange;
}, [onChange]);
/**
* A common pattern for users of JSON Forms is to feed back the data which is emitted by
* JSON Forms to JSON Forms ('controlled style').
*
* Every time this happens, we dispatch the 'updateCore' action which will be a no-op when
* the data handed over is the one which was just recently emitted. This allows us to skip
* rerendering for all normal cases of use.
*
* However there can be extreme use cases, for example when using Chrome Auto-fill for forms,
* which can cause JSON Forms to emit multiple change events before the parent component is
* rerendered. Therefore not the very recent data, but the previous data is fed back to
* JSON Forms first. JSON Forms recognizes that this is not the very recent data and will
* validate, rerender and emit a change event again. This can then lead to data loss or even
* an endless rerender loop, depending on the emitted events chain.
*
* To handle these edge cases in which many change events are sent in an extremely short amount
* of time we debounce them over a short amount of time. 10ms was chosen as this worked well
* even on low-end mobile device settings in the Chrome simulator.
*/
const debouncedEmit = useCallback(
debounce((...args: any[]) => onChangeRef.current?.(...args), 10),
[]
);
useEffect(() => {
debouncedEmit({ data: core.data, errors: core.errors });
}, [core.data, core.errors]);
return (
<JsonFormsContext.Provider value={contextValue}>
{children}
</JsonFormsContext.Provider>
);
};
export const useJsonForms = (): JsonFormsStateContext =>
useContext(JsonFormsContext);
export interface JsonFormsReduxContextProps extends JsonFormsSubStates {
children: any;
dispatch: Dispatch<ReducerAction<any>>;
}
export const ctxToArrayLayoutProps = (
ctx: JsonFormsStateContext,
props: OwnPropsOfControl
) => mapStateToArrayLayoutProps({ jsonforms: { ...ctx } }, props);
export const ctxToArrayControlProps = (
ctx: JsonFormsStateContext,
props: OwnPropsOfControl
) => mapStateToArrayControlProps({ jsonforms: { ...ctx } }, props);
export const ctxToLayoutProps = (
ctx: JsonFormsStateContext,
props: OwnPropsOfLayout
): LayoutProps => mapStateToLayoutProps({ jsonforms: { ...ctx } }, props);
export const ctxToControlProps = (
ctx: JsonFormsStateContext,
props: OwnPropsOfControl
) => mapStateToControlProps({ jsonforms: { ...ctx } }, props);
export const ctxToEnumControlProps = (
ctx: JsonFormsStateContext,
props: OwnPropsOfEnum
) => {
const enumProps = mapStateToEnumControlProps(
{ jsonforms: { ...ctx } },
props
);
/**
* Make sure, that options are memoized as otherwise the component will rerender for every change,
* as the options array is recreated every time.
*/
const options = useMemo(
() => enumProps.options,
[props.options, enumProps.schema, ctx.i18n?.translate]
);
return { ...enumProps, options };
};
export const ctxToOneOfEnumControlProps = (
ctx: JsonFormsStateContext,
props: OwnPropsOfControl & OwnPropsOfEnum
) => {
const enumProps = mapStateToOneOfEnumControlProps(
{ jsonforms: { ...ctx } },
props
);
/**
* Make sure, that options are memoized as otherwise the component will rerender for every change,
* as the options array is recreated every time.
*/
const options = useMemo(
() => enumProps.options,
[props.options, enumProps.schema, ctx.i18n?.translate]
);
return { ...enumProps, options };
};
export const ctxToMultiEnumControlProps = (
ctx: JsonFormsStateContext,
props: OwnPropsOfControl
) => {
const enumProps = mapStateToMultiEnumControlProps(
{ jsonforms: { ...ctx } },
props
);
/**
* Make sure, that options are memoized as otherwise the component will rerender for every change,
* as the options array is recreated every time.
*/
const options = useMemo(
() => enumProps.options,
[enumProps.schema, ctx.i18n?.translate]
);
return { ...enumProps, options };
};
export const ctxToControlWithDetailProps = (
ctx: JsonFormsStateContext,
props: OwnPropsOfControl
): StatePropsOfControlWithDetail =>
mapStateToControlWithDetailProps({ jsonforms: { ...ctx } }, props);
export const ctxToAllOfProps = (
ctx: JsonFormsStateContext,
ownProps: OwnPropsOfControl
) => {
const props = mapStateToAllOfProps({ jsonforms: { ...ctx } }, ownProps);
return {
...props,
};
};
export const ctxDispatchToControlProps = (
dispatch: Dispatch<ReducerAction<any>>
): DispatchPropsOfControl =>
useMemo(() => mapDispatchToControlProps(dispatch as any), [dispatch]);
// context mappers
export const ctxToAnyOfProps = (
ctx: JsonFormsStateContext,
ownProps: OwnPropsOfControl
): CombinatorRendererProps => {
const props = mapStateToAnyOfProps({ jsonforms: { ...ctx } }, ownProps);
const dispatchProps = ctxDispatchToControlProps(ctx.dispatch);
return {
...props,
...dispatchProps,
};
};
export const ctxToOneOfProps = (
ctx: JsonFormsStateContext,
ownProps: OwnPropsOfControl
): CombinatorRendererProps => {
const props = mapStateToOneOfProps({ jsonforms: { ...ctx } }, ownProps);
const dispatchProps = ctxDispatchToControlProps(ctx.dispatch);
return {
...props,
...dispatchProps,
};
};
export const ctxToJsonFormsRendererProps = (
ctx: JsonFormsStateContext,
ownProps: OwnPropsOfJsonFormsRenderer
) => mapStateToJsonFormsRendererProps({ jsonforms: { ...ctx } }, ownProps);
export const ctxDispatchToArrayControlProps = (
dispatch: Dispatch<ReducerAction<any>>
) => ({
...ctxDispatchToControlProps(dispatch),
...useMemo(() => mapDispatchToArrayControlProps(dispatch as any), [dispatch]),
});
export const ctxToMasterListItemProps = (
ctx: JsonFormsStateContext,
ownProps: OwnPropsOfMasterListItem
) => mapStateToMasterListItemProps({ jsonforms: { ...ctx } }, ownProps);
export const ctxToCellProps = (
ctx: JsonFormsStateContext,
ownProps: OwnPropsOfCell
) => {
return mapStateToCellProps({ jsonforms: { ...ctx } }, ownProps);
};
export const ctxToEnumCellProps = (
ctx: JsonFormsStateContext,
ownProps: EnumCellProps
) => {
const cellProps = defaultMapStateToEnumCellProps(
{ jsonforms: { ...ctx } },
ownProps
);
/**
* Make sure, that options are memoized as otherwise the cell will rerender for every change,
* as the options array is recreated every time.
*/
const options = useMemo(
() => cellProps.options,
[ownProps.options, cellProps.schema, ctx.i18n?.translate]
);
return { ...cellProps, options };
};
export const ctxToOneOfEnumCellProps = (
ctx: JsonFormsStateContext,
props: OwnPropsOfEnumCell
) => {
const enumCellProps = mapStateToOneOfEnumCellProps(
{ jsonforms: { ...ctx } },
props
);
/**
* Make sure, that options are memoized as otherwise the cell will rerender for every change,
* as the options array is recreated every time.
*/
const options = useMemo(
() => enumCellProps.options,
[props.options, enumCellProps.schema, ctx.i18n?.translate]
);
return { ...enumCellProps, options };
};
export const ctxToDispatchCellProps = (
ctx: JsonFormsStateContext,
ownProps: OwnPropsOfCell
) => {
return mapStateToDispatchCellProps({ jsonforms: { ...ctx } }, ownProps);
};
export const ctxDispatchToMultiEnumProps = (
dispatch: Dispatch<ReducerAction<any>>
) => ({
...ctxDispatchToControlProps(dispatch),
...useMemo(() => mapDispatchToMultiEnumProps(dispatch as any), [dispatch]),
});
export const ctxToLabelProps = (
ctx: JsonFormsStateContext,
ownProps: OwnPropsOfLabel
) => {
return mapStateToLabelProps({ jsonforms: { ...ctx } }, ownProps);
};
// --
// HOCs utils
interface WithContext {
ctx: JsonFormsStateContext;
}
export const withJsonFormsContext = (
Component: ComponentType<WithContext & any>
): ComponentType<any> =>
function WithJsonFormsContext(props: any) {
const ctx = useJsonForms();
return <Component ctx={ctx} props={props} />;
};
export const withContextToJsonFormsRendererProps = (
Component: ComponentType<JsonFormsProps>
): ComponentType<OwnPropsOfJsonFormsRenderer> =>
function WithContextToJsonFormsRendererProps({
ctx,
props,
}: JsonFormsStateContext & JsonFormsProps) {
const contextProps = ctxToJsonFormsRendererProps(ctx, props);
return <Component {...props} {...contextProps} />;
};
const withContextToControlProps = (
Component: ComponentType<ControlProps>
): ComponentType<OwnPropsOfControl> =>
function WithContextToControlProps({
ctx,
props,
}: JsonFormsStateContext & ControlProps) {
const controlProps = ctxToControlProps(ctx, props);
const dispatchProps = ctxDispatchToControlProps(ctx.dispatch);
return <Component {...props} {...controlProps} {...dispatchProps} />;
};
const withContextToLayoutProps = (
Component: ComponentType<LayoutProps>
): ComponentType<OwnPropsOfJsonFormsRenderer> =>
function WithContextToLayoutProps({
ctx,
props,
}: JsonFormsStateContext & LayoutProps) {
const layoutProps = ctxToLayoutProps(ctx, props);
return <Component {...props} {...layoutProps} />;
};
const withContextToOneOfProps = (
Component: ComponentType<CombinatorRendererProps>
): ComponentType<OwnPropsOfControl> =>
function WithContextToOneOfProps({
ctx,
props,
}: JsonFormsStateContext & CombinatorRendererProps) {
const oneOfProps = ctxToOneOfProps(ctx, props);
const dispatchProps = ctxDispatchToControlProps(ctx.dispatch);
return <Component {...props} {...oneOfProps} {...dispatchProps} />;
};
const withContextToAnyOfProps = (
Component: ComponentType<CombinatorRendererProps>
): ComponentType<OwnPropsOfControl> =>
function WithContextToAnyOfProps({
ctx,
props,
}: JsonFormsStateContext & CombinatorRendererProps) {
const oneOfProps = ctxToAnyOfProps(ctx, props);
const dispatchProps = ctxDispatchToControlProps(ctx.dispatch);
return <Component {...props} {...oneOfProps} {...dispatchProps} />;
};
const withContextToAllOfProps = (
Component: ComponentType<CombinatorRendererProps>
): ComponentType<OwnPropsOfControl> =>
function WithContextToAllOfProps({
ctx,
props,
}: JsonFormsStateContext & CombinatorRendererProps) {
const allOfProps = ctxToAllOfProps(ctx, props);
const dispatchProps = ctxDispatchToControlProps(ctx.dispatch);
return <Component {...props} {...allOfProps} {...dispatchProps} />;
};
const withContextToDetailProps = (
Component: ComponentType<StatePropsOfControlWithDetail>
): ComponentType<OwnPropsOfControl> =>
function WithContextToDetailProps({
ctx,
props,
}: JsonFormsStateContext & StatePropsOfControlWithDetail) {
const detailProps = ctxToControlWithDetailProps(ctx, props);
return <Component {...props} {...detailProps} />;
};
const withContextToArrayLayoutProps = (
Component: ComponentType<ArrayLayoutProps>
): ComponentType<OwnPropsOfControl> =>
function WithContextToArrayLayoutProps({
ctx,
props,
}: JsonFormsStateContext & ArrayLayoutProps) {
const arrayLayoutProps = ctxToArrayLayoutProps(ctx, props);
const dispatchProps = ctxDispatchToArrayControlProps(ctx.dispatch);
return <Component {...props} {...arrayLayoutProps} {...dispatchProps} />;
};
const withContextToArrayControlProps = (
Component: ComponentType<ArrayControlProps>
): ComponentType<OwnPropsOfControl> =>
function WithContextToArrayControlProps({
ctx,
props,
}: JsonFormsStateContext & ArrayControlProps) {
const stateProps = ctxToArrayControlProps(ctx, props);
const dispatchProps = ctxDispatchToArrayControlProps(ctx.dispatch);
return <Component {...props} {...stateProps} {...dispatchProps} />;
};
const withContextToMasterListItemProps = (
Component: ComponentType<StatePropsOfMasterItem>
): ComponentType<OwnPropsOfMasterListItem> =>
function WithContextToMasterListItemProps({
ctx,
props,
}: JsonFormsStateContext & StatePropsOfMasterItem) {
const stateProps = ctxToMasterListItemProps(ctx, props);
return <Component {...props} {...stateProps} />;
};
const withContextToCellProps = (
Component: ComponentType<CellProps>
): ComponentType<OwnPropsOfCell> =>
function WithContextToCellProps({
ctx,
props,
}: JsonFormsStateContext & CellProps) {
const cellProps = ctxToCellProps(ctx, props);
const dispatchProps = ctxDispatchToControlProps(ctx.dispatch);
return <Component {...props} {...dispatchProps} {...cellProps} />;
};
const withContextToDispatchCellProps = (
Component: ComponentType<DispatchCellProps>
): ComponentType<OwnPropsOfCell> =>
function WithContextToDispatchCellProps({
ctx,
props,
}: JsonFormsStateContext & CellProps) {
const cellProps = ctxToDispatchCellProps(ctx, props);
const dispatchProps = ctxDispatchToControlProps(ctx.dispatch);
return <Component {...props} {...dispatchProps} {...cellProps} />;
};
const withContextToEnumCellProps = (
Component: ComponentType<EnumCellProps>
): ComponentType<OwnPropsOfEnumCell> =>
function WithContextToEnumCellProps({
ctx,
props,
}: JsonFormsStateContext & EnumCellProps) {
const cellProps = ctxToEnumCellProps(ctx, props);
const dispatchProps = ctxDispatchToControlProps(ctx.dispatch);
return <Component {...props} {...dispatchProps} {...cellProps} />;
};
const withContextToEnumProps = (
Component: ComponentType<ControlProps & OwnPropsOfEnum>
): ComponentType<OwnPropsOfControl & OwnPropsOfEnum> =>
function WithContextToEnumProps({
ctx,
props,
}: JsonFormsStateContext & ControlProps & OwnPropsOfEnum) {
const stateProps = ctxToEnumControlProps(ctx, props);
const dispatchProps = ctxDispatchToControlProps(ctx.dispatch);
return <Component {...props} {...dispatchProps} {...stateProps} />;
};
const withContextToOneOfEnumCellProps = (
Component: ComponentType<EnumCellProps>
): ComponentType<OwnPropsOfEnumCell> =>
function WithContextToOneOfEnumCellProps({
ctx,
props,
}: JsonFormsStateContext & EnumCellProps) {
const cellProps = ctxToOneOfEnumCellProps(ctx, props);
const dispatchProps = ctxDispatchToControlProps(ctx.dispatch);
return <Component {...props} {...dispatchProps} {...cellProps} />;
};
const withContextToOneOfEnumProps = (
Component: ComponentType<ControlProps & OwnPropsOfEnum>
): ComponentType<OwnPropsOfControl & OwnPropsOfEnum> =>
function WithContextToOneOfEnumProps({
ctx,
props,
}: JsonFormsStateContext & ControlProps & OwnPropsOfEnum) {
const stateProps = ctxToOneOfEnumControlProps(ctx, props);
const dispatchProps = ctxDispatchToControlProps(ctx.dispatch);
return <Component {...props} {...dispatchProps} {...stateProps} />;
};
const withContextToMultiEnumProps = (
Component: ComponentType<ControlProps & OwnPropsOfEnum>
): ComponentType<OwnPropsOfControl & OwnPropsOfEnum> =>
function WithContextToMultiEnumProps({
ctx,
props,
}: JsonFormsStateContext & ControlProps & OwnPropsOfEnum) {
const stateProps = ctxToMultiEnumControlProps(ctx, props);
const dispatchProps = ctxDispatchToMultiEnumProps(ctx.dispatch);
return <Component {...props} {...dispatchProps} {...stateProps} />;
};
const withContextToLabelProps = (
Component: ComponentType<LabelProps>
): ComponentType<OwnPropsOfLabel> =>
function WithContextToLabelProps({
ctx,
props,
}: JsonFormsStateContext & LabelProps & OwnPropsOfLabel) {
const stateProps = ctxToLabelProps(ctx, props);
return <Component {...props} {...stateProps} />;
};
// --
// top level HOCs --
export const withJsonFormsRendererProps = (
Component: ComponentType<JsonFormsProps>,
memoize = true
): ComponentType<OwnPropsOfJsonFormsRenderer> =>
withJsonFormsContext(
withContextToJsonFormsRendererProps(
memoize ? React.memo(Component) : Component
)
);
export const withJsonFormsControlProps = (
Component: ComponentType<ControlProps>,
memoize = true
): ComponentType<OwnPropsOfControl> =>
withJsonFormsContext(
withContextToControlProps(memoize ? React.memo(Component) : Component)
);
export const withJsonFormsLayoutProps = <T extends LayoutProps>(
Component: ComponentType<T>,
memoize = true
): ComponentType<T & OwnPropsOfLayout> =>
withJsonFormsContext(
withContextToLayoutProps(memoize ? React.memo(Component) : Component)
);
export const withJsonFormsOneOfProps = (
Component: ComponentType<CombinatorRendererProps>,
memoize = true
): ComponentType<OwnPropsOfControl> =>
withJsonFormsContext(
withContextToOneOfProps(memoize ? React.memo(Component) : Component)
);
export const withJsonFormsAnyOfProps = (
Component: ComponentType<CombinatorRendererProps>,
memoize = true
): ComponentType<OwnPropsOfControl> =>
withJsonFormsContext(
withContextToAnyOfProps(memoize ? React.memo(Component) : Component)
);
export const withJsonFormsAllOfProps = (
Component: ComponentType<CombinatorRendererProps>,
memoize = true
): ComponentType<OwnPropsOfControl> =>
withJsonFormsContext(
withContextToAllOfProps(memoize ? React.memo(Component) : Component)
);
export const withJsonFormsDetailProps = (
Component: ComponentType<StatePropsOfControlWithDetail>,
memoize = true
): ComponentType<OwnPropsOfControl> =>
withJsonFormsContext(
withContextToDetailProps(memoize ? React.memo(Component) : Component)
);
export const withJsonFormsArrayLayoutProps = (
Component: ComponentType<ArrayLayoutProps>,
memoize = true
): ComponentType<OwnPropsOfControl> =>
withJsonFormsContext(
withContextToArrayLayoutProps(memoize ? React.memo(Component) : Component)
);
export const withJsonFormsArrayControlProps = (
Component: ComponentType<ArrayControlProps>,
memoize = true
): ComponentType<OwnPropsOfControl> =>
withJsonFormsContext(
withContextToArrayControlProps(memoize ? React.memo(Component) : Component)
);
export const withJsonFormsMasterListItemProps = (
Component: ComponentType<StatePropsOfMasterItem>,
memoize = true
): ComponentType<OwnPropsOfMasterListItem> =>
withJsonFormsContext(
withContextToMasterListItemProps(
memoize ? React.memo(Component) : Component
)
);
export const withJsonFormsCellProps = (
Component: ComponentType<CellProps>,
memoize = true
): ComponentType<OwnPropsOfCell> =>
withJsonFormsContext(
withContextToCellProps(memoize ? React.memo(Component) : Component)
);
export const withJsonFormsDispatchCellProps = (
Component: ComponentType<DispatchCellProps>,
memoize = true
): ComponentType<OwnPropsOfCell> =>
withJsonFormsContext(
withContextToDispatchCellProps(memoize ? React.memo(Component) : Component)
);
export const withJsonFormsEnumCellProps = (
Component: ComponentType<EnumCellProps>,
memoize = true
): ComponentType<OwnPropsOfEnumCell> =>
withJsonFormsContext(
withContextToEnumCellProps(memoize ? React.memo(Component) : Component)
);
export const withJsonFormsEnumProps = (
Component: ComponentType<ControlProps & OwnPropsOfEnum>,
memoize = true
): ComponentType<OwnPropsOfControl & OwnPropsOfEnum> =>
withJsonFormsContext(
withContextToEnumProps(memoize ? React.memo(Component) : Component)
);
export const withJsonFormsOneOfEnumCellProps = (
Component: ComponentType<EnumCellProps>,
memoize = true
): ComponentType<OwnPropsOfEnumCell> =>
withJsonFormsContext(
withContextToOneOfEnumCellProps(memoize ? React.memo(Component) : Component)
);
export const withJsonFormsOneOfEnumProps = (
Component: ComponentType<ControlProps & OwnPropsOfEnum>,
memoize = true
): ComponentType<OwnPropsOfControl & OwnPropsOfEnum> =>
withJsonFormsContext(
withContextToOneOfEnumProps(memoize ? React.memo(Component) : Component)
);
export const withJsonFormsMultiEnumProps = (
Component: ComponentType<
ControlProps & OwnPropsOfEnum & DispatchPropsOfMultiEnumControl
>,
memoize = true
): ComponentType<
OwnPropsOfControl & OwnPropsOfEnum & DispatchPropsOfMultiEnumControl
> =>
withJsonFormsContext(
withContextToMultiEnumProps(memoize ? React.memo(Component) : Component)
);
export const withJsonFormsLabelProps = (
Component: ComponentType<LabelProps & OwnPropsOfEnum>,
memoize = true
): ComponentType<OwnPropsOfLabel> =>
withJsonFormsContext(
withContextToLabelProps(memoize ? React.memo(Component) : Component)
);
// Util HOCs
export interface TranslateProps {
t: Translator;
locale: string;
}
// TODO fix @typescript-eslint/ban-types
// eslint-disable-next-line @typescript-eslint/ban-types
export const withTranslateProps = <P extends {}>(
Component: ComponentType<TranslateProps & P>
) =>
function WithTranslateProps(props: P) {
const ctx = useJsonForms();
const locale = ctx.i18n?.locale ?? defaultJsonFormsI18nState.locale;
const t = ctx.i18n?.translate ?? defaultJsonFormsI18nState.translate;
return <Component {...props} locale={locale} t={t} />;
};