Skip to content

Commit 63b40d1

Browse files
TheZokersdirix
andauthored
Update React enum options on i18n changes
We use a custom caching for enum options in the React bindings to enable enum renderer memoization. This caching didn't take the new internationalization mechanism into account, leading to enum labels not being updated on 'translate' change. Also enables cachings for multi-enums which were not cached at all before. Co-authored-by: Stefan Dirix <sdirix@eclipsesource.com>
1 parent 8c3b626 commit 63b40d1

File tree

2 files changed

+88
-6
lines changed

2 files changed

+88
-6
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2017-2019 EclipseSource Munich
5+
https://github.com/eclipsesource/jsonforms
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
*/
25+
import './MatchMediaMock';
26+
import * as React from 'react';
27+
import {
28+
ControlElement
29+
} from '@jsonforms/core';
30+
import { materialRenderers, MuiSelect } from '../../src';
31+
32+
import Enzyme, { mount } from 'enzyme';
33+
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
34+
import { JsonFormsStateProvider } from '@jsonforms/react';
35+
import { initCore } from './util';
36+
import { MaterialEnumControl } from '../../src';
37+
38+
Enzyme.configure({ adapter: new Adapter() });
39+
40+
const data = { nationality: 'JP' };
41+
const schema = {
42+
type: 'string',
43+
enum: ['DE', 'IT', 'JP', 'US', 'RU', 'Other']
44+
};
45+
const uischema: ControlElement = {
46+
type: 'Control',
47+
scope: '#/properties/nationality',
48+
options: {
49+
autocomplete: false
50+
}
51+
};
52+
53+
describe('Material enum control', () => {
54+
it('enum options should change when translation changes', () => {
55+
const core = initCore(schema, uischema, data);
56+
const translate = () => 'Translated';
57+
const changedTranslate = () => 'OtherTranslation';
58+
const wrapper = mount(
59+
<JsonFormsStateProvider initState={{ renderers: materialRenderers, core, i18n: {translate} }}>
60+
<MaterialEnumControl
61+
schema={schema}
62+
uischema={uischema}
63+
path='nationality'
64+
/>
65+
</JsonFormsStateProvider>
66+
);
67+
68+
expect(wrapper.find(MuiSelect).props().options[0].label).toBe('Translated');
69+
70+
wrapper.setProps({ initState: { renderers: materialRenderers, core, i18n: {translate: changedTranslate} }} );
71+
wrapper.update();
72+
73+
expect(wrapper.find(MuiSelect).props().options[0].label).toBe('OtherTranslation');
74+
});
75+
});

packages/react/src/JsonFormsContext.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export const ctxToEnumControlProps = (ctx: JsonFormsStateContext, props: OwnProp
225225
* Make sure, that options are memoized as otherwise the component will rerender for every change,
226226
* as the options array is recreated every time.
227227
*/
228-
const options = useMemo(() => enumProps.options, [props.options, enumProps.schema]);
228+
const options = useMemo(() => enumProps.options, [props.options, enumProps.schema, ctx.i18n?.translate]);
229229
return {...enumProps, options}
230230
}
231231

@@ -235,12 +235,19 @@ export const ctxToOneOfEnumControlProps = (ctx: JsonFormsStateContext, props: Ow
235235
* Make sure, that options are memoized as otherwise the component will rerender for every change,
236236
* as the options array is recreated every time.
237237
*/
238-
const options = useMemo(() => enumProps.options, [props.options, enumProps.schema]);
238+
const options = useMemo(() => enumProps.options, [props.options, enumProps.schema, ctx.i18n?.translate]);
239239
return {...enumProps, options}
240240
}
241241

242-
export const ctxToMultiEnumControlProps = (ctx: JsonFormsStateContext, props: OwnPropsOfControl) =>
243-
mapStateToMultiEnumControlProps({ jsonforms: { ...ctx } }, props);
242+
export const ctxToMultiEnumControlProps = (ctx: JsonFormsStateContext, props: OwnPropsOfControl) => {
243+
const enumProps = mapStateToMultiEnumControlProps({ jsonforms: { ...ctx } }, props);
244+
/**
245+
* Make sure, that options are memoized as otherwise the component will rerender for every change,
246+
* as the options array is recreated every time.
247+
*/
248+
const options = useMemo(() => enumProps.options, [enumProps.schema, ctx.i18n?.translate]);
249+
return {...enumProps, options}
250+
}
244251

245252
export const ctxToControlWithDetailProps = (
246253
ctx: JsonFormsStateContext,
@@ -318,7 +325,7 @@ export const ctxToEnumCellProps = (
318325
* Make sure, that options are memoized as otherwise the cell will rerender for every change,
319326
* as the options array is recreated every time.
320327
*/
321-
const options = useMemo(() => cellProps.options, [ownProps.options, cellProps.schema]);
328+
const options = useMemo(() => cellProps.options, [ownProps.options, cellProps.schema, ctx.i18n?.translate]);
322329
return {...cellProps, options}
323330
};
324331

@@ -331,7 +338,7 @@ export const ctxToOneOfEnumCellProps = (
331338
* Make sure, that options are memoized as otherwise the cell will rerender for every change,
332339
* as the options array is recreated every time.
333340
*/
334-
const options = useMemo(() => enumCellProps.options, [props.options, enumCellProps.schema])
341+
const options = useMemo(() => enumCellProps.options, [props.options, enumCellProps.schema, ctx.i18n?.translate])
335342
return {...enumCellProps, options};
336343
};
337344

0 commit comments

Comments
 (0)