Skip to content

Commit 3aeffce

Browse files
LukasBollsdirix
andauthored
Delete values where appropriate
When a user empties an input, they usually expect the corresponding attribute to be deleted. This commit adapts all renderer sets to delete values instead of storing an empty string or '0' into the data. The previous behavior can be restored using custom renderers. Also introduces a translatable 'None' label for the empty option in React Material. Co-authored-by: Stefan Dirix <[email protected]>
1 parent f4b51e6 commit 3aeffce

27 files changed

+67
-42
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/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/MuiInputText.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ interface MuiTextInputProps {
4040
muiInputProps?: InputProps['inputProps'];
4141
inputComponent?: InputProps['inputComponent'];
4242
}
43+
44+
const eventToValue = (ev:any) => ev.target.value === '' ? undefined : ev.target.value;
45+
4346
export const MuiInputText = React.memo((props: CellProps & WithClassname & MuiTextInputProps) => {
4447
const [showAdornment, setShowAdornment] = useState(false);
4548
const {
@@ -71,7 +74,7 @@ export const MuiInputText = React.memo((props: CellProps & WithClassname & MuiTe
7174
inputProps.size = maxLength;
7275
};
7376

74-
const [inputText, onChange, onClear] = useDebouncedChange(handleChange, '', data, path);
77+
const [inputText, onChange, onClear] = useDebouncedChange(handleChange, '', data, path, eventToValue);
7578
const onPointerEnter = () => setShowAdornment(true);
7679
const onPointerLeave = () => setShowAdornment(false);
7780

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +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,
41-
config
44+
config,
45+
t
4246
} = props;
4347
const appliedUiSchemaOptions = merge({}, config, uischema.options);
48+
const noneOptionLabel = useMemo(() => t('enum.none', i18nDefaults['enum.none'], { schema, uischema, path}), [t, schema, uischema, path]);
4449

4550
return (
4651
<Select
@@ -49,11 +54,11 @@ export const MuiSelect = React.memo((props: EnumCellProps & WithClassname) => {
4954
disabled={!enabled}
5055
autoFocus={appliedUiSchemaOptions.focus}
5156
value={data !== undefined ? data : ''}
52-
onChange={ev => handleChange(path, ev.target.value)}
57+
onChange={ev =>handleChange(path, ev.target.value || undefined)}
5358
fullWidth={true}
5459
variant={'standard'}
5560
>
56-
{[<MenuItem value='' key={'empty'} />].concat(
61+
{[<MenuItem value={''} key='jsonforms.enum.none'><em>{noneOptionLabel}</em></MenuItem>].concat(
5762
options.map(optionValue => (
5863
<MenuItem value={optionValue.value} key={optionValue.value}>
5964
{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+
}

packages/material/src/util/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ export * from './layout';
2727
export * from './theme';
2828
export * from './debounce';
2929
export * from './focus';
30+
export * from './i18nDefaults';

0 commit comments

Comments
 (0)