Skip to content

Commit 1f3f92a

Browse files
eneufeldsdirix
authored andcommitted
Update ContextToProps to handle enums correctly
* Update withContextToEnumCellProps to handle const case * Add test case * Extend example Fixes #1548
1 parent 93111bb commit 1f3f92a

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

packages/examples/src/arrays.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export const schema = {
3939
message: {
4040
type: 'string',
4141
maxLength: 5
42+
},
43+
enum: {
44+
type: 'string',
45+
const: 'foo'
4246
}
4347
}
4448
}

packages/react/src/JsonFormsContext.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,7 @@ const withContextToEnumCellProps =
341341
({ ctx, props }: JsonFormsStateContext & EnumCellProps) => {
342342
const cellProps = ctxToCellProps(ctx, props);
343343
const dispatchProps = ctxDispatchToControlProps(ctx.dispatch);
344-
const options =
345-
props.options !== undefined ? props.options : props.schema.enum;
344+
const options = props.options !== undefined ? props.options : props.schema.enum || [props.schema.const];
346345

347346
return (<Component {...props} {...dispatchProps} {...cellProps} options={options} />);
348347
};

packages/react/test/JsonFormsContext.test.tsx

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import React from 'react';
22
import Enzyme, { mount } from 'enzyme';
33
import Adapter from 'enzyme-adapter-react-16';
44
import {
5+
CellProps,
56
ControlProps,
67
OwnPropsOfEnum,
78
rankWith
89
} from '@jsonforms/core';
910

1011
import { JsonForms } from '../src/JsonForms';
11-
import { withJsonFormsEnumProps } from '../src/JsonFormsContext';
12+
import { withJsonFormsEnumCellProps, withJsonFormsEnumProps } from '../src/JsonFormsContext';
1213

1314
Enzyme.configure({ adapter: new Adapter() });
1415

@@ -110,3 +111,86 @@ test('withJsonFormsEnumProps - enum: should supply control and enum props', () =
110111
null
111112
]);
112113
});
114+
115+
test('withJsonFormsEnumCellProps - constant: should supply control and enum props', () => {
116+
const MockEnumCellUnwrapped = (_: CellProps & OwnPropsOfEnum) => {
117+
return <></>;
118+
};
119+
120+
const MockEnumControl = withJsonFormsEnumCellProps(MockEnumCellUnwrapped);
121+
122+
const schema = {
123+
const: 'Cambodia'
124+
};
125+
126+
const renderers = [
127+
{
128+
tester: rankWith(1, () => true),
129+
renderer: MockEnumControl
130+
}
131+
];
132+
133+
const uischema = {
134+
type: 'Control',
135+
scope: '#/properties/name'
136+
};
137+
138+
const wrapper = mount(
139+
<JsonForms
140+
data={{}}
141+
schema={schema}
142+
uischema={uischema}
143+
renderers={renderers}
144+
/>
145+
);
146+
const mockEnumControlUnwrappedProps = wrapper
147+
.find(MockEnumCellUnwrapped)
148+
.props();
149+
150+
expect(mockEnumControlUnwrappedProps.uischema).toEqual(uischema);
151+
expect(mockEnumControlUnwrappedProps.schema).toEqual(schema);
152+
expect(mockEnumControlUnwrappedProps.options).toEqual(['Cambodia']);
153+
});
154+
155+
test('withJsonFormsEnumCellProps - enum: should supply control and enum props', () => {
156+
const MockEnumCellUnwrapped = (_: CellProps & OwnPropsOfEnum) => {
157+
return <></>;
158+
};
159+
160+
const MockEnumControl = withJsonFormsEnumCellProps(MockEnumCellUnwrapped);
161+
162+
const schema = {
163+
type: 'string',
164+
enum: ['red', 'amber', 'green', null]
165+
};
166+
167+
const renderers = [
168+
{
169+
tester: rankWith(1, () => true),
170+
renderer: MockEnumControl
171+
}
172+
];
173+
174+
const uischema = {
175+
type: 'Control',
176+
scope: '#/properties/color'
177+
};
178+
179+
const wrapper = mount(
180+
<JsonForms
181+
data={{}}
182+
schema={schema}
183+
uischema={uischema}
184+
renderers={renderers}
185+
/>
186+
);
187+
const mockEnumControlUnwrappedProps = wrapper.find(MockEnumCellUnwrapped).props();
188+
expect(mockEnumControlUnwrappedProps.uischema).toEqual(uischema);
189+
expect(mockEnumControlUnwrappedProps.schema).toEqual(schema);
190+
expect(mockEnumControlUnwrappedProps.options).toEqual([
191+
'red',
192+
'amber',
193+
'green',
194+
null
195+
]);
196+
});

0 commit comments

Comments
 (0)