Skip to content

Commit

Permalink
fix(form): use most specific title for schema type in modal header
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Aug 13, 2022
1 parent 2e00ef3 commit 06f862a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {Subscription} from 'rxjs'
import {randomKey, resolveTypeName} from '@sanity/util/content'
import {SanityClient} from '@sanity/client'
import {Uploader, UploaderResolver, UploadEvent} from '../../../studio/uploads/types'
import {getSchemaTypeTitle} from '../../../../schema/helpers'
import {isDev} from '../../../../environment'
import {Alert} from '../../../components/Alert'
import {Details} from '../../../components/Details'
Expand Down Expand Up @@ -264,6 +265,8 @@ export class ArrayInput extends React.PureComponent<ArrayInputProps> {
if (isReferenceSchemaType(itemProps.schemaType)) {
return itemProps.children
}

const typeTitle = getSchemaTypeTitle(itemProps.schemaType)
return (
<>
<ArrayItem
Expand All @@ -287,7 +290,7 @@ export class ArrayInput extends React.PureComponent<ArrayInputProps> {
{itemProps.open ? (
<Dialog
width={1}
header={`Edit ${itemProps.schemaType.title}`}
header={`Edit ${typeTitle}`}
id={`${id}-item-${itemProps.key}-dialog`}
onClose={itemProps.onClose}
>
Expand Down
22 changes: 22 additions & 0 deletions packages/sanity/src/schema/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type {SchemaType} from '@sanity/types'

/**
* Get the most specific defined title of a schema type
* If not set directly on the given type, it will traverse up the tree until it
* finds one, falling back to the _name_ of the type.
*
* @param type - The schema type to get the title of
* @returns A title, alternatively the schema type _name_
* @internal
*/
export function getSchemaTypeTitle(type: SchemaType): string {
if (typeof type.title === 'string') {
return type.title
}

if (type.type) {
return getSchemaTypeTitle(type.type)
}

return type.name || type.jsonType
}

0 comments on commit 06f862a

Please sign in to comment.