Skip to content

Commit e91e11e

Browse files
authored
Merge pull request #252 from code0-tech/feat/#247
Default value for nested objects is wrongly created
2 parents 81fde7a + 5c18cd0 commit e91e11e

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

src/packages/ce/src/datatype/components/inputs/json/DataTypeJSONInputComponent.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from "@edition/datatype/components/inputs/json/DataTypeJSONInputEditDialogComponent";
1010
import {DataTypeInputValueComponent} from "@edition/datatype/components/inputs/DataTypeInputValueComponent";
1111
import {useDebouncedCallback} from "use-debounce";
12-
import {DataInput} from "@code0-tech/triangulum/dist/util/schema.util";
12+
import {DataInput, ListInput} from "@code0-tech/triangulum/dist/util/schema.util";
1313

1414
export interface EditableJSONEntry {
1515
key: string
@@ -19,7 +19,6 @@ export interface EditableJSONEntry {
1919

2020
export type DataTypeJSONInputComponentProps = DataTypeInputComponentProps
2121

22-
//TODO render fallback value if undefined based on schema
2322
export const DataTypeJSONInputComponent: React.FC<DataTypeJSONInputComponentProps> = (props) => {
2423

2524
const {schema, title, description, suggestions, formValidation, initialValue, onChange} = props
@@ -87,22 +86,23 @@ export const DataTypeJSONInputComponent: React.FC<DataTypeJSONInputComponentProp
8786
}
8887

8988

90-
//TODO: only if required
9189
const generateDefaultDataValue = (schema: DataInput): LiteralValue => {
9290
return {
9391
__typename: "LiteralValue",
94-
value: {
95-
...(Object.entries(schema.properties ?? {})?.map(([key, propSchema]) => {
96-
if (!Array.isArray(propSchema)) {
97-
if (propSchema.input === "data") {
98-
return {[key]: generateDefaultDataValue(propSchema).value}
99-
}
100-
if (propSchema.input === "list") {
101-
return {[key]: []}
92+
value: Object.assign({}, ...Object.entries(schema.properties ?? {}).map(([key, propSchema]) => {
93+
if (!Array.isArray(propSchema)) {
94+
if (propSchema.input === "data") {
95+
return {[key]: generateDefaultDataValue(propSchema).value}
96+
}
97+
if (propSchema.input === "list") {
98+
const itemSchema = (propSchema as ListInput).items?.[0]
99+
if (itemSchema && !Array.isArray(itemSchema) && itemSchema.input === "data") {
100+
return {[key]: [generateDefaultDataValue(itemSchema as DataInput).value]}
102101
}
103-
return {[key]: null}
102+
return {[key]: []}
104103
}
105-
}))
106-
}
104+
return {[key]: null}
105+
}
106+
}))
107107
}
108108
}

0 commit comments

Comments
 (0)