diff --git a/CHANGELOG.md b/CHANGELOG.md index ae26ce189..05f8b4500 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/lib/components/json-schema/templates/ObjectFieldTemplate.tsx b/src/lib/components/json-schema/templates/ObjectFieldTemplate.tsx index 06617cdfd..f41476afa 100644 --- a/src/lib/components/json-schema/templates/ObjectFieldTemplate.tsx +++ b/src/lib/components/json-schema/templates/ObjectFieldTemplate.tsx @@ -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 = ( props: ObjectFieldTemplateProps @@ -39,8 +52,21 @@ const ObjectFieldTemplate = ( /> )} - {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)[element.name]) + ) + (formData as Record)[element.name] = []; + + return element.hidden ? ( element.content ) : ( ( > {element.content} - ) - )} + ); + })} {canExpand(schema, uiSchema, formData) && (