Skip to content

Commit

Permalink
Remove custom areEqual methods
Browse files Browse the repository at this point in the history
Due to previous refactorings the data is already only changing
where needed. All other control properties are also memoized
in some way.
So the manual areEqual check is not needed, instead plain memo is
used.
Also add named memos to missing renderers.
Change all material renderers to be functional components.
  • Loading branch information
eneufeld committed Oct 13, 2021
1 parent 3f706fe commit 096815f
Show file tree
Hide file tree
Showing 61 changed files with 1,087 additions and 1,031 deletions.
30 changes: 30 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@

## Migrating to JSON Forms 3.0 for React users

### Removal of Class Components in React Material

With Version 3.0 of JSON Forms we removed all class components.
Please check whether you extended any of our base renderers in your adaptation.

### Memoization changes in React bindings

Previously components wrapped in our bindings (e.g. "`withJsonFormsControlProps`") were automatically memoized using a custom comparison function ("`areEqual`").
The comparison function was rather expensive, invoking deep comparisons on some of the props.
Also automatically applying "React.memo" on all renderers is counter productive when a user might add renderers which always rerender anyway.
Enabled by [previous refactorings](https://github.com/eclipsesource/jsonforms/issues/1715) we now removed the custom comparison function as well as the automatic memoization.
Thus memoization can now achieved by simply using `React.memo`.
If your component relied on our bindings, e.g. `withJsonFormsControlProps` to be memoized, you now need to do it yourself:

```ts
import React from 'react';
import { ControlProps, rankWith } from '@jsonforms/core';
import { withJsonFormsControlProps } from '@jsonforms/react';

const MyControlComponent = ({data}: ControlProps) => {
return (<span>{data}</span>);
};

export const myControlTester = rankWith(2, true);
export const MyControl = React.memo(MyControlComponent);
export default withJsonFormsControlProps(MyControl);
```

### Removal of JSON Schema $Ref Parser

With version 3.0 of JSON Forms, we removed the `json-schema-ref-parser` dependency within the core package.
This change only affects users of the React variant, Vue and Angular users are not affected.

Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/util/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1002,8 +1002,6 @@ export const mapStateToArrayLayoutProps = (
};
};

export type CombinatorProps = StatePropsOfCombinator & DispatchPropsOfControl;

/**
* Props of an array control.
*/
Expand Down
7 changes: 6 additions & 1 deletion packages/examples/src/arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ export const schema = {
}
}
}
}
},
foo:{type:'string'}
}
};

export const uischema = {
type: 'VerticalLayout',
elements: [
{
type: 'Control',
scope: '#/properties/foo'
},
{
type: 'Control',
scope: '#/properties/comments'
Expand Down
3 changes: 2 additions & 1 deletion packages/material/src/additional/MaterialLabelRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const materialLabelRendererTester: RankedTester = rankWith(1, uiTypeIs('L
/**
* Default renderer for a label.
*/
export const MaterialLabelRenderer = ({ uischema, visible }: OwnPropsOfRenderer) => {
const MaterialLabelRendererComponent = ({ uischema, visible }: OwnPropsOfRenderer) => {
const labelElement: LabelElement = uischema as LabelElement;
return (
<Hidden xsUp={!visible}>
Expand All @@ -56,4 +56,5 @@ export const MaterialLabelRenderer = ({ uischema, visible }: OwnPropsOfRenderer)
);
};

export const MaterialLabelRenderer = React.memo(MaterialLabelRendererComponent);
export default withJsonFormsLayoutProps(MaterialLabelRenderer);
3 changes: 2 additions & 1 deletion packages/material/src/cells/MaterialBooleanCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
import { withJsonFormsCellProps } from '@jsonforms/react';
import { MuiCheckbox } from '../mui-controls/MuiCheckbox';

export const MaterialBooleanCell = (props: CellProps & WithClassname) => {
const MaterialBooleanCellComponent = (props: CellProps & WithClassname) => {
return <MuiCheckbox {...props} />;
};

Expand All @@ -42,4 +42,5 @@ export const materialBooleanCellTester: RankedTester = rankWith(
isBooleanControl
);

export const MaterialBooleanCell = React.memo(MaterialBooleanCellComponent);
export default withJsonFormsCellProps(MaterialBooleanCell);
3 changes: 2 additions & 1 deletion packages/material/src/cells/MaterialBooleanToggleCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
import { withJsonFormsCellProps } from '@jsonforms/react';
import { MuiToggle } from '../mui-controls/MuiToggle';

export const MaterialBooleanToggleCell = (props: CellProps & WithClassname) => {
const MaterialBooleanToggleCellComponent = (props: CellProps & WithClassname) => {
return <MuiToggle {...props} />;
};

Expand All @@ -44,4 +44,5 @@ export const materialBooleanToggleCellTester: RankedTester = rankWith(
and(isBooleanControl, optionIs('toggle', true))
);;

export const MaterialBooleanToggleCell = React.memo(MaterialBooleanToggleCellComponent);
export default withJsonFormsCellProps(MaterialBooleanToggleCell);
3 changes: 2 additions & 1 deletion packages/material/src/cells/MaterialDateCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { withJsonFormsCellProps } from '@jsonforms/react';
import Input from '@material-ui/core/Input';
import merge from 'lodash/merge';

export const MaterialDateCell = (props: CellProps & WithClassname) => {
const MaterialDateCellComponent = (props: CellProps & WithClassname) => {
const {
data,
className,
Expand Down Expand Up @@ -63,4 +63,5 @@ export const MaterialDateCell = (props: CellProps & WithClassname) => {
};
export const materialDateCellTester: RankedTester = rankWith(2, isDateControl);

export const MaterialDateCell = React.memo(MaterialDateCellComponent);
export default withJsonFormsCellProps(MaterialDateCell);
3 changes: 2 additions & 1 deletion packages/material/src/cells/MaterialEnumCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
import { withJsonFormsEnumCellProps } from '@jsonforms/react';
import { MuiSelect } from '../mui-controls/MuiSelect';

export const MaterialEnumCell = (props: EnumCellProps & WithClassname) => (
const MaterialEnumCellComponent = (props: EnumCellProps & WithClassname) => (
<MuiSelect {...props} />
);

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

export const MaterialEnumCell = React.memo(MaterialEnumCellComponent);
export default withJsonFormsEnumCellProps(MaterialEnumCell);
3 changes: 2 additions & 1 deletion packages/material/src/cells/MaterialIntegerCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ import {
import { withJsonFormsCellProps } from '@jsonforms/react';
import { MuiInputInteger } from '../mui-controls/MuiInputInteger';

export const MaterialIntegerCell = (props: CellProps & WithClassname) => (
const MaterialIntegerCellComponent = (props: CellProps & WithClassname) => (
<MuiInputInteger {...props} />
);
export const materialIntegerCellTester: RankedTester = rankWith(
2,
isIntegerControl
);

export const MaterialIntegerCell = React.memo(MaterialIntegerCellComponent);
export default withJsonFormsCellProps(MaterialIntegerCell);
4 changes: 3 additions & 1 deletion packages/material/src/cells/MaterialNumberCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
import { withJsonFormsCellProps } from '@jsonforms/react';
import { MuiInputNumber } from '../mui-controls/MuiInputNumber';

export const MaterialNumberCell = (props: CellProps & WithClassname) => (
const MaterialNumberCellComponent = (props: CellProps & WithClassname) => (
<MuiInputNumber {...props} />
);
/**
Expand All @@ -44,4 +44,6 @@ export const materialNumberCellTester: RankedTester = rankWith(
2,
isNumberControl
);

export const MaterialNumberCell = React.memo(MaterialNumberCellComponent);
export default withJsonFormsCellProps(MaterialNumberCell);
3 changes: 2 additions & 1 deletion packages/material/src/cells/MaterialNumberFormatCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
import { withJsonFormsCellProps } from '@jsonforms/react';
import { MuiInputNumberFormat } from '../mui-controls/MuiInputNumberFormat';

export const MaterialNumberFormatCell = (
const MaterialNumberFormatCellComponent = (
props: CellProps & WithClassname & Formatted<number>
) => <MuiInputNumberFormat {...props} />;
/**
Expand All @@ -46,4 +46,5 @@ export const materialNumberFormatCellTester: RankedTester = rankWith(
isNumberFormatControl
);

export const MaterialNumberFormatCell = React.memo(MaterialNumberFormatCellComponent);
export default withJsonFormsCellProps(MaterialNumberFormatCell);
3 changes: 2 additions & 1 deletion packages/material/src/cells/MaterialOneOfEnumCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
import { withJsonFormsOneOfEnumCellProps } from '@jsonforms/react';
import { MuiSelect } from '../mui-controls/MuiSelect';

export const MaterialOneOfEnumCell = (props: EnumCellProps & WithClassname) => (
const MaterialOneOfEnumCellComponent = (props: EnumCellProps & WithClassname) => (
<MuiSelect {...props} />
);

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

export const MaterialOneOfEnumCell = React.memo(MaterialOneOfEnumCellComponent);
export default withJsonFormsOneOfEnumCellProps(MaterialOneOfEnumCell);
3 changes: 2 additions & 1 deletion packages/material/src/cells/MaterialTextCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
import { withJsonFormsCellProps } from '@jsonforms/react';
import { MuiInputText } from '../mui-controls/MuiInputText';

export const MaterialTextCell = (props: CellProps & WithClassname) => (
const MaterialTextCellComponent = (props: CellProps & WithClassname) => (
<MuiInputText {...props} />
);

Expand All @@ -46,4 +46,5 @@ export const materialTextCellTester: RankedTester = rankWith(
isStringControl
);

export const MaterialTextCell = React.memo(MaterialTextCellComponent);
export default withJsonFormsCellProps(MaterialTextCell);
4 changes: 3 additions & 1 deletion packages/material/src/cells/MaterialTimeCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ import {
import { withJsonFormsCellProps } from '@jsonforms/react';
import { MuiInputTime } from '../mui-controls/MuiInputTime';

export const MaterialTimeCell = (props: CellProps & WithClassname) => (
const MaterialTimeCellComponent = (props: CellProps & WithClassname) => (
<MuiInputTime {...props} />
);
export const materialTimeCellTester: RankedTester = rankWith(2, isTimeControl);

export const MaterialTimeCell = React.memo(MaterialTimeCellComponent);
export default withJsonFormsCellProps(MaterialTimeCell);
4 changes: 3 additions & 1 deletion packages/material/src/complex/MaterialAllOfRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
} from '@jsonforms/core';
import { JsonFormsDispatch, withJsonFormsAllOfProps } from '@jsonforms/react';

const MaterialAllOfRenderer = ({
const MaterialAllOfRendererComponent = ({
schema,
rootSchema,
visible,
Expand Down Expand Up @@ -95,4 +95,6 @@ export const materialAllOfControlTester: RankedTester = rankWith(
3,
isAllOfControl
);

export const MaterialAllOfRenderer = React.memo(MaterialAllOfRendererComponent);
export default withJsonFormsAllOfProps(MaterialAllOfRenderer);
4 changes: 3 additions & 1 deletion packages/material/src/complex/MaterialAnyOfRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { JsonFormsDispatch, withJsonFormsAnyOfProps } from '@jsonforms/react';
import { Hidden, Tab, Tabs } from '@material-ui/core';
import CombinatorProperties from './CombinatorProperties';

const MaterialAnyOfRenderer = ({
const MaterialAnyOfRendererComponent = ({
schema,
rootSchema,
indexOfFittingSchema,
Expand Down Expand Up @@ -97,4 +97,6 @@ export const materialAnyOfControlTester: RankedTester = rankWith(
3,
isAnyOfControl
);

export const MaterialAnyOfRenderer = React.memo(MaterialAnyOfRendererComponent);
export default withJsonFormsAnyOfProps(MaterialAnyOfRenderer);
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { MaterialTableControl } from './MaterialTableControl';
import { Hidden } from '@material-ui/core';
import { DeleteDialog } from './DeleteDialog';

export const MaterialArrayControlRenderer = (props: ArrayLayoutProps) => {
export const MaterialArrayControlRendererComponent = (props: ArrayLayoutProps) => {
const [open, setOpen] = useState(false);
const [path, setPath] = useState(undefined);
const [rowData, setRowData] = useState(undefined);
Expand Down Expand Up @@ -64,4 +64,5 @@ export const MaterialArrayControlRenderer = (props: ArrayLayoutProps) => {
);
};

export const MaterialArrayControlRenderer = React.memo(MaterialArrayControlRendererComponent);
export default withJsonFormsArrayLayoutProps(MaterialArrayControlRenderer);
3 changes: 2 additions & 1 deletion packages/material/src/complex/MaterialEnumArrayRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { startCase } from 'lodash';
import { isEmpty } from 'lodash';
import React from 'react';

export const MaterialEnumArrayRenderer = ({
const MaterialEnumArrayRendererComponent = ({
schema,
visible,
errors,
Expand Down Expand Up @@ -108,4 +108,5 @@ export const materialEnumArrayRendererTester: RankedTester = rankWith(
)
);

export const MaterialEnumArrayRenderer = React.memo(MaterialEnumArrayRendererComponent);
export default withJsonFormsMultiEnumProps(MaterialEnumArrayRenderer);
4 changes: 3 additions & 1 deletion packages/material/src/complex/MaterialObjectRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { JsonFormsDispatch, withJsonFormsDetailProps } from '@jsonforms/react';
import { Hidden } from '@material-ui/core';
import React, { useMemo } from 'react';

const MaterialObjectRenderer = ({
const MaterialObjectRendererComponent = ({
renderers,
cells,
uischemas,
Expand Down Expand Up @@ -84,4 +84,6 @@ export const materialObjectControlTester: RankedTester = rankWith(
2,
isObjectControl
);

export const MaterialObjectRenderer = React.memo(MaterialObjectRendererComponent);
export default withJsonFormsDetailProps(MaterialObjectRenderer);
8 changes: 5 additions & 3 deletions packages/material/src/complex/MaterialOneOfRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import React, { useCallback, useState } from 'react';
import isEmpty from 'lodash/isEmpty';

import {
CombinatorProps,
CombinatorRendererProps,
createCombinatorRenderInfos,
createDefaultValue,
isOneOfControl,
Expand Down Expand Up @@ -58,8 +58,8 @@ export interface OwnOneOfProps extends OwnPropsOfControl {
}

const oneOf = 'oneOf';
const MaterialOneOfRenderer =
({ handleChange, schema, path, renderers, cells, rootSchema, id, visible, indexOfFittingSchema, uischema, uischemas, data }: CombinatorProps) => {
const MaterialOneOfRendererComponent =
({ handleChange, schema, path, renderers, cells, rootSchema, id, visible, indexOfFittingSchema, uischema, uischemas, data }: CombinatorRendererProps) => {
const [open, setOpen] = useState(false);
const [selectedIndex, setSelectedIndex] = useState(indexOfFittingSchema || 0);
const [newSelectedIndex, setNewSelectedIndex] = useState(0);
Expand Down Expand Up @@ -150,4 +150,6 @@ const MaterialOneOfRenderer =
};

export const materialOneOfControlTester: RankedTester = rankWith(3, isOneOfControl);

export const MaterialOneOfRenderer = React.memo(MaterialOneOfRendererComponent);
export default withJsonFormsOneOfProps(MaterialOneOfRenderer);
Loading

0 comments on commit 096815f

Please sign in to comment.