Skip to content

Commit

Permalink
fix: #3131 ArrayField fallback to SchemaField (#3136)
Browse files Browse the repository at this point in the history
* fix: #3131 ArrayField fallback to SchemaField
- Updated `ArrayField` to fallback to `SchemaField` when `ArraySchemaField` was not defined in fields
  - Updated the test to show that the `SchemaField` fallback is working
- Updated the `fields/index.ts` to remove the definition of `ArraySchemaField` and replace it with a comment
- Updated the `custom-widgets-fields.md` file to clarify the `ArraySchemaField` docs based on this improvement
- Updated the `CHANGELOG.md` accordingly

* - Responded to reviewer feedback
  • Loading branch information
heath-freenome committed Sep 22, 2022
1 parent 295664f commit 355fc92
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ should change the heading of the (upcoming) version to include a major version b
- Implemented programmatic validation via new `validateForm()` method on `Form`, fixing (https://github.com/rjsf-team/react-jsonschema-form/issues/2755, https://github.com/rjsf-team/react-jsonschema-form/issues/2552, https://github.com/rjsf-team/react-jsonschema-form/issues/2381, https://github.com/rjsf-team/react-jsonschema-form/issues/2343, https://github.com/rjsf-team/react-jsonschema-form/issues/1006, https://github.com/rjsf-team/react-jsonschema-form/issues/246)
- Renamed `WithThemeProps` to `ThemeProps` to prevent another breaking-change by returning the type back to the name it had in version 4
- Pass `uiSchema` appropriately to all of the `IconButton`s, `ArrayFieldItemTemplate` and `WrapIfAdditional` components, fixing (https://github.com/rjsf-team/react-jsonschema-form/issues/3130)
- Updated `ArrayField` to fall back to `SchemaField` if `ArraySchemaField` is not defined, fixing (https://github.com/rjsf-team/react-jsonschema-form/issues/3131)

## @rjsf/fluent-ui
- Updated `Theme` to use the renamed `ThemeProps` from `@rjsf/core`
Expand Down Expand Up @@ -62,6 +63,7 @@ should change the heading of the (upcoming) version to include a major version b
- Fixed the `chakra-ui` custom `uiSchema` documentation to make it clear they work on a per-field basis, fixing (https://github.com/rjsf-team/react-jsonschema-form/issues/2865)
- Added `formElement` breaking-change documentation to the `5.x upgrade guide.md`
- Replace Webpack with Vite
- Updated documentation for `ArraySchemaField` to better represent the updated implementation, fixing (https://github.com/rjsf-team/react-jsonschema-form/issues/3131)

# 5.0.0-beta.8

Expand Down
4 changes: 2 additions & 2 deletions docs/advanced-customization/custom-widgets-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ Props passed to a custom SchemaField are the same as [the ones passed to a custo
### Custom ArraySchemaField

Everything that was mentioned above for a `Custom SchemaField` applies, but this is only used to render the Array item `children` that are then passed to the `ArrayFieldItemTemplate`.
By default, `ArraySchemaField` uses the main `SchemaField` implementation.
If you want to customize how the individual items for an array are rendered, override the `ArraySchemaField`.
By default, `ArraySchemaField` is not actually implemented in the `fields` list since `ArrayField` falls back to `SchemaField` if `ArraySchemaField` is not provided.
If you want to customize how the individual items for an array are rendered, provide your implementation of `ArraySchemaField` as a `fields` override.

```jsx
import validator from '@rjsf/validator-ajv6';
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/components/fields/ArrayField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -805,8 +805,9 @@ class ArrayField<T = any, F = any> extends Component<
formContext,
} = this.props;
const {
fields: { ArraySchemaField },
fields: { ArraySchemaField, SchemaField },
} = registry;
const ItemSchemaField = ArraySchemaField || SchemaField;
const { orderable = true, removable = true } = getUiOptions<T[], F>(
uiSchema
);
Expand All @@ -820,7 +821,7 @@ class ArrayField<T = any, F = any> extends Component<

return {
children: (
<ArraySchemaField
<ItemSchemaField
name={name}
index={index}
schema={itemSchema}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/fields/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import NullField from "./NullField";
const fields: RegistryFieldsType = {
AnyOfField: MultiSchemaField,
ArrayField,
ArraySchemaField: SchemaField,
// ArrayField falls back to SchemaField if ArraySchemaField is not defined, which it isn't by default
BooleanField,
NumberField,
ObjectField,
Expand Down
10 changes: 3 additions & 7 deletions packages/core/test/ArrayField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2192,7 +2192,7 @@ describe("ArrayField", () => {
},
],
};
it("should pass form context to schema field for the root", () => {
it("should pass form context to schema field for the root AND array schema fields", () => {
const formContext = {
root: "root-id",
root_0: "root_0-id",
Expand All @@ -2214,13 +2214,9 @@ describe("ArrayField", () => {
});

const codeBlocks = node.querySelectorAll("code");
expect(codeBlocks).to.have.length(1);
expect(codeBlocks).to.have.length(3);
Object.keys(formContext).forEach((key) => {
if (key === "root") {
expect(node.querySelector(`code#${formContext[key]}`)).to.exist;
} else {
expect(node.querySelector(`code#${formContext[key]}`)).to.not.exist;
}
expect(node.querySelector(`code#${formContext[key]}`)).to.exist;
});
});
it("should pass form context to array schema field only", () => {
Expand Down

0 comments on commit 355fc92

Please sign in to comment.