import { and, ControlProps, DispatchPropsOfMultiEnumControl, hasType, JsonSchema, OwnPropsOfEnum, Paths, RankedTester, rankWith, schemaMatches, schemaSubPathMatches, uiTypeIs } from '@json-forms/core'; import { withJsonFormsMultiEnumProps } from '@json-forms/react'; import { MuiCheckbox } from '../mui-controls'; import { FormControl, FormControlLabel, FormGroup, FormHelperText, Hidden } from '@material-ui/core'; import { startCase } from 'lodash'; import { isEmpty } from 'lodash'; import React from 'react'; export const MaterialEnumArrayRenderer = ({ schema, visible, errors, path, options, data, addItem, removeItem, handleChange, ...otherProps }: ControlProps & OwnPropsOfEnum & DispatchPropsOfMultiEnumControl) => { return ( {options.map((option: any, index: number) => { const optionPath = Paths.compose(path, `${index}`); const checkboxValue = data?.includes(option.value) ? option.value : undefined; return ( newValue ? addItem(path, option.value) : removeItem(path, option.value) } data={checkboxValue} errors={errors} schema={schema} visible={visible} {...otherProps} /> } label={startCase(option.label)} /> ); })} {errors} ); }; 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);