Skip to content

Commit

Permalink
fix: resolve refs in EnumArrayRenderer
Browse files Browse the repository at this point in the history
resolve refs ind the MultiEnumControlProps mapper and tester

fixes #2192
  • Loading branch information
LukasBoll authored and lucas-koehler committed Nov 13, 2023
1 parent f905c82 commit c060472
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/core/src/util/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import {
arrayDefaultTranslations,
ArrayTranslations,
} from '../i18n/arrayTranslations';
import { resolveSchema } from './resolvers';

const isRequired = (
schema: JsonSchema,
Expand Down Expand Up @@ -617,7 +618,11 @@ export const mapStateToMultiEnumControlProps = (
ownProps: OwnPropsOfControl & OwnPropsOfEnum
): StatePropsOfControl & OwnPropsOfEnum => {
const props: StatePropsOfControl = mapStateToControlProps(state, ownProps);
const items = props.schema.items as JsonSchema;
let items = props.schema.items as JsonSchema;
items =
items && items.$ref
? resolveSchema(props.rootSchema, items.$ref, props.rootSchema)
: items;
const options: EnumOption[] =
ownProps.options ||
(items?.oneOf &&
Expand Down
44 changes: 44 additions & 0 deletions packages/core/test/util/renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,50 @@ test('mapStateToMultiEnumControlProps - enum items', (t) => {
]);
});

test('mapStateToMultiEnumControlProps - enum with ref', (t) => {
const uischema: ControlElement = {
type: 'Control',
scope: '#/properties/colors',
};
const state = {
jsonforms: {
core: {
schema: {
definitions: {
colors: {
type: 'string',
enum: ['red', 'green', 'pink'],
},
},
type: 'object',
properties: {
colors: {
type: 'array',
items: {
$ref: '#/definitions/colors',
},
uniqueItems: true,
},
},
},
data: {},
uischema,
errors: [] as ErrorObject[],
},
},
};
const ownProps = {
uischema,
path: 'colors',
};
const props = mapStateToMultiEnumControlProps(state, ownProps);
t.deepEqual(props.options, [
{ label: 'red', value: 'red' },
{ label: 'green', value: 'green' },
{ label: 'pink', value: 'pink' },
]);
});

test('mapDispatchToMultiEnumProps - enum schema - addItem', (t) => {
const uischema: ControlElement = {
type: 'Control',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Paths,
RankedTester,
rankWith,
resolveSchema,
schemaMatches,
schemaSubPathMatches,
uiTypeIs,
Expand Down Expand Up @@ -99,8 +100,11 @@ export const materialEnumArrayRendererTester: RankedTester = rankWith(
!Array.isArray(schema.items) &&
schema.uniqueItems === true
),
schemaSubPathMatches('items', (schema) => {
return hasOneOfItems(schema) || hasEnumItems(schema);
schemaSubPathMatches('items', (schema, rootSchema) => {
const resolvedSchema = schema.$ref
? resolveSchema(rootSchema, schema.$ref, rootSchema)
: schema;
return hasOneOfItems(resolvedSchema) || hasEnumItems(resolvedSchema);
})
)
)
Expand Down

0 comments on commit c060472

Please sign in to comment.