Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: json array field #470

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Bug fixes

- [#470](https://github.com/alleslabs/celatone-frontend/pull/470) Fix json schema array field default behavior
- [#456](https://github.com/alleslabs/celatone-frontend/pull/456) Fix pool count chip in pool transaction table
- [#454](https://github.com/alleslabs/celatone-frontend/pull/454) Fix contract selection loading state for other folders
- [#452](https://github.com/alleslabs/celatone-frontend/pull/452) Fix public project data on the account details and code details page still remains when switching network
Expand Down
38 changes: 32 additions & 6 deletions src/lib/components/json-schema/templates/ObjectFieldTemplate.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Grid, GridItem } from "@chakra-ui/react";
import type { ObjectFieldTemplateProps } from "@rjsf/utils";
import { canExpand, getTemplate, getUiOptions } from "@rjsf/utils";
import type {
ObjectFieldTemplatePropertyType,
ObjectFieldTemplateProps,
} from "@rjsf/utils";
import {
canExpand,
getSchemaType,
getTemplate,
getUiOptions,
} from "@rjsf/utils";

interface ObjectFieldTemplatePropertyTypeWithRequired
extends ObjectFieldTemplatePropertyType {
required?: boolean;
}

const ObjectFieldTemplate = <T = any, F = any>(
props: ObjectFieldTemplateProps<T, F>
Expand Down Expand Up @@ -39,17 +52,30 @@ const ObjectFieldTemplate = <T = any, F = any>(
/>
)}
<Grid gap={4} my={2}>
{properties.map((element, index) =>
element.hidden ? (
{properties.map((element, index) => {
// NOTE: required array field doesn't create an empty array as a default
const elementType = getSchemaType(element.content.props.schema);
const elementRequired = (
element as ObjectFieldTemplatePropertyTypeWithRequired
).required;
if (
typeof elementType === "string" &&
elementType === "array" &&
elementRequired &&
!Array.isArray((formData as Record<string, object>)[element.name])
)
(formData as Record<string, object>)[element.name] = [];

return element.hidden ? (
element.content
) : (
<GridItem
key={`${idSchema.$id}-${element.name}-${index.toString()}`}
>
{element.content}
</GridItem>
)
)}
);
})}
{canExpand<T, F>(schema, uiSchema, formData) && (
<GridItem justifySelf="flex-end">
<AddButton
Expand Down