Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MultiEnum renderer for enums and oneOf #1709

Merged
merged 2 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions packages/core/src/util/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ export const enumToEnumOptionMapper = (e: any): EnumOption => {
return { label: stringifiedEnum, value: e };
};

export const oneOfToEnumOptionMapper = (e: any): EnumOption => ({
value: e.const,
label:
e.title || (typeof e.const === 'string' ? e.const : JSON.stringify(e.const))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should respect empty titles ;)

Suggested change
e.title || (typeof e.const === 'string' ? e.const : JSON.stringify(e.const))
e.title ?? (typeof e.const === 'string' ? e.const : JSON.stringify(e.const))

});

export interface OwnPropsOfRenderer {
/**
* The UI schema to be rendered.
Expand Down Expand Up @@ -492,10 +498,29 @@ export const mapStateToOneOfEnumControlProps = (
const props: StatePropsOfControl = mapStateToControlProps(state, ownProps);
const options: EnumOption[] =
ownProps.options ||
(props.schema.oneOf as JsonSchema[])?.map(e => ({
value: e.const,
label: e.title || (typeof e.const === 'string' ? e.const : JSON.stringify(e.const))
}));
(props.schema.oneOf as JsonSchema[])?.map(oneOfToEnumOptionMapper);
return {
...props,
options
};
};

/**
* Default mapStateToCellProps for multi enum control. Options is used for populating dropdown list
* @param state
* @param ownProps
* @returns {StatePropsOfControl & OwnPropsOfEnum}
*/
export const mapStateToMultiEnumControlProps = (
state: JsonFormsState,
ownProps: OwnPropsOfControl & OwnPropsOfEnum
): StatePropsOfControl & OwnPropsOfEnum => {
const props: StatePropsOfControl = mapStateToControlProps(state, ownProps);
const items = props.schema.items as JsonSchema;
const options: EnumOption[] =
ownProps.options ||
(items?.oneOf && (items.oneOf as JsonSchema[]).map(oneOfToEnumOptionMapper)) ||
items?.enum?.map(enumToEnumOptionMapper);
return {
...props,
options
Expand Down Expand Up @@ -676,6 +701,36 @@ export const mapDispatchToArrayControlProps = (
}
});

export interface DispatchPropsOfMultiEnumControl {
addItem: (path:string, value: any) => void;
removeItem? :(path:string, toDelete: any) => void;
}

export const mapDispatchToMultiEnumProps = (
dispatch: Dispatch<CoreActions>
): DispatchPropsOfMultiEnumControl => ({
addItem: (path:string, value: any) => {
dispatch(
update(path, data => {
if (data === undefined || data === null) {
return [value];
}
data.push(value);
return data;
})
);
},
removeItem: (path:string, toDelete: any) => {
dispatch(
update(path, data => {
const indexInData = data.indexOf(toDelete);
data.splice(indexInData, 1);
return data;
})
);
}
});

/**
* Props of an array control.
*/
Expand Down
236 changes: 236 additions & 0 deletions packages/core/test/util/renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {
mapStateToJsonFormsRendererProps,
mapStateToLayoutProps,
mapStateToOneOfProps,
mapStateToMultiEnumControlProps,
mapDispatchToMultiEnumProps
} from '../../src/util';
import configureStore from 'redux-mock-store';
import test from 'ava';
Expand Down Expand Up @@ -854,6 +856,240 @@ test("mapStateToOneOfProps - indexOfFittingSchema should not select schema if en
t.is(oneOfProps.indexOfFittingSchema, 1);
});

test('mapStateToMultiEnumControlProps - oneOf items', t => {
const uischema: ControlElement = {
type: 'Control',
scope: '#/properties/colors'
};
const state = {
jsonforms: {
core: {
schema: {
type: 'object',
properties: {
colors: {
type: 'array',
items: {
oneOf: [
{
const: 'red'
},
{
const: 'pink',
title: 'almost red'
}
]
},
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: 'almost red', value: 'pink' }
]);
});

test('mapStateToMultiEnumControlProps - enum items', t => {
const uischema: ControlElement = {
type: 'Control',
scope: '#/properties/colors'
};
const state = {
jsonforms: {
core: {
schema: {
type: 'object',
properties: {
colors: {
type: 'array',
items: {
type: 'string',
enum: ['red', 'green', 'pink']
},
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',
scope: '#/properties/colors'
};
const schema = {
type: 'object',
properties: {
colors: {
type: 'array',
items: {
type: 'string',
enum: ['red', 'green', 'pink']
},
uniqueItems: true
}
}
};
const data = {colors:['green']};
const initCore : JsonFormsCore = {
uischema,
schema,
data,
errors: [] as ErrorObject[]
};
const [getCore, dispatch] = mockDispatch(initCore);
dispatch(init(data, schema, uischema, createAjv({ useDefaults: true })));
const props = mapDispatchToMultiEnumProps(dispatch);
props.addItem('colors', 'pink');

t.is(getCore().data.colors.length, 2);
t.deepEqual(getCore().data.colors[0], 'green');
t.deepEqual(getCore().data.colors[1], 'pink');
});

test('mapDispatchToMultiEnumProps - enum schema - removeItem', t => {
const uischema: ControlElement = {
type: 'Control',
scope: '#/properties/colors'
};
const schema = {
type: 'object',
properties: {
colors: {
type: 'array',
items: {
type: 'string',
enum: ['red', 'green', 'pink']
},
uniqueItems: true
}
}
};
const data = {colors:['green', 'red']};
const initCore : JsonFormsCore = {
uischema,
schema,
data,
errors: [] as ErrorObject[]
};
const [getCore, dispatch] = mockDispatch(initCore);
dispatch(init(data, schema, uischema, createAjv({ useDefaults: true })));
const props = mapDispatchToMultiEnumProps(dispatch);
props.removeItem('colors', 'red');

t.is(getCore().data.colors.length, 1);
t.deepEqual(getCore().data.colors[0], 'green');
});

test('mapDispatchToMultiEnumProps - oneOf schema - addItem', t => {
const uischema: ControlElement = {
type: 'Control',
scope: '#/properties/colors'
};
const schema = {
type: 'object',
properties: {
colors: {
type: 'array',
items: {
oneOf: [
{
const: 'red'
},
{
const: 'pink',
title: 'almost red'
}
]
},
uniqueItems: true
}
}
};
const data = {};
const initCore : JsonFormsCore = {
uischema,
schema,
data,
errors: [] as ErrorObject[]
};
const [getCore, dispatch] = mockDispatch(initCore);
dispatch(init(data, schema, uischema, createAjv({ useDefaults: true })));
const props = mapDispatchToMultiEnumProps(dispatch);
props.addItem('colors', 'pink');

t.is(getCore().data.colors.length, 1);
t.deepEqual(getCore().data.colors[0], 'pink');
});

test('mapDispatchToMultiEnumProps - oneOf schema - removeItem', t => {
const uischema: ControlElement = {
type: 'Control',
scope: '#/properties/colors'
};
const schema = {
type: 'object',
properties: {
colors: {
type: 'array',
items: {
oneOf: [
{
const: 'red'
},
{
const: 'pink',
title: 'almost red'
}
]
},
uniqueItems: true
}
}
};
const data = {colors:['pink']};
const initCore : JsonFormsCore = {
uischema,
schema,
data,
errors: [] as ErrorObject[]
};
const [getCore, dispatch] = mockDispatch(initCore);
dispatch(init(data, schema, uischema, createAjv({ useDefaults: true })));
const props = mapDispatchToMultiEnumProps(dispatch);
props.removeItem('colors', 'pink');

t.is(getCore().data.colors.length, 0);
});

test('should assign defaults to enum', t => {
const schema: JsonSchema = {
type: 'object',
Expand Down
Loading