From 90d69519b2a82c38b69a7c805519ef25afe8caee Mon Sep 17 00:00:00 2001 From: Milind Chatrabhuji Date: Mon, 26 Oct 2026 16:39:48 +0530 Subject: [PATCH] fix(fields/array): add edit button to rows and make edit dialog dismissable (#1644) --- .changeset/array-field-edit-and-dismiss.md | 5 ++ .../src/form/fields/array/ui.test.tsx | 59 +++++++++++++++++++ .../keystatic/src/form/fields/array/ui.tsx | 21 +++++-- 3 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 .changeset/array-field-edit-and-dismiss.md create mode 100644 packages/keystatic/src/form/fields/array/ui.test.tsx diff --git a/.changeset/array-field-edit-and-dismiss.md b/.changeset/array-field-edit-and-dismiss.md new file mode 100644 index 000000000..ddd76f14c --- /dev/null +++ b/.changeset/array-field-edit-and-dismiss.md @@ -0,0 +1,5 @@ +--- +'@keystatic/core': patch +--- + +Add visible edit action button to array field rows and enable dismissable close button on edit item dialog (#1644) diff --git a/packages/keystatic/src/form/fields/array/ui.test.tsx b/packages/keystatic/src/form/fields/array/ui.test.tsx new file mode 100644 index 000000000..265ef21eb --- /dev/null +++ b/packages/keystatic/src/form/fields/array/ui.test.tsx @@ -0,0 +1,59 @@ +/** @vitest-environment jsdom */ +import { KeystarProvider } from '@keystar/ui/core'; +import '@testing-library/jest-dom/vitest'; +import { render, screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { useState } from 'react'; +import ResizeObserver from 'resize-observer-polyfill'; +import { describe, expect, test } from 'vitest'; + +import { fields } from '../../api'; +import { usePreviewProps } from '../../../app/preview-props'; +import { ArrayFieldInput } from './ui'; + +globalThis.ResizeObserver ??= ResizeObserver; + +const schema = fields.array(fields.text({ label: 'Name' }), { + label: 'Tags', + itemLabel: item => item.value || 'Empty', +}); + +function TestArrayField() { + const [state, setState] = useState(['First', 'Second']); + const previewProps = usePreviewProps(schema, setState, state); + return ( + + + + ); +} + +describe('ArrayFieldInput', () => { + test('opens edit dialog from row edit button and allows dismissing it', async () => { + const user = userEvent.setup(); + render(); + + const editButtons = screen.getAllByRole('button', { name: 'Edit' }); + expect(editButtons).toHaveLength(2); + + await user.click(editButtons[1]); + + const dialog = await screen.findByRole('dialog'); + expect(dialog).toBeInTheDocument(); + expect( + screen.getByRole('heading', { name: 'Edit item' }) + ).toBeInTheDocument(); + expect(screen.getByDisplayValue('Second')).toBeInTheDocument(); + + const dismissButton = screen.getByRole('button', { name: 'Dismiss' }); + await user.click(dismissButton); + + await waitFor(() => { + expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); + }); + }); +}); diff --git a/packages/keystatic/src/form/fields/array/ui.tsx b/packages/keystatic/src/form/fields/array/ui.tsx index a9209d1bd..75de28e52 100644 --- a/packages/keystatic/src/form/fields/array/ui.tsx +++ b/packages/keystatic/src/form/fields/array/ui.tsx @@ -7,6 +7,7 @@ import { } from '@keystar/ui/drag-and-drop'; import { FieldDescription, FieldLabel, FieldMessage } from '@keystar/ui/field'; import { Icon } from '@keystar/ui/icon'; +import { pencilIcon } from '@keystar/ui/icon/icons/pencilIcon'; import { trash2Icon } from '@keystar/ui/icon/icons/trash2Icon'; import { VStack } from '@keystar/ui/layout'; import { Item, ListView } from '@keystar/ui/list-view'; @@ -94,6 +95,7 @@ export function ArrayFieldInput( )} { setModalState({ state: 'closed' }); }} @@ -113,7 +115,7 @@ export function ArrayFieldInput( } if (modalState.state !== 'edit') return; return ( - + Edit item ( }} > {item => { - const label = - props.schema.itemLabel?.(item) || - `Item ${props.elements.indexOf(item) + 1}`; + const index = props.elements.indexOf(item); + const label = props.schema.itemLabel?.(item) || `Item ${index + 1}`; return ( {label} { + props.onOpenItem(index); + }} + > + + + {stringFormatter.format('edit')} + + + { onRemoveKey(item.key); }}