Skip to content

Commit 991bc40

Browse files
committed
Review changes
- determine label in renderers - adapt angular material controls for consistency - adapt vue vanilla controls for consistency
1 parent e93f7d1 commit 991bc40

23 files changed

+56
-61
lines changed

packages/angular-material/src/controls/text.renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class TextControlRenderer extends JsonFormsControl {
4848
constructor(jsonformsService: JsonFormsAngularService) {
4949
super(jsonformsService);
5050
}
51-
getEventValue = (event: any) => event.target.value;
51+
getEventValue = (event: any) => event.target.value || undefined;
5252
getType = (): string => {
5353
if (this.uischema.options && this.uischema.options.format) {
5454
return this.uischema.options.format;

packages/angular-material/src/controls/textarea.renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class TextAreaRenderer extends JsonFormsControl {
4747
constructor(jsonformsService: JsonFormsAngularService) {
4848
super(jsonformsService);
4949
}
50-
getEventValue = (event: any) => event.target.value;
50+
getEventValue = (event: any) => event.target.value || undefined;
5151
}
5252
export const TextAreaRendererTester: RankedTester = rankWith(
5353
2,

packages/core/src/util/cell.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import {
5555
} from './renderer';
5656
import { JsonFormsState } from '../store';
5757
import { JsonSchema } from '../models';
58-
import { getI18nKey, getI18nKeyPrefix } from '../i18n';
58+
import { getI18nKeyPrefix } from '../i18n';
5959

6060
export type { JsonFormsCellRendererRegistryEntry };
6161

@@ -212,11 +212,9 @@ export const defaultMapStateToEnumCellProps = (
212212
getI18nKeyPrefix(props.schema, props.uischema, props.path)
213213
)
214214
]);
215-
const emptyLabel = getEmptyLabel(props,state);
216215
return {
217216
...props,
218-
options,
219-
emptyLabel
217+
options
220218
};
221219
};
222220

@@ -240,11 +238,9 @@ export const mapStateToOneOfEnumCellProps = (
240238
getI18nKeyPrefix(props.schema, props.uischema, props.path)
241239
)
242240
);
243-
const emptyLabel = getEmptyLabel(props,state);
244241
return {
245242
...props,
246-
options,
247-
emptyLabel
243+
options
248244
};
249245
};
250246

@@ -271,8 +267,3 @@ export const defaultMapDispatchToControlProps =
271267
handleChange: ownProps.handleChange || handleChange
272268
};
273269
};
274-
275-
function getEmptyLabel(props: OwnPropsOfEnumCell, state: JsonFormsState) {
276-
const t = getTranslator()(state);
277-
return t(getI18nKey(props.schema, props.uischema, props.path, 'empty'), 'empty');
278-
}

packages/core/src/util/renderer.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ export interface OwnPropsOfLabel extends OwnPropsOfRenderer {
260260

261261
export interface OwnPropsOfEnum {
262262
options?: EnumOption[];
263-
emptyLabel?: string;
264263
}
265264

266265
export interface OwnPropsOfLayout extends OwnPropsOfRenderer {
@@ -534,11 +533,9 @@ export const mapStateToEnumControlProps = (
534533
getI18nKeyPrefix(props.schema, props.uischema, props.path)
535534
)
536535
]);
537-
const emptyLabel = getEmptyLabel(props,state);
538536
return {
539537
...props,
540-
options,
541-
emptyLabel
538+
options
542539
};
543540
};
544541

@@ -562,11 +559,9 @@ export const mapStateToOneOfEnumControlProps = (
562559
getI18nKeyPrefix(props.schema, props.uischema, props.path)
563560
)
564561
);
565-
const emptyLabel = getEmptyLabel(props,state);
566562
return {
567563
...props,
568-
options,
569-
emptyLabel
564+
options
570565
};
571566
};
572567

@@ -1096,9 +1091,3 @@ export const mapStateToLabelProps = (
10961091
cells: props.cells || getCells(state),
10971092
}
10981093
}
1099-
1100-
function getEmptyLabel(props: StatePropsOfControl, state: JsonFormsState) {
1101-
const t = getTranslator()(state);
1102-
return t(getI18nKey(props.schema, props.uischema, props.path, 'empty'), 'empty');
1103-
}
1104-

packages/material/src/cells/MaterialEnumCell.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ import {
3030
rankWith,
3131
WithClassname
3232
} from '@jsonforms/core';
33-
import { withJsonFormsEnumCellProps } from '@jsonforms/react';
33+
import { TranslateProps, withJsonFormsEnumCellProps, withTranslateProps } from '@jsonforms/react';
3434
import { MuiSelect } from '../mui-controls/MuiSelect';
3535

36-
export const MaterialEnumCell = (props: EnumCellProps & WithClassname) => (
36+
export const MaterialEnumCell = (props: EnumCellProps & WithClassname & TranslateProps) => (
3737
<MuiSelect {...props} />
3838
);
3939

@@ -43,4 +43,5 @@ export const MaterialEnumCell = (props: EnumCellProps & WithClassname) => (
4343
*/
4444
export const materialEnumCellTester: RankedTester = rankWith(2, isEnumControl);
4545

46-
export default withJsonFormsEnumCellProps(MaterialEnumCell);
46+
// HOC order can be reversed with https://github.com/eclipsesource/jsonforms/issues/1987
47+
export default withJsonFormsEnumCellProps(withTranslateProps(React.memo(MaterialEnumCell)), false);

packages/material/src/cells/MaterialOneOfEnumCell.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ import {
3030
rankWith,
3131
WithClassname
3232
} from '@jsonforms/core';
33-
import { withJsonFormsOneOfEnumCellProps } from '@jsonforms/react';
33+
import { TranslateProps, withJsonFormsOneOfEnumCellProps, withTranslateProps } from '@jsonforms/react';
3434
import { MuiSelect } from '../mui-controls/MuiSelect';
3535

36-
export const MaterialOneOfEnumCell = (props: EnumCellProps & WithClassname) => (
36+
export const MaterialOneOfEnumCell = (props: EnumCellProps & WithClassname & TranslateProps) => (
3737
<MuiSelect {...props} />
3838
);
3939

@@ -43,4 +43,4 @@ export const MaterialOneOfEnumCell = (props: EnumCellProps & WithClassname) => (
4343
*/
4444
export const materialOneOfEnumCellTester: RankedTester = rankWith(2, isOneOfEnumControl);
4545

46-
export default withJsonFormsOneOfEnumCellProps(MaterialOneOfEnumCell);
46+
export default withJsonFormsOneOfEnumCellProps(withTranslateProps(React.memo(MaterialOneOfEnumCell)), false);

packages/material/src/controls/MaterialEnumControl.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ import {
3030
RankedTester,
3131
rankWith,
3232
} from '@jsonforms/core';
33-
import { withJsonFormsEnumProps } from '@jsonforms/react';
33+
import { TranslateProps, withJsonFormsEnumProps, withTranslateProps } from '@jsonforms/react';
3434
import { MuiSelect } from '../mui-controls/MuiSelect';
3535
import merge from 'lodash/merge';
3636
import { MaterialInputControl } from './MaterialInputControl';
3737
import { MuiAutocomplete, WithOptionLabel } from '../mui-controls/MuiAutocomplete';
3838

39-
export const MaterialEnumControl = (props: ControlProps & OwnPropsOfEnum & WithOptionLabel) => {
39+
export const MaterialEnumControl = (props: ControlProps & OwnPropsOfEnum & WithOptionLabel & TranslateProps) => {
4040
const {config, uischema, errors} = props;
4141
const appliedUiSchemaOptions = merge({}, config, uischema.options);
4242
const isValid = errors.length === 0;
@@ -58,4 +58,5 @@ export const materialEnumControlTester: RankedTester = rankWith(
5858
isEnumControl
5959
);
6060

61-
export default withJsonFormsEnumProps(MaterialEnumControl);
61+
// HOC order can be reversed with https://github.com/eclipsesource/jsonforms/issues/1987
62+
export default withJsonFormsEnumProps(withTranslateProps(React.memo(MaterialEnumControl)), false);

packages/material/src/controls/MaterialOneOfEnumControl.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ import {
3030
RankedTester,
3131
rankWith,
3232
} from '@jsonforms/core';
33-
import { withJsonFormsOneOfEnumProps } from '@jsonforms/react';
33+
import { TranslateProps, withJsonFormsOneOfEnumProps, withTranslateProps } from '@jsonforms/react';
3434
import { MuiAutocomplete, WithOptionLabel } from '../mui-controls/MuiAutocomplete';
3535
import { MuiSelect } from '../mui-controls/MuiSelect';
3636
import { MaterialInputControl } from '../controls/MaterialInputControl';
3737
import merge from 'lodash/merge';
3838

39-
export const MaterialOneOfEnumControl = (props: ControlProps & OwnPropsOfEnum & WithOptionLabel) => {
39+
export const MaterialOneOfEnumControl = (props: ControlProps & OwnPropsOfEnum & WithOptionLabel & TranslateProps) => {
4040
const {config, uischema, errors} = props;
4141
const appliedUiSchemaOptions = merge({}, config, uischema.options);
4242
const isValid = errors.length === 0;
@@ -59,4 +59,5 @@ export const materialOneOfEnumControlTester: RankedTester = rankWith(
5959
isOneOfEnumControl
6060
);
6161

62-
export default withJsonFormsOneOfEnumProps(MaterialOneOfEnumControl);
62+
// HOC order can be reversed with https://github.com/eclipsesource/jsonforms/issues/1987
63+
export default withJsonFormsOneOfEnumProps(withTranslateProps(React.memo(MaterialOneOfEnumControl)), false);

packages/material/src/mui-controls/MuiSelect.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,30 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
*/
25-
import React from 'react';
25+
import React, { useMemo } from 'react';
2626
import { EnumCellProps, WithClassname } from '@jsonforms/core';
2727

2828
import { MenuItem, Select } from '@mui/material';
2929
import merge from 'lodash/merge';
30+
import { TranslateProps } from '@jsonforms/react';
31+
import { i18nDefaults } from '../util';
3032

31-
export const MuiSelect = React.memo((props: EnumCellProps & WithClassname) => {
33+
export const MuiSelect = React.memo((props: EnumCellProps & WithClassname & TranslateProps) => {
3234
const {
3335
data,
3436
className,
3537
id,
3638
enabled,
39+
schema,
3740
uischema,
3841
path,
3942
handleChange,
4043
options,
4144
config,
42-
emptyLabel
45+
t
4346
} = props;
4447
const appliedUiSchemaOptions = merge({}, config, uischema.options);
48+
const noneOptionLabel = useMemo(() => t('enum.none', i18nDefaults['enum.none'], { schema, uischema, path}), [t, schema, uischema, path]);
4549

4650
return (
4751
<Select
@@ -50,11 +54,11 @@ export const MuiSelect = React.memo((props: EnumCellProps & WithClassname) => {
5054
disabled={!enabled}
5155
autoFocus={appliedUiSchemaOptions.focus}
5256
value={data !== undefined ? data : ''}
53-
onChange={ev =>handleChange(path, ev.target.value === '' ? undefined : ev.target.value)}
57+
onChange={ev =>handleChange(path, ev.target.value || undefined)}
5458
fullWidth={true}
5559
variant={'standard'}
5660
>
57-
{[<MenuItem value={''} key={''}><em>{emptyLabel ? emptyLabel : ''}</em></MenuItem>].concat(
61+
{[<MenuItem value={''} key='jsonforms.enum.none'><em>{noneOptionLabel}</em></MenuItem>].concat(
5862
options.map(optionValue => (
5963
<MenuItem value={optionValue.value} key={optionValue.value}>
6064
{optionValue.label}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const i18nDefaults = {
2+
'enum.none': 'None'
3+
}

0 commit comments

Comments
 (0)