Skip to content

Commit

Permalink
[form-builder] Fix an issue with arrays and anonymous objects (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Aug 25, 2017
1 parent 80d1228 commit 8ebb4f6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
6 changes: 4 additions & 2 deletions packages/@sanity/form-builder/src/inputs/Array/Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down Expand Up @@ -194,7 +195,8 @@ export default class ArrayInput<T: ItemValue> 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() {
Expand Down
4 changes: 3 additions & 1 deletion packages/@sanity/form-builder/src/inputs/Array/ItemValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -58,7 +59,8 @@ export default class Item<T: ItemValue> 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<any> {
Expand Down
18 changes: 17 additions & 1 deletion packages/test-studio/schemas/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 8ebb4f6

Please sign in to comment.