Hello,
I’ve (re)created a multi-select custom renderer based on :
I’ve also an issue with the validation…
“must be string must be equal to one of the allowed values” is displayed under the control
my code :
import React, { ReactNode } from 'react';
import merge from 'lodash/merge';
import isEqual from 'lodash/isEqual';
import {
ControlProps,
OwnPropsOfEnum,
WithClassname,
EnumOption,
isDescriptionHidden,
RankedTester,
rankWith,
and,
isControl,
isEnumControl,
optionIs,
} from '@jsonforms/core';
import {
withJsonFormsEnumProps,
withJsonFormsControlProps,
TranslateProps,
withTranslateProps,
} from '@jsonforms/react';
import { Autocomplete, FormHelperText, Hidden, TextField } from '@mui/material';
import { WithOptionLabel } from '@jsonforms/material-renderers/lib/mui-controls/MuiAutocomplete';
import { useFocus } from '@jsonforms/material-renderers';
export const MultiSelectEnumArray = (
props: ControlProps & OwnPropsOfEnum & WithClassname & WithOptionLabel,
) => {
const [focused, onFocus, onBlur] = useFocus();
const {
description,
className,
config,
id,
label,
required,
errors,
data,
visible,
options,
handleChange,
path,
enabled,
getOptionLabel,
} = props;
const isValid = errors.length === 0;
const appliedUiSchemaOptions = merge({}, config, props.uischema.options);
const showDescription = !isDescriptionHidden(
visible,
description,
focused,
appliedUiSchemaOptions.showUnfocusedDescription,
);
const firstFormHelperText = showDescription
? description
: !isValid
? errors
: null;
const secondFormHelperText = showDescription && !isValid ? errors : null;
const onChange = (_ev: any, newValues: EnumOption[]) => {
// convert from an EnumOption to its value
var values = newValues.map(o => (o.value ? o.value : o));
handleChange(path, values);
};
return (
<Hidden xsUp={!visible}>
<Autocomplete
multiple
id={id}
className={className}
disabled={!enabled}
autoHighlight
autoSelect
autoComplete
fullWidth
freeSolo={false}
options={options ? options : []}
value={data}
getOptionLabel={option => (option.label ? option.label : option)}
onChange={onChange}
isOptionEqualToValue={(o, v) => {
return isEqual(o.value, v);
}}
renderInput={params => (
<TextField
label={label}
variant={
appliedUiSchemaOptions.variant
? appliedUiSchemaOptions.variant
: 'outlined'
}
//type="text"
inputRef={params.InputProps.ref}
autoFocus={appliedUiSchemaOptions.focus}
//placeholder="Favorites"
{...params}
id={id + '-input'}
required={required && !appliedUiSchemaOptions.hideRequiredAsterisk}
error={!isValid}
fullWidth={!appliedUiSchemaOptions.trim}
InputLabelProps={data ? { shrink: true } : undefined}
onFocus={onFocus}
onBlur={onBlur}
focused={focused}
/>
)}
/>
<FormHelperText error={!isValid && !showDescription}>
{firstFormHelperText}
</FormHelperText>
<FormHelperText error={!isValid}>{secondFormHelperText}</FormHelperText>
</Hidden>
);
};
export const MultiSelectEnumArrayTester: RankedTester = rankWith(
10,
//isEnumControl,
and(isEnumControl, optionIs('xxmultiple', true)),
);
export default withJsonFormsEnumProps(
withTranslateProps(React.memo(MultiSelectEnumArray)),
false,
);
extract schema.json
“v09”: {
“title”: “member of ?”,
“type”: “string”,
“enum”: [“ABC”, “DEF”, “GHI”, “Forum”],
},
(extract) uischema.json
{
“type”: “Control”,
“scope”: “#/properties/VENDOR/properties/v09”,
“options”: {
“xxmultiple”: true
}
},
I imagine it’s probably something bad on schemas… but what ?!
Thx!!