Skip to content

Commit 446c0c9

Browse files
Chore: Convert ObjectField to a stateless component
The work to convert `ObjectField` to a stateless functional component with some optimizations
1 parent db40580 commit 446c0c9

File tree

37 files changed

+468
-353
lines changed

37 files changed

+468
-353
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export default function FieldTemplate<
3434
hidden,
3535
id,
3636
label,
37+
onRemovePropertyClick,
38+
onKeyRenameBlur,
3739
onDropPropertyClick,
3840
onKeyChange,
3941
rawErrors,
@@ -86,6 +88,8 @@ export default function FieldTemplate<
8688
disabled={disabled}
8789
id={id}
8890
label={label}
91+
onRemovePropertyClick={onRemovePropertyClick}
92+
onKeyRenameBlur={onKeyRenameBlur}
8993
onDropPropertyClick={onDropPropertyClick}
9094
onKeyChange={onKeyChange}
9195
readonly={readonly}

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+
onRemovePropertyClick,
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={onRemovePropertyClick}
116113
uiSchema={buttonUiOptions}
117114
registry={registry}
118115
/>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export default function FieldTemplate<
2222
displayLabel,
2323
hidden,
2424
label,
25+
onRemovePropertyClick,
26+
onKeyRenameBlur,
2527
onDropPropertyClick,
2628
onKeyChange,
2729
readonly,
@@ -53,6 +55,8 @@ export default function FieldTemplate<
5355
disabled={disabled}
5456
id={id}
5557
label={label}
58+
onRemovePropertyClick={onRemovePropertyClick}
59+
onKeyRenameBlur={onKeyRenameBlur}
5660
onDropPropertyClick={onDropPropertyClick}
5761
onKeyChange={onKeyChange}
5862
readonly={readonly}

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+
onRemovePropertyClick,
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={onRemovePropertyClick}
7269
uiSchema={uiSchema}
7370
registry={registry}
7471
/>

packages/core/src/components/fields/ArrayField.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,8 @@ class ArrayField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
519519
const _schemaItems: S = isObject(schema.items) ? (schema.items as S) : ({} as S);
520520
const itemsSchema: S = schemaUtils.retrieveSchema(_schemaItems);
521521
const formData = keyedToPlainFormData(this.state.keyedFormData);
522-
const renderOptionalField = shouldRenderOptionalField(registry, schema, required, uiSchema);
523-
const hasFormData = isFormDataAvailable(this.props.formData);
522+
const renderOptionalField = shouldRenderOptionalField<T[], S, F>(registry, schema, required, uiSchema);
523+
const hasFormData = isFormDataAvailable<T[]>(this.props.formData);
524524
const canAdd = this.canAddItem(formData) && (!renderOptionalField || hasFormData);
525525
const actualFormData = hasFormData ? keyedFormData : [];
526526
const extraClass = renderOptionalField ? ' rjsf-optional-array-field' : '';
@@ -752,8 +752,8 @@ class ArrayField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
752752
const uiOptions = getUiOptions<T[], S, F>(uiSchema);
753753
const { schemaUtils, fields, formContext, globalFormOptions } = registry;
754754
const { OptionalDataControlsField } = fields;
755-
const renderOptionalField = shouldRenderOptionalField(registry, schema, required, uiSchema);
756-
const hasFormData = isFormDataAvailable(formData);
755+
const renderOptionalField = shouldRenderOptionalField<T[], S, F>(registry, schema, required, uiSchema);
756+
const hasFormData = isFormDataAvailable<T[]>(formData);
757757
const _schemaItems: S[] = isObject(schema.items) ? (schema.items as S[]) : ([] as S[]);
758758
const itemSchemas = _schemaItems.map((item: S, index: number) =>
759759
schemaUtils.retrieveSchema(item, items[index] as unknown as T[]),

packages/core/src/components/fields/LayoutMultiSchemaField.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ export default function LayoutMultiSchemaField<
195195
displayLabel={displayLabel}
196196
errors={errors}
197197
onChange={onChange}
198+
onKeyRenameBlur={noop}
199+
onRemovePropertyClick={noop}
198200
onDropPropertyClick={ignored}
199201
onKeyChange={ignored}
200202
>

packages/core/src/components/fields/MultiSchemaField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class AnyOfField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
167167
globalUiOptions,
168168
);
169169
const isOptionalRender = shouldRenderOptionalField<T, S, F>(registry, schema, required, uiSchema);
170-
const hasFormData = isFormDataAvailable(formData);
170+
const hasFormData = isFormDataAvailable<T>(formData);
171171

172172
const { selectedOption, retrievedOptions } = this.state;
173173
const {

0 commit comments

Comments
 (0)