Skip to content

Commit 03c2f60

Browse files
authoredMar 11, 2025··
fix(core): arrays of references support disableActions (#8877)
1 parent 16919e6 commit 03c2f60

File tree

5 files changed

+56
-23
lines changed

5 files changed

+56
-23
lines changed
 

‎dev/test-studio/schema/debug/arrayCapabilities.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {defineType} from '@sanity/types'
22

3-
const DISABLED_ACTIONS = ['add', 'addBefore', 'addAfter', 'duplicate', 'remove', 'copy'] as const
3+
const DISABLED_ACTIONS = ['add', 'duplicate', 'addBefore', 'addAfter', 'remove', 'copy'] as const
44

55
export const arrayCapabilities = defineType({
6-
name: 'arrayCapabilitiesExample',
6+
name: 'arrayCapabilities',
77
type: 'document',
88
title: 'Array Capabilities test',
99
// icon,
@@ -31,6 +31,7 @@ export const arrayCapabilities = defineType({
3131
fields: [{name: 'first', type: 'string', title: 'First string'}],
3232
},
3333
],
34+
initialValue: [{_type: 'something', first: 'First'}],
3435
},
3536
{
3637
name: 'objectArrayAsGrid',
@@ -73,5 +74,17 @@ export const arrayCapabilities = defineType({
7374
},
7475
],
7576
},
77+
{
78+
name: 'arrayOfReferences',
79+
title: 'Array of references to authors',
80+
description: `With disabledActions: ${DISABLED_ACTIONS.join(', ')}`,
81+
type: 'array',
82+
options: {
83+
collapsible: true,
84+
collapsed: true,
85+
disableActions: DISABLED_ACTIONS,
86+
},
87+
of: [{type: 'reference', to: [{type: 'author'}]}],
88+
},
7689
],
7790
})

‎packages/sanity/src/core/form/inputs/ReferenceInput/ReferenceItem.tsx

+31-18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {ContextMenuButton} from '../../../components/contextMenuButton'
2525
import {LoadingBlock} from '../../../components/loadingBlock'
2626
import {useTranslation} from '../../../i18n'
2727
import {FieldPresence} from '../../../presence'
28+
import {EMPTY_ARRAY} from '../../../util/empty'
2829
import {FormFieldSet, FormFieldValidationStatus} from '../../components/formField'
2930
import {useDidUpdate} from '../../hooks/useDidUpdate'
3031
import {useScrollIntoViewOnFocusWithin} from '../../hooks/useScrollIntoViewOnFocusWithin'
@@ -210,6 +211,8 @@ export function ReferenceItem<Item extends ReferenceItemValue = ReferenceItemVal
210211
referenceElement: contextMenuButtonElement,
211212
})
212213

214+
const disableActions = parentSchemaType.options?.disableActions || EMPTY_ARRAY
215+
213216
const menu = useMemo(
214217
() =>
215218
readOnly ? null : (
@@ -230,12 +233,14 @@ export function ReferenceItem<Item extends ReferenceItemValue = ReferenceItemVal
230233
<Menu ref={menuRef}>
231234
{!readOnly && (
232235
<>
233-
<MenuItem
234-
text={t('inputs.reference.action.remove')}
235-
tone="critical"
236-
icon={TrashIcon}
237-
onClick={onRemove}
238-
/>
236+
{!disableActions.includes('remove') && (
237+
<MenuItem
238+
text={t('inputs.reference.action.remove')}
239+
tone="critical"
240+
icon={TrashIcon}
241+
onClick={onRemove}
242+
/>
243+
)}
239244
<MenuItem
240245
text={t(
241246
hasRef && isEditing
@@ -245,18 +250,25 @@ export function ReferenceItem<Item extends ReferenceItemValue = ReferenceItemVal
245250
icon={hasRef && isEditing ? CloseIcon : ReplaceIcon}
246251
onClick={handleReplace}
247252
/>
248-
<MenuItem
249-
text={t('inputs.reference.action.copy')}
250-
icon={CopyIcon}
251-
onClick={handleCopy}
252-
/>
253-
<MenuItem
254-
text={t('inputs.reference.action.duplicate')}
255-
icon={AddDocumentIcon}
256-
onClick={handleDuplicate}
257-
/>
258-
{insertBefore.menuItem}
259-
{insertAfter.menuItem}
253+
{!disableActions.includes('copy') && (
254+
<MenuItem
255+
text={t('inputs.reference.action.copy')}
256+
icon={CopyIcon}
257+
onClick={handleCopy}
258+
/>
259+
)}
260+
{!disableActions.includes('duplicate') && (
261+
<MenuItem
262+
text={t('inputs.reference.action.duplicate')}
263+
icon={AddDocumentIcon}
264+
onClick={handleDuplicate}
265+
/>
266+
)}
267+
{!(disableActions.includes('add') || disableActions.includes('addBefore')) &&
268+
insertBefore.menuItem}
269+
{!disableActions.includes('add') &&
270+
!disableActions.includes('addAfter') &&
271+
insertAfter.menuItem}
260272
</>
261273
)}
262274

@@ -279,6 +291,7 @@ export function ReferenceItem<Item extends ReferenceItemValue = ReferenceItemVal
279291
),
280292
[
281293
OpenLink,
294+
disableActions,
282295
handleCopy,
283296
handleDuplicate,
284297
handleReplace,

‎packages/sanity/src/structure/panes/document/statusBar/DocumentStatusBar.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface DocumentStatusBarProps {
2727

2828
const CONTAINER_BREAKPOINT = 480 // px
2929

30-
const AnimatedCard = motion(Card)
30+
const AnimatedCard = motion.create(Card)
3131

3232
export function DocumentStatusBar(props: DocumentStatusBarProps) {
3333
const {actionsBoxRef, createLinkMetadata} = props

‎playwright.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const playwrightConfig = createPlaywrightConfig({
6363
reporter: excludeGithub([['list'], ['blob']]),
6464
use: {
6565
...config.use,
66+
video: 'retain-on-failure',
6667
baseURL: 'http://localhost:3339',
6768
headless: HEADLESS,
6869
contextOptions: {reducedMotion: 'reduce'},

‎test/e2e/tests/inputs/array-capabilities.spec.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import {test as base} from '@sanity/test'
44

55
const test = base.extend<{testDoc: SanityDocument}>({
66
testDoc: async ({page, sanityClient}, use) => {
7+
const referenceDoc = await sanityClient.create({
8+
_type: 'author',
9+
name: 'Test Author',
10+
})
711
const testDoc = await sanityClient.create({
8-
_type: 'arrayCapabilitiesExample',
12+
_type: 'arrayCapabilities',
913
title: 'e2e fixture',
1014
objectArray: [
1115
{_type: 'something', _key: '5b75c4005e47', first: 'First'},
@@ -16,14 +20,16 @@ const test = base.extend<{testDoc: SanityDocument}>({
1620
{_type: 'something', _key: '630ae68957fb', first: 'Second'},
1721
],
1822
primitiveArray: ['First', 2],
23+
arrayOfReferences: [{_type: 'reference', _ref: referenceDoc._id}],
1924
})
2025
await use(testDoc)
2126
await sanityClient.delete(testDoc._id)
27+
await sanityClient.delete(referenceDoc._id)
2228
},
2329
})
2430

2531
test(`Scenario: Disabling all array capabilities`, async ({page, testDoc}) => {
26-
await page.goto(`/test/content/arrayCapabilitiesExample;${testDoc._id}`)
32+
await page.goto(`/test/content/input-debug;arrayCapabilities;${testDoc._id}`)
2733
await expect(page.getByTestId('document-panel-scroller')).toBeAttached({
2834
timeout: 40000,
2935
})

0 commit comments

Comments
 (0)
Please sign in to comment.