diff --git a/.changeset/29177-untyped-repeater-json-editor.md b/.changeset/29177-untyped-repeater-json-editor.md new file mode 100644 index 00000000000..8fc1af2fa3f --- /dev/null +++ b/.changeset/29177-untyped-repeater-json-editor.md @@ -0,0 +1,14 @@ +--- +'@object-ui/fields': patch +--- + +fix(fields): an untyped `repeater` field edits its stored rows as JSON in the record form + +Clause-②: no +Metadata-protocol: none + +A `repeater` field is an untyped array of embedded JSON objects: `@objectstack/spec` declares no sub-field or column member on it. The form's type table sent it to the line-items grid (`field:grid`), which draws only declared columns, so a stored `[{ label: 'one', qty: 1 }, …]` showed numbered rows with Duplicate and Remove buttons and no cell. The content could not be read or changed, and Add line appended an empty object. + +`repeater` now resolves to the JSON editor (`field:object`) that `composite`, `record` and `json` already use. The record form shows the stored array as pretty-printed JSON, an edit saves an array of objects (never a string), and unparsable text is refused before the save. Inline editing stays excluded, as before. A line-items grid that declares its columns is unchanged. + +The field is no longer laid out full-row in the record form and on the Studio form canvas: the JSON editor is a normal-width widget, like `composite` beside it. The read side (record detail, the list cell's row count) is unchanged. A list that needs typed columns is a child object whose `master_detail` field sets `inlineEdit: 'grid'`. diff --git a/ui/packages/app-shell/src/views/studio-design/ObjectFormDesigner.test.tsx b/ui/packages/app-shell/src/views/studio-design/ObjectFormDesigner.test.tsx index 44ef29017ba..243664fdc70 100644 --- a/ui/packages/app-shell/src/views/studio-design/ObjectFormDesigner.test.tsx +++ b/ui/packages/app-shell/src/views/studio-design/ObjectFormDesigner.test.tsx @@ -280,12 +280,15 @@ describe('ObjectFormDesigner — emits the one legal `FormField.type` spelling ( expect(toFormFieldType('field:markdown')).not.toBe('field:text'); }); - it('spans a `repeater` field full-row — the drift the raw pass hid', () => { - // `repeater` is a spec `FieldType` that resolves to the WIDE `field:grid` - // widget, but bare `repeater` is not one of `WIDE_FIELD_TYPES`' bare - // members. Passing the raw object type in therefore matched nothing: the - // canvas laid a repeater out at normal width while the runtime form — - // which reaches the set through `mapFieldTypeToFormType` — spanned it. + it('lays a `repeater` field out the way the runtime form resolves it', () => { + // The canvas asks the width question through `mapFieldTypeToFormType`, the + // runtime form's own resolution, so the two cannot drift. `repeater` is an + // untyped JSON array in the spec and resolves to the JSON editor + // (`field:object`) beside `composite` and `record`; that widget is not + // wide, so the canvas lays it out at normal width, as the form does. (It + // resolved to the WIDE line-items grid until that grid was found drawing + // bare rows for a column-less repeater.) + expect(toFormFieldType('repeater')).toBe('field:object'); render( { diff --git a/ui/packages/fields/src/field-type-alias.ts b/ui/packages/fields/src/field-type-alias.ts index a5b6a50c462..f615bb00347 100644 --- a/ui/packages/fields/src/field-type-alias.ts +++ b/ui/packages/fields/src/field-type-alias.ts @@ -170,19 +170,25 @@ export function mapFieldTypeToFormType( color: 'field:color', code: 'field:code', // A JSON value is edited as JSON — shown with `JSON.stringify`, saved - // with `JSON.parse` — by the editor `object`, `composite` and `record` - // below use (objectui#11448). It used to alias `field:code`, the raw-text + // with `JSON.parse` — by the editor `object`, `composite`, `record` and + // `repeater` below use (objectui#11448). It used to alias `field:code`, the raw-text // editor, which showed a stored object as `[object Object]` and saved an // edit as a string. `code` keeps the raw-text editor. json: 'field:object', qrcode: 'field:qrcode', vector: 'field:vector', - // Embedded structured values (stored as JSON on the row) + // Embedded structured values (stored as JSON on the row). The spec + // declares `composite`, `record` and `repeater` UNTYPED: no sub-field or + // column member exists for a widget to read, so all three take the JSON + // editor. `repeater` used to alias the line-items grid, which draws only + // declared columns and so showed a stored array as bare numbered rows, + // its content neither visible nor editable. A list that needs typed + // columns is a child object edited with `inlineEdit: 'grid'`. object: 'field:object', composite: 'field:object', // embedded object record: 'field:object', // name-keyed map - repeater: 'field:grid', // embedded array of rows + repeater: 'field:object', // embedded array of objects // Auto-generated/computed fields (typically read-only) formula: 'field:formula', diff --git a/ui/packages/plugin-detail/src/__tests__/autoLayout.wideSpelling.test.ts b/ui/packages/plugin-detail/src/__tests__/autoLayout.wideSpelling.test.ts index a14a8af6ed1..8826a21bfb3 100644 --- a/ui/packages/plugin-detail/src/__tests__/autoLayout.wideSpelling.test.ts +++ b/ui/packages/plugin-detail/src/__tests__/autoLayout.wideSpelling.test.ts @@ -100,9 +100,8 @@ describe('detail WIDE_FIELD_TYPES — keyed on the spec vocabulary (#4250)', () // `grid` is absent from this assertion by construction: it is not a spec // FieldType but a sanctioned objectui-local key (`GridFieldMetadata` in // `@object-ui/types`), so it is not part of the spec-facing surface. - // `repeater` — the spec type whose widget IS `field:grid` — is deliberately - // NOT wide here; that asymmetry is filed separately rather than fixed under - // this card. + // `repeater` is not wide either: it resolves to the JSON editor + // (`field:object`), not to the line-items grid, so no surface spans it. const wideSpecTypes = SPEC_TYPES.filter((t) => isWideFieldType(t)); expect([...wideSpecTypes].sort()).toEqual([...WIDE_SPEC_TYPES].sort()); }); diff --git a/ui/packages/plugin-form/src/ModalForm.untypedRepeater.test.tsx b/ui/packages/plugin-form/src/ModalForm.untypedRepeater.test.tsx new file mode 100644 index 00000000000..dd69e1b63ec --- /dev/null +++ b/ui/packages/plugin-form/src/ModalForm.untypedRepeater.test.tsx @@ -0,0 +1,175 @@ +/** + * ObjectUI + * Copyright (c) 2024-present ObjectStack Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/** + * An untyped `repeater` field shows and edits its stored rows in the record + * Edit dialog. + * + * `@objectstack/spec` declares `repeater` an untyped array of embedded JSON + * objects: `FieldSchema` has no sub-field or column member, so nothing a widget + * could read names the keys of a row. The alias table used to send it to the + * line-items grid (`field:grid`), which draws only declared columns, so a + * stored `[{label:'one',qty:1},…]` drew numbered rows with Duplicate and + * Remove and no cell: the content could be neither read nor changed. It now + * takes the JSON editor `composite` and `record` beside it use. + * + * Driven through the dialog the console mounts for Edit — the global + * `ModalForm`, whose body is a real `SchemaRenderer` form — with the real + * registered widgets and a click on the real submit button. + * + * The control: a line-items grid that DECLARES its columns still draws one + * input per declared column and writes typed cells. + */ +import { describe, it, expect, vi } from 'vitest'; +import { render, waitFor, fireEvent, screen, act } from '@testing-library/react'; +import React from 'react'; + +import { registerAllFields, GridField, resolveFormWidgetType } from '@object-ui/fields'; +import { ModalForm } from './ModalForm'; + +registerAllFields(); + +const ROWS = [ + { label: 'one', qty: 1 }, + { label: 'two', qty: 2 }, +]; + +const STORED = { + id: 'z1', + name: 'Specimen — Full', + f_repeater: ROWS, +}; + +function makeDS() { + return { + getObjectSchema: vi.fn().mockResolvedValue({ + name: 'field_zoo', + fields: { + name: { type: 'text', label: 'Name' }, + f_repeater: { type: 'repeater', label: 'Repeater (embedded array)' }, + }, + }), + findOne: vi.fn().mockResolvedValue(structuredClone(STORED)), + find: vi.fn().mockResolvedValue({ data: [] }), + create: vi.fn(async (_o: string, d: any) => ({ id: 'n1', ...d })), + update: vi.fn(async (_o: string, _id: string, d: any) => ({ ...STORED, ...d })), + }; +} + +function openEditDialog() { + const ds = makeDS(); + render( + , + ); + return ds; +} + +const repeaterBox = () => + waitFor(() => { + const el = document.querySelector('textarea[name="f_repeater"]') as HTMLTextAreaElement | null; + if (!el) throw new Error('f_repeater not ready'); + return el; + }); + +const type = async (el: HTMLElement, text: string) => { + await act(async () => { + fireEvent.change(el, { target: { value: text } }); + }); +}; + +const clickUpdate = async () => { + await act(async () => { + fireEvent.click(screen.getByRole('button', { name: 'Update' })); + }); + await act(async () => { + await new Promise((resolve) => setTimeout(resolve, 30)); + }); +}; + +const row = (name: string) => document.querySelector(`[data-field="${name}"]`) as HTMLElement | null; + +const payloads = (ds: ReturnType) => ds.update.mock.calls.map((call) => call[2]); + +describe('an untyped repeater field in the record Edit dialog', () => { + it('shows the stored rows: "one", "two", 1 and 2 are on screen', async () => { + openEditDialog(); + const box = await repeaterBox(); + await waitFor(() => expect(box.value).toBe(JSON.stringify(ROWS, null, 2))); + for (const shown of ['"one"', '"two"', '"qty": 1', '"qty": 2']) { + expect(box.value).toContain(shown); + } + // Not the bare line-items grid it used to draw. + expect(row('f_repeater')?.querySelector('[data-testid="line-items"]')).toBeNull(); + }); + + it('saves an edit as an array of objects, never a string', async () => { + const ds = openEditDialog(); + const box = await repeaterBox(); + await waitFor(() => expect(box.value).toContain('"one"')); + const next = [ + { label: 'one', qty: 5 }, + { label: 'three', qty: 3 }, + ]; + await type(box, JSON.stringify(next)); + await clickUpdate(); + await waitFor(() => expect(ds.update).toHaveBeenCalledTimes(1)); + const sent = payloads(ds)[0].f_repeater; + expect(Array.isArray(sent)).toBe(true); + expect(sent).toEqual(next); + expect(typeof sent[0].qty).toBe('number'); + }); + + it('an untouched save never writes the field as text', async () => { + const ds = openEditDialog(); + await repeaterBox(); + await clickUpdate(); + await waitFor(() => expect(ds.update).toHaveBeenCalledTimes(1)); + const sent = payloads(ds)[0]; + // Present or absent are both safe; a string in this slot is the defect. + if ('f_repeater' in sent) expect(sent.f_repeater).toEqual(ROWS); + expect(typeof sent.f_repeater === 'string').toBe(false); + }); + + it('CONTROL — a grid that declares columns still draws the line-items grid', async () => { + // `grid` is objectui's own line-items key (`GridFieldMetadata`), not a spec + // field type, so it is driven at the widget the form resolves for it. + expect(resolveFormWidgetType('grid')).toBe('grid'); + const onChange = vi.fn(); + render( + , + ); + const grid = screen.getByTestId('line-items'); + expect((grid.querySelector('input[aria-label="Item"]') as HTMLInputElement).value).toBe('bolt'); + const qty = grid.querySelector('input[aria-label="Qty"]') as HTMLInputElement; + expect(qty.value).toBe('3'); + await act(async () => { + fireEvent.change(qty, { target: { value: '4' } }); + }); + expect(onChange).toHaveBeenLastCalledWith([{ item: 'bolt', qty: 4 }]); + }); +});