/
jsonFormsCompositions.ts
476 lines (441 loc) · 13.9 KB
/
jsonFormsCompositions.ts
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
import {
ControlElement,
Dispatch,
Layout,
mapDispatchToControlProps,
mapStateToControlProps,
mapStateToLayoutProps,
JsonFormsSubStates,
JsonSchema,
UISchemaElement,
CoreActions,
mapStateToEnumControlProps,
JsonFormsState,
mapStateToOneOfEnumControlProps,
OwnPropsOfMasterListItem,
mapStateToMasterListItemProps,
mapStateToControlWithDetailProps,
mapStateToArrayControlProps,
mapDispatchToArrayControlProps,
mapStateToAllOfProps,
mapStateToAnyOfProps,
mapStateToOneOfProps,
mapStateToJsonFormsRendererProps,
mapStateToArrayLayoutProps,
mapStateToCellProps,
JsonFormsRendererRegistryEntry,
JsonFormsCellRendererRegistryEntry,
defaultMapStateToEnumCellProps,
mapStateToDispatchCellProps,
mapStateToOneOfEnumCellProps,
StatePropsOfJsonFormsRenderer,
createId,
removeId,
mapStateToMultiEnumControlProps,
mapDispatchToMultiEnumProps,
mapStateToLabelProps,
LabelElement,
} from '@jsonforms/core';
import {
PropType,
computed,
ComputedRef,
inject,
onBeforeMount,
onUnmounted,
ref,
} from 'vue';
/**
* Constructs a props declaration for Vue components which can be used
* for registered renderers and cells. These are typically used in combination
* with one of the provided bindings, e.g. 'useJsonFormsControl'.
*
* Use the generic type parameter when using a specialized binding, e.g.
* `rendererProps<Layout>()` in combination with `useJsonFormsLayout` or
* `rendererProps<ControlElement>()` in combination with `useJsonFormsControl`.
*/
export const rendererProps = <U = UISchemaElement>() => ({
schema: {
required: true as const,
type: [Object, Boolean] as PropType<JsonSchema>,
},
uischema: {
required: true as const,
type: Object as PropType<U>,
},
path: {
required: true as const,
type: String,
},
enabled: {
required: false as const,
type: Boolean,
default: undefined,
},
renderers: {
required: false,
type: Array as PropType<JsonFormsRendererRegistryEntry[]>,
default: undefined,
},
cells: {
required: false,
type: Array as PropType<JsonFormsCellRendererRegistryEntry[]>,
default: undefined,
},
config: {
required: false,
type: Object,
default: undefined,
},
});
/**
* Constructs a props declaration for Vue components which shall be used as
* master list items.
*/
export const masterListItemProps = () => ({
index: {
required: true as const,
type: Number,
},
selected: {
required: true as const,
type: Boolean,
},
path: {
required: true as const,
type: String,
},
schema: {
required: true as const,
type: [Object, Boolean] as PropType<JsonSchema>,
},
handleSelect: {
required: false as const,
type: Function as PropType<(index: number) => void>,
default: undefined,
},
removeItem: {
required: false as const,
type: Function as PropType<(path: string, value: number) => void>,
default: undefined,
},
});
export interface RendererProps<U = UISchemaElement> {
schema: JsonSchema;
uischema: U;
path: string;
enabled?: boolean;
renderers?: JsonFormsRendererRegistryEntry[];
cells?: JsonFormsCellRendererRegistryEntry[];
config?: any;
}
export interface ControlProps extends RendererProps {
uischema: ControlElement;
}
export type Required<T> = T extends object
? { [P in keyof T]-?: NonNullable<T[P]> }
: T;
// TODO fix @typescript-eslint/ban-types
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-unused-vars
export function useControl<R, D, P extends {}>(
props: P,
stateMap: (state: JsonFormsState, props: P) => R
): { control: ComputedRef<Required<R>> };
// TODO fix @typescript-eslint/ban-types
// eslint-disable-next-line @typescript-eslint/ban-types
export function useControl<R, D, P extends {}>(
props: P,
stateMap: (state: JsonFormsState, props: P) => R,
dispatchMap: (dispatch: Dispatch<CoreActions>) => D
): { control: ComputedRef<Required<R>> } & D;
// TODO fix @typescript-eslint/ban-types
// eslint-disable-next-line @typescript-eslint/ban-types
export function useControl<R, D, P extends {}>(
props: P,
stateMap: (state: JsonFormsState, props: P) => R,
dispatchMap?: (dispatch: Dispatch<CoreActions>) => D
) {
const jsonforms = inject<JsonFormsSubStates>('jsonforms');
const dispatch = inject<Dispatch<CoreActions>>('dispatch');
if (!jsonforms || !dispatch) {
throw "'jsonforms' or 'dispatch' couldn't be injected. Are you within JSON Forms?";
}
const id = ref<string | undefined>(undefined);
const control = computed(() => ({
...props,
...stateMap({ jsonforms }, props),
id: id.value,
}));
const dispatchMethods = dispatchMap?.(dispatch);
onBeforeMount(() => {
if ((control.value as any).uischema.scope) {
id.value = createId((control.value as any).uischema.scope);
}
});
onUnmounted(() => {
if (id.value) {
removeId(id.value);
id.value = undefined;
}
});
return {
control: control as unknown as ComputedRef<R>,
...dispatchMethods,
};
}
/**
* Provides generic bindings for 'Control' elements.
* Should be used when no specialized bindings are appropriate.
*
* Access bindings via the provided reactive `control` object.
* Dispatch changes via the provided `handleChange` method.
*/
export const useJsonFormsControl = (props: ControlProps) => {
return useControl(props, mapStateToControlProps, mapDispatchToControlProps);
};
/**
* Provides bindings for 'Control' elements which can provide a 'detail',
* for example array and object renderers.
*
* Access bindings via the provided reactive `control` object.
* Dispatch changes via the provided `handleChange` method.
*/
export const useJsonFormsControlWithDetail = (props: ControlProps) => {
return useControl(
props,
mapStateToControlWithDetailProps,
mapDispatchToControlProps
);
};
/**
* Provides bindings for 'Control' elements which resolve to 'enum' schema elements.
*
* Access bindings via the provided reactive `control` object.
* Dispatch changes via the provided `handleChange` method.
*/
export const useJsonFormsEnumControl = (props: ControlProps) => {
return useControl(
props,
mapStateToEnumControlProps,
mapDispatchToControlProps
);
};
/**
* Provides bindings for 'Control' elements which resolve to manually constructed
* 'oneOf' enums. These are used to enhance enums with label support.
*
* Access bindings via the provided reactive `control` object.
* Dispatch changes via the provided `handleChange` method.
*/
export const useJsonFormsOneOfEnumControl = (props: ControlProps) => {
return useControl(
props,
mapStateToOneOfEnumControlProps,
mapDispatchToControlProps
);
};
// Explicitly create type to avoid TS2732.
// This is due to core's StatePropsOfArrayControl exposing AJV's ErrorObject type.
// The same as the parameterized return type of useControl
// Could alternatively be created like the following with Typescript 4.7+
// but this does not work with the ESLint typescript parser, yet:
// type UseJsonFormsArrayControlReturnType = typeof useControl<
// ReturnType<typeof mapStateToArrayControlProps>,
// ReturnType<typeof mapDispatchToArrayControlProps>,
// ControlProps
// >;
type UseJsonFormsArrayControlReturnType = {
control: ComputedRef<
Required<ReturnType<typeof mapStateToArrayControlProps>>
>;
} & ReturnType<typeof mapDispatchToArrayControlProps>;
/**
* Provides bindings for 'Control' elements which resolve to 'array' schema elements.
*
* Access bindings via the provided reactive `control` object.
* Dispatch changes via the provided `handleChange` method.
*/
export const useJsonFormsArrayControl: (
props: ControlProps
) => UseJsonFormsArrayControlReturnType = (props: ControlProps) => {
return useControl(
props,
mapStateToArrayControlProps,
mapDispatchToArrayControlProps
);
};
/**
* Provides bindings for 'Control' elements which resolve to 'allOf' schema elements.
*
* Access bindings via the provided reactive `control` object.
* Dispatch changes via the provided `handleChange` method.
*/
export const useJsonFormsAllOfControl = (props: ControlProps) => {
return useControl(props, mapStateToAllOfProps, mapDispatchToControlProps);
};
/**
* Provides bindings for 'Control' elements which resolve to 'anyOf' schema elements.
*
* Access bindings via the provided reactive `control` object.
* Dispatch changes via the provided `handleChange` method.
*/
export const useJsonFormsAnyOfControl = (props: ControlProps) => {
return useControl(props, mapStateToAnyOfProps, mapDispatchToControlProps);
};
/**
* Provides bindings for 'Control' elements which resolve to 'oneOf' schema elements.
*
* Access bindings via the provided reactive `control` object.
* Dispatch changes via the provided `handleChange` method.
*/
export const useJsonFormsOneOfControl = (props: ControlProps) => {
return useControl(props, mapStateToOneOfProps, mapDispatchToControlProps);
};
/**
* Provides bindings for 'Control' elements which resolve to multiple choice enums.
*
* Access bindings via the provided reactive `control` object.
* Dispatch changes via the provided `handleChange` method.
*/
export const useJsonFormsMultiEnumControl = (props: ControlProps) => {
return useControl(
props,
mapStateToMultiEnumControlProps,
mapDispatchToMultiEnumProps
);
};
export interface LayoutProps extends RendererProps {
uischema: Layout;
}
/**
* Provides bindings for 'Layout' elements, e.g. VerticalLayout, HorizontalLayout, Group.
*
* Access bindings via the provided reactive 'layout' object.
*/
export const useJsonFormsLayout = (props: LayoutProps) => {
const { control, ...other } = useControl(props, mapStateToLayoutProps);
return { layout: control, ...other };
};
/**
* Provides bindings for 'Control' elements which resolve to 'array' elements which
* shall be rendered as a layout instead of a control.
*
* Access bindings via the provided reactive 'layout' object.
*/
export const useJsonFormsArrayLayout = (props: ControlProps) => {
const { control, ...other } = useControl(props, mapStateToArrayLayoutProps);
return { layout: control, ...other };
};
/**
* Provides bindings for list elements of a master-list-detail control setup.
* The element using this binding is not supposed to be registered as an own renderer
* but used in a more specialized control.
*
* Access bindings via the provided reactive 'item' object.
*/
export const useJsonFormsMasterListItem = (props: OwnPropsOfMasterListItem) => {
const { control, ...other } = useControl<
Omit<OwnPropsOfMasterListItem, 'handleSelect' | 'removeItem'>,
unknown,
OwnPropsOfMasterListItem
>(props, mapStateToMasterListItemProps);
return { item: control, ...other };
};
/**
* Provides specialized bindings which can be used for any renderer.
* Useful for meta elements like dispatchers.
*
* Access bindings via the provided reactive 'renderer' object.
*/
export const useJsonFormsRenderer = (props: RendererProps) => {
const jsonforms = inject<JsonFormsSubStates>('jsonforms');
const dispatch = inject<Dispatch<CoreActions>>('dispatch');
if (!jsonforms || !dispatch) {
throw "'jsonforms' or 'dispatch' couldn't be injected. Are you within JSON Forms?";
}
const rawProps = computed(
() =>
mapStateToJsonFormsRendererProps(
{ jsonforms },
props
) as Required<StatePropsOfJsonFormsRenderer>
);
const rootSchema = computed(() => rawProps.value.rootSchema);
const renderer = computed(() => {
const { rootSchema: _rootSchema, ...rest } = rawProps.value;
return rest;
});
return {
renderer,
rootSchema,
};
};
/**
* Provides bindings for 'Label' elements.
*
* Access bindings via the provided reactive `label` object.
*/
export const useJsonFormsLabel = (props: RendererProps<LabelElement>) => {
const { control, ...other } = useControl(props, mapStateToLabelProps);
return { label: control, ...other };
};
/**
* Provides bindings for cell elements. Cells are meant to show simple inputs,
* for example without error validation, within a larger structure like tables.
*
* Access bindings via the provided reactive 'cell' object.
* Dispatch changes via the provided `handleChange` method.
*/
export const useJsonFormsCell = (props: ControlProps) => {
const { control, ...other } = useControl(
props,
mapStateToCellProps,
mapDispatchToControlProps
);
return { cell: control, ...other };
};
/**
* Provides bindings for enum cell elements. Cells are meant to show simple inputs,
* for example without error validation, within a larger structure like tables.
*
* Access bindings via the provided reactive 'cell' object.
* Dispatch changes via the provided `handleChange` method.
*/
export const useJsonFormsEnumCell = (props: ControlProps) => {
const { control, ...other } = useControl(
props,
defaultMapStateToEnumCellProps,
mapDispatchToControlProps
);
return { cell: control, ...other };
};
/**
* Provides bindings for 'oneOf' enum cell elements. Cells are meant to show simple inputs,
* for example without error validation, within a larger structure like tables.
*
* Access bindings via the provided reactive 'cell' object.
* Dispatch changes via the provided `handleChange` method.
*/
export const useJsonFormsOneOfEnumCell = (props: ControlProps) => {
const { control, ...other } = useControl(
props,
mapStateToOneOfEnumCellProps,
mapDispatchToControlProps
);
return { cell: control, ...other };
};
/**
* Provides bindings for a cell dispatcher. Cells are meant to show simple inputs,
* for example without error validation, within a larger structure like tables.
*
* Access bindings via the provided reactive 'cell' object.
* Dispatch changes via the provided `handleChange` method.
*/
export const useJsonFormsDispatchCell = (props: ControlProps) => {
const { control, ...other } = useControl(
props,
mapStateToDispatchCellProps,
mapDispatchToControlProps
);
return { cell: control, ...other };
};