UNPKG

@cristianuser/antd-renderers

Version:

Ant Design Renderer Set for JSON Forms

34 lines 2.16 kB
import React from 'react'; import { and, hasType, Paths, rankWith, schemaMatches, schemaSubPathMatches, uiTypeIs, } from '@jsonforms/core'; import { withJsonFormsMultiEnumProps } from '@jsonforms/react'; import { Form } from 'antd'; import { isEmpty, startCase } from 'lodash'; import { AntdCheckbox } from '../antd-controls'; import Hidden from '../util/Hidden'; export const EnumArrayRenderer = ({ schema, visible, errors, path, options, data, addItem, removeItem, ...otherProps }) => { const isValid = errors.length === 0; // NOTE: probably will need to remove the marginBottom of Form.Item return (React.createElement(Hidden, { hidden: !visible }, React.createElement(Form.Item, { hasFeedback: !isValid, validateStatus: isValid ? '' : 'error', help: errors }, options.map((option, index) => { const optionPath = Paths.compose(path, `${index}`); const checkboxValue = data?.includes(option.value) ? option.value : undefined; return (React.createElement(AntdCheckbox, Object.assign({ id: option.value, key: option.value, label: startCase(option.label), 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))); })))); }; const hasOneOfItems = (schema) => schema.oneOf !== undefined && schema.oneOf.length > 0 && schema.oneOf.every((entry) => { return entry.const !== undefined; }); const hasEnumItems = (schema) => schema.type === 'string' && schema.enum !== undefined; export const enumArrayRendererTester = rankWith(5, and(uiTypeIs('Control'), and(schemaMatches((schema) => hasType(schema, 'array') && !Array.isArray(schema.items) && schema.uniqueItems), schemaSubPathMatches('items', (schema) => { return hasOneOfItems(schema) || hasEnumItems(schema); })))); export default withJsonFormsMultiEnumProps(EnumArrayRenderer); //# sourceMappingURL=EnumArrayRenderer.js.map