Skip to content

Commit

Permalink
Enhancements to OrderableDocumentList: Custom createIntent Handling &…
Browse files Browse the repository at this point in the history
… menuItems injection (#75)

* feat: allow menuItems and create intent to be customized

* refactor: update README to include new usage
  • Loading branch information
williamli authored Jan 22, 2024
1 parent 3149cae commit f5be62e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export default defineConfig({
params: {
lang: 'en_US',
},
createIntent: false, // do not add an option for item creation
menuItems: [], // allow an array of `S.menuItem()` to be injected to orderable document list menu
// pass from the structure callback params above
S,
context,
Expand Down
19 changes: 13 additions & 6 deletions src/desk-structure/orderableDocumentListDeskItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {GenerateIcon, SortIcon} from '@sanity/icons'
import type {ConfigContext} from 'sanity'

import {ComponentType} from 'react'
import {StructureBuilder, type ListItem} from 'sanity/desk'
import {StructureBuilder, type ListItem, type MenuItem} from 'sanity/desk'
import OrderableDocumentList from '../OrderableDocumentList'

export interface OrderableListConfig {
Expand All @@ -12,6 +12,8 @@ export interface OrderableListConfig {
icon?: ComponentType
params?: Record<string, unknown>
filter?: string
menuItems?: MenuItem[]
createIntent?: boolean
context: ConfigContext
S: StructureBuilder
}
Expand All @@ -25,7 +27,7 @@ export function orderableDocumentListDeskItem(config: OrderableListConfig): List
`)
}

const {type, filter, params, title, icon, id, context, S} = config
const {type, filter, menuItems = [], createIntent, params, title, icon, id, context, S} = config
const {schema, getClient} = context
const client = getClient({apiVersion: '2021-09-01'})

Expand All @@ -34,6 +36,14 @@ export function orderableDocumentListDeskItem(config: OrderableListConfig): List
const listIcon = icon ?? SortIcon
const typeTitle = schema.get(type)?.title ?? type

if (createIntent !== false) {
menuItems.push(
S.menuItem()
.title(`Create new ${typeTitle}`)
.intent({type: 'create', params: {type}})
.serialize()
)
}
return S.listItem()
.title(listTitle)
.id(listId)
Expand All @@ -49,10 +59,7 @@ export function orderableDocumentListDeskItem(config: OrderableListConfig): List
component: OrderableDocumentList,
options: {type, filter, params, client},
menuItems: [
S.menuItem()
.title(`Create new ${typeTitle}`)
.intent({type: 'create', params: {type}})
.serialize(),
...menuItems,
S.menuItem().title(`Reset Order`).icon(GenerateIcon).action(`resetOrder`).serialize(),
S.menuItem()
.title(`Toggle Increments`)
Expand Down

0 comments on commit f5be62e

Please sign in to comment.