Skip to content

Commit

Permalink
[structure] Try to infer schema type from filter in document lists (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Nov 29, 2019
1 parent 6c9d0a9 commit daae65f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 11 additions & 0 deletions packages/@sanity/structure/src/DocumentList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ export class DocumentListBuilder extends GenericListBuilder<
builder.spec.initialValueTemplates = inferInitialValueTemplates(builder.spec)
}

if (!this.spec.schemaTypeName) {
builder.spec.schemaTypeName = inferTypeName(builder.spec)
}

return builder
}

Expand Down Expand Up @@ -196,6 +200,13 @@ function inferInitialValueTemplates(
}, templateItems)
}

function inferTypeName(spec: PartialDocumentList): string | undefined {
const {options} = spec
const {filter, params} = options || {filter: '', params: {}}
const typeNames = getTypeNamesFromFilter(filter, params)
return typeNames.length === 1 ? typeNames[0] : undefined
}

export function getTypeNamesFromFilter(
filter: string,
params: {[key: string]: any} = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Object {
"type": "book",
},
},
"schemaTypeName": undefined,
"schemaTypeName": "book",
"title": "Books",
"type": "documentList",
}
Expand Down Expand Up @@ -50,7 +50,7 @@ Object {
"type": "book",
},
},
"schemaTypeName": undefined,
"schemaTypeName": "book",
"title": "Books",
"type": "documentList",
}
Expand Down Expand Up @@ -78,7 +78,7 @@ Object {
"type": "book",
},
},
"schemaTypeName": undefined,
"schemaTypeName": "book",
"title": "Books",
"type": "documentList",
}
Expand Down

0 comments on commit daae65f

Please sign in to comment.