Skip to content

Commit 803c813

Browse files
Chore: Convert ObjectField to function components (rjsf-team#4806)
* Chore: Convert ObjectField to a stateless component The work to convert `ObjectField` to a stateless functional component with some optimizations * - Responded to code walkthru feedback * - Updated documentation * - Added `useFieldPathId()` * - Responded to reviewer feedback
1 parent db40580 commit 803c813

File tree

47 files changed

+686
-450
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+686
-450
lines changed

CHANGELOG.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,61 @@ should change the heading of the (upcoming) version to include a major version b
2020
## @rjsf/antd
2121

2222
- Updated most of the widgets to get `formContext` from the `registry` instead of the `props` since it will no longer be passed
23+
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
24+
25+
## @rjsf/chakra-ui
26+
27+
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
2328

2429
## @rjsf/core
2530

2631
- Updated `MultiSchemaField` and `SchemaField` to properly display `anyOf`/`oneOf` optional data fields by hiding the label and selector control when it is an optional field AND there is no form data
2732
- Updated `ArrayField`, `BooleanField`, `LayoutMultiSchemaField`, `MultiSchemaField`, `ObjectField`, `SchemaField`, `StringField` and `BaseInputTemplate` to remove `formContext` from the props
33+
- Updated `ObjectField` to refactor the code from a class component to two stateless functional components, replacing the 3 generator-props with the 4 memoized props mentioned in the `@rjsf/utils` changes
34+
- Updated `Form` to "memoize" the `fieldPathId` and `registry` into the `FormState`, adding a `toIChangeEvent()` helper to restrict the state returned on the `IChangeEvent` interface callbacks
35+
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
2836

2937
## @rjsf/daisyui
3038

3139
- Updated the test mocks to remove `formContext` for the widget mock
40+
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
41+
42+
## @rjsf/fluentui-rc
43+
44+
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
45+
46+
## @rjsf/mantine
47+
48+
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
3249

3350
## @rjsf/mui
3451

3552
- Updated `BaseInputTemplate` and `SelectWidget` to remove `formContext` from the props
53+
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
3654

3755
## @rjsf/primereact
3856

3957
- Updated `SelectWidget` to remove `formContext` from the props
58+
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
59+
60+
## @rjsf/react-bootstrap
61+
62+
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
63+
64+
## @rjsf/semantic-ui
65+
66+
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
4067

4168
## @rjsf/shadcn
4269

4370
- Updated the test mocks to remove `formContext` for the widget mock and added `globalFormOptions` in the registry mock
71+
- Updated `FieldTemplate`, `ObjectFieldTemplate` and `WrapIfAdditionalTemplate` to rename the old `additionalProperties` interface props to the new ones
72+
73+
## @rjsf/utils
74+
75+
- BREAKING CHANGE: Updated `FieldTemplateProps` and `WrapIfAdditionalTemplateProps` to replace the `onKeyChange()` and `onDropPropertyClick()` callback generator props with the `onKeyRename()`, `onKeyRenameBlur()` and `onRemoveProperty()` callback props
76+
- BREAKING CHANGE: Updated `ObjectFieldTemplateProps` to replace the `onAddClick()` callback generator prop with the `onAddProperty()` callback prop
77+
- Added new hook `useDeepCompareMemo()` and its associated tests
4478

4579
## Dev / docs / playground
4680
- Updated the `formTests.tsx` snapshots to add an `anyOf` of all arrays with different item types and removed the disabling of the optional data controls feature for the optional object with oneOfs
@@ -49,6 +83,9 @@ should change the heading of the (upcoming) version to include a major version b
4983
- Updated the `Sample` and `LiveSettings` types to support the `liveSettings` inside of a sample
5084
- Updated the `Playground`'s `onSampleSelected` callback to merge any `liveSettings` in the sample on top of those already used in the playground
5185
- Updated the `customFieldAnyOf` sample to switch `IdSchema` to `FieldPathId`
86+
- Updated the `custom-templates.md` documentation to reflect the `additionalProperties`-based interface props replacement
87+
- Updated the `utility-functions.mf` documenation to add the new `useDeepCompareMemo()` hook
88+
- Updated the `v6.x upgrade guide.md` documentation to add the BREAKING CHANGES to the `FieldTemplateProps`, `ObjectFieldTemplateProps` and `WrapIfAdditionalTemplateProps` interface props changes and the `useDeepCompareMemo()` hook
5289

5390
# 6.0.0-beta.21
5491

packages/antd/src/templates/FieldTemplate/index.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ export default function FieldTemplate<
3434
hidden,
3535
id,
3636
label,
37-
onDropPropertyClick,
38-
onKeyChange,
37+
onKeyRename,
38+
onKeyRenameBlur,
39+
onRemoveProperty,
3940
rawErrors,
4041
rawDescription,
4142
rawHelp,
@@ -86,8 +87,9 @@ export default function FieldTemplate<
8687
disabled={disabled}
8788
id={id}
8889
label={label}
89-
onDropPropertyClick={onDropPropertyClick}
90-
onKeyChange={onKeyChange}
90+
onKeyRename={onKeyRename}
91+
onKeyRenameBlur={onKeyRenameBlur}
92+
onRemoveProperty={onRemoveProperty}
9193
readonly={readonly}
9294
required={required}
9395
schema={schema}

packages/antd/src/templates/ObjectFieldTemplate/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default function ObjectFieldTemplate<
4040
disabled,
4141
formData,
4242
fieldPathId,
43-
onAddClick,
43+
onAddProperty,
4444
optionalDataControl,
4545
properties,
4646
readonly,
@@ -160,7 +160,7 @@ export default function ObjectFieldTemplate<
160160
id={buttonId(fieldPathId, 'add')}
161161
className='rjsf-object-property-expand'
162162
disabled={disabled || readonly}
163-
onClick={onAddClick(schema)}
163+
onClick={onAddProperty}
164164
uiSchema={uiSchema}
165165
registry={registry}
166166
/>

packages/antd/src/templates/WrapIfAdditionalTemplate/index.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { FocusEvent } from 'react';
21
import { Col, Row, Form, Input } from 'antd';
32
import {
43
ADDITIONAL_PROPERTY_FLAG,
@@ -35,8 +34,8 @@ export default function WrapIfAdditionalTemplate<
3534
disabled,
3635
id,
3736
label,
38-
onDropPropertyClick,
39-
onKeyChange,
37+
onRemoveProperty,
38+
onKeyRenameBlur,
4039
readonly,
4140
required,
4241
registry,
@@ -66,8 +65,6 @@ export default function WrapIfAdditionalTemplate<
6665
);
6766
}
6867

69-
const handleBlur = ({ target }: FocusEvent<HTMLInputElement>) => onKeyChange(target && target.value);
70-
7168
// The `block` prop is not part of the `IconButtonProps` defined in the template, so put it into the uiSchema instead
7269
const uiOptions = uiSchema ? uiSchema[UI_OPTIONS_KEY] : {};
7370
const buttonUiOptions = {
@@ -97,7 +94,7 @@ export default function WrapIfAdditionalTemplate<
9794
disabled={disabled || (readonlyAsDisabled && readonly)}
9895
id={`${id}-key`}
9996
name={`${id}-key`}
100-
onBlur={!readonly ? handleBlur : undefined}
97+
onBlur={!readonly ? onKeyRenameBlur : undefined}
10198
style={INPUT_STYLE}
10299
type='text'
103100
/>
@@ -112,7 +109,7 @@ export default function WrapIfAdditionalTemplate<
112109
id={buttonId(id, 'remove')}
113110
className='rjsf-object-property-remove'
114111
disabled={disabled || readonly}
115-
onClick={onDropPropertyClick(label)}
112+
onClick={onRemoveProperty}
116113
uiSchema={buttonUiOptions}
117114
registry={registry}
118115
/>

packages/chakra-ui/src/FieldTemplate/FieldTemplate.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ export default function FieldTemplate<
2222
displayLabel,
2323
hidden,
2424
label,
25-
onDropPropertyClick,
26-
onKeyChange,
25+
onKeyRename,
26+
onKeyRenameBlur,
27+
onRemoveProperty,
2728
readonly,
2829
registry,
2930
required,
@@ -53,8 +54,9 @@ export default function FieldTemplate<
5354
disabled={disabled}
5455
id={id}
5556
label={label}
56-
onDropPropertyClick={onDropPropertyClick}
57-
onKeyChange={onKeyChange}
57+
onKeyRename={onKeyRename}
58+
onKeyRenameBlur={onKeyRenameBlur}
59+
onRemoveProperty={onRemoveProperty}
5860
readonly={readonly}
5961
required={required}
6062
schema={schema}

packages/chakra-ui/src/ObjectFieldTemplate/ObjectFieldTemplate.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default function ObjectFieldTemplate<
2929
schema,
3030
formData,
3131
optionalDataControl,
32-
onAddClick,
32+
onAddProperty,
3333
registry,
3434
} = props;
3535
const uiOptions = getUiOptions<T, S, F>(uiSchema);
@@ -81,7 +81,7 @@ export default function ObjectFieldTemplate<
8181
<AddButton
8282
id={buttonId(fieldPathId, 'add')}
8383
className='rjsf-object-property-expand'
84-
onClick={onAddClick(schema)}
84+
onClick={onAddProperty}
8585
disabled={disabled || readonly}
8686
uiSchema={uiSchema}
8787
registry={registry}

packages/chakra-ui/src/WrapIfAdditionalTemplate/WrapIfAdditionalTemplate.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { FocusEvent } from 'react';
21
import {
32
ADDITIONAL_PROPERTY_FLAG,
43
buttonId,
@@ -24,8 +23,8 @@ export default function WrapIfAdditionalTemplate<
2423
disabled,
2524
id,
2625
label,
27-
onDropPropertyClick,
28-
onKeyChange,
26+
onRemoveProperty,
27+
onKeyRenameBlur,
2928
readonly,
3029
registry,
3130
required,
@@ -45,8 +44,6 @@ export default function WrapIfAdditionalTemplate<
4544
);
4645
}
4746

48-
const handleBlur = ({ target }: FocusEvent<HTMLInputElement>) => onKeyChange(target.value);
49-
5047
return (
5148
<Grid key={`${id}-key`} className={classNames} style={style} alignItems='center' gap={2}>
5249
<GridItem>
@@ -56,7 +53,7 @@ export default function WrapIfAdditionalTemplate<
5653
disabled={disabled || readonly}
5754
id={`${id}-key`}
5855
name={`${id}-key`}
59-
onBlur={!readonly ? handleBlur : undefined}
56+
onBlur={!readonly ? onKeyRenameBlur : undefined}
6057
type='text'
6158
mb={1}
6259
/>
@@ -68,7 +65,7 @@ export default function WrapIfAdditionalTemplate<
6865
id={buttonId(id, 'remove')}
6966
className='rjsf-object-property-remove'
7067
disabled={disabled || readonly}
71-
onClick={onDropPropertyClick(label)}
68+
onClick={onRemoveProperty}
7269
uiSchema={uiSchema}
7370
registry={registry}
7471
/>

0 commit comments

Comments
 (0)