/
MaterialEnumArrayRenderer.tsx
109 lines (103 loc) · 2.83 KB
/
MaterialEnumArrayRenderer.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
import {
and,
ControlProps,
DispatchPropsOfMultiEnumControl,
hasType,
JsonSchema,
OwnPropsOfEnum,
Paths,
RankedTester,
rankWith,
schemaMatches,
schemaSubPathMatches,
uiTypeIs,
} from '@jsonforms/core';
import { withJsonFormsMultiEnumProps } from '@jsonforms/react';
import { MuiCheckbox } from '../mui-controls';
import {
FormControl,
FormControlLabel,
FormGroup,
FormHelperText,
Hidden,
} from '@mui/material';
import isEmpty from 'lodash/isEmpty';
import React from 'react';
export const MaterialEnumArrayRenderer = ({
schema,
visible,
errors,
path,
options,
data,
addItem,
removeItem,
handleChange: _handleChange,
...otherProps
}: ControlProps & OwnPropsOfEnum & DispatchPropsOfMultiEnumControl) => {
return (
<Hidden xlUp={!visible}>
<FormControl component='fieldset'>
<FormGroup row>
{options.map((option: any, index: number) => {
const optionPath = Paths.compose(path, `${index}`);
const checkboxValue = data?.includes(option.value)
? option.value
: undefined;
return (
<FormControlLabel
id={option.value}
key={option.value}
control={
<MuiCheckbox
key={'checkbox-' + option.value}
isValid={isEmpty(errors)}
path={optionPath}
handleChange={(_childPath, newValue) =>
newValue
? addItem(path, option.value)
: removeItem(path, option.value)
}
data={checkboxValue}
errors={errors}
schema={schema}
visible={visible}
{...otherProps}
/>
}
label={option.label}
/>
);
})}
</FormGroup>
<FormHelperText error>{errors}</FormHelperText>
</FormControl>
</Hidden>
);
};
const hasOneOfItems = (schema: JsonSchema): boolean =>
schema.oneOf !== undefined &&
schema.oneOf.length > 0 &&
(schema.oneOf as JsonSchema[]).every((entry: JsonSchema) => {
return entry.const !== undefined;
});
const hasEnumItems = (schema: JsonSchema): boolean =>
schema.type === 'string' && schema.enum !== undefined;
export const materialEnumArrayRendererTester: RankedTester = rankWith(
5,
and(
uiTypeIs('Control'),
and(
schemaMatches(
(schema) =>
hasType(schema, 'array') &&
!Array.isArray(schema.items) &&
schema.uniqueItems === true
),
schemaSubPathMatches('items', (schema) => {
return hasOneOfItems(schema) || hasEnumItems(schema);
})
)
)
);
export default withJsonFormsMultiEnumProps(MaterialEnumArrayRenderer);