From 8ebb4f6139f1d113f12a98d83b95a16576372beb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rge=20N=C3=A6ss?= Date: Fri, 25 Aug 2017 11:13:19 +0200 Subject: [PATCH] [form-builder] Fix an issue with arrays and anonymous objects (#140) --- .../form-builder/src/inputs/Array/Array.js | 6 ++++-- .../form-builder/src/inputs/Array/ItemValue.js | 4 +++- packages/test-studio/schemas/arrays.js | 18 +++++++++++++++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/@sanity/form-builder/src/inputs/Array/Array.js b/packages/@sanity/form-builder/src/inputs/Array/Array.js index 9e10bdd6532..8e339645887 100644 --- a/packages/@sanity/form-builder/src/inputs/Array/Array.js +++ b/packages/@sanity/form-builder/src/inputs/Array/Array.js @@ -9,6 +9,7 @@ import styles from './styles/Array.css' import randomKey from './randomKey' import PatchEvent, {insert, setIfMissing, unset, set} from '../../PatchEvent' import resolveListComponents from './resolveListComponents' +import {resolveTypeName} from '../../utils/resolveTypeName' function hasKeys(object, exclude = []) { for (const key in object) { @@ -33,7 +34,7 @@ function createProtoValue(type): ItemValue { if (type.jsonType !== 'object') { throw new Error(`Invalid item type: "${type.type}". Default array input can only contain objects (for now)`) } - return { + return type.name === 'object' ? {_key: randomKey(12)} : { _type: type.name, _key: randomKey(12) } @@ -194,7 +195,8 @@ export default class ArrayInput extends React.Component<*, *, Stat getMemberTypeOfItem(item: T): ? Type { const {type} = this.props - return type.of.find(memberType => memberType.name === item._type) + const itemTypeName = resolveTypeName(item) + return type.of.find(memberType => memberType.name === itemTypeName) } renderList() { diff --git a/packages/@sanity/form-builder/src/inputs/Array/ItemValue.js b/packages/@sanity/form-builder/src/inputs/Array/ItemValue.js index 766e610fd6f..5e20fb725b2 100644 --- a/packages/@sanity/form-builder/src/inputs/Array/ItemValue.js +++ b/packages/@sanity/form-builder/src/inputs/Array/ItemValue.js @@ -16,6 +16,7 @@ import MemberValue from '../../Member' import PatchEvent from '../../PatchEvent' import {DragHandle} from 'part:@sanity/components/lists/sortable' +import {resolveTypeName} from '../../utils/resolveTypeName' type Props = { type: Type, @@ -58,7 +59,8 @@ export default class Item extends React.Component<*, Props, *> { getMemberType(): ?Type { const {value, type} = this.props - return type.of.find(memberType => memberType.name === value._type) + const itemTypeName = resolveTypeName(value) + return type.of.find(memberType => memberType.name === itemTypeName) } renderEditItemForm(item: any): ?React.Element { diff --git a/packages/test-studio/schemas/arrays.js b/packages/test-studio/schemas/arrays.js index 622f5167cb8..688056d4d3c 100644 --- a/packages/test-studio/schemas/arrays.js +++ b/packages/test-studio/schemas/arrays.js @@ -53,12 +53,28 @@ export default { }, { name: 'tags', - title: 'tags', + title: 'Tags', description: 'Enter a tag and press enter. Should result in an array of strings and should be possible to remove items', type: 'array', options: {layout: 'tags'}, of: [{type: 'string'}] }, + { + name: 'arrayWithAnonymousObject', + title: 'Array with anonymous objects', + description: 'This array contains objects of type as defined inline', + type: 'array', + of: [ + { + type: 'object', + title: 'Something', + fields: [ + {name: 'first', type: 'string', title: 'First string'}, + {name: 'second', type: 'string', title: 'Second string'} + ] + } + ] + }, { name: 'arrayOfStringsWithLegacyList', title: 'Array of strings with legacy format on lists',