Skip to content

Commit d19ebd0

Browse files
committed
feat(ui): the form's section bar stops at a group-placed master-detail table
A slot row carries its field group's label, once per group, and the form renderer reports a labelled slot to the section navigation between the headings around it. Adds an edit-mode pin for the placed collection. Claude-Session: https://claude.ai/code/session_01L6bhgXUMw7b8gUmNsyoXHR Co-authored-by: Claude <noreply@anthropic.com>
1 parent e7816f5 commit d19ebd0

7 files changed

Lines changed: 94 additions & 13 deletions

File tree

‎.changeset/35889-form-places-detail-at-group.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Metadata-protocol: none
1212
A master-detail child that a field group of the parent places (`fieldGroups[].relatedList`) is now drawn at that group in the parent's create and edit forms, where the record page draws it, instead of below all of the record's fields:
1313

1414
- A form laid out from the parent's field groups draws the table after the sections of the groups declared before the placing group and before the ones declared after it.
15+
- The form's section bar stops at the table too, between the sections around it.
1516
- A tabbed form gives it a tab of its own at that position, labelled with the group's label. A wizard master-detail form is drawn as the simple form, as before, and places it the same way.
1617
- A form whose sections name no field group, and a child no group places (a form view's own `subforms`), keep the table below the record's fields, as before.
1718

‎ui/packages/components/src/renderers/form/__tests__/form-section-nav-34605.test.tsx‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,27 @@ function renderWithSink(schema: Record<string, unknown>) {
6666
}
6767

6868
describe('form section list for the host navigation (objectstack#34605)', () => {
69+
it('stops at a labelled host slot between the headings around it, and skips an unlabelled one (objectstack#35889)', async () => {
70+
const { last } = renderWithSink({
71+
fields: [
72+
divider('s_header', 'Header', ['name']),
73+
{ name: 'name', label: 'Name', type: 'input' },
74+
{ name: '__form_slot__d0', label: 'Lines', type: 'form-slot' },
75+
{ name: '__form_slot__d1', label: '', type: 'form-slot' },
76+
divider('s_totals', 'Totals', ['memo']),
77+
{ name: 'memo', label: 'Memo', type: 'input' },
78+
],
79+
});
80+
await waitFor(() => expect(last()).toBeTruthy());
81+
const items = last()!;
82+
expect(items.map((i) => [i.key, i.label])).toEqual([
83+
['s_header', 'Header'],
84+
['__form_slot__d0', 'Lines'],
85+
['s_totals', 'Totals'],
86+
]);
87+
expect(document.getElementById(items[1].anchorId)?.getAttribute('data-form-slot')).toBe('__form_slot__d0');
88+
});
89+
6990
it('reports the drawn headings in order, each with the DOM id of its row', async () => {
7091
const { last } = renderWithSink({ fields: SECTIONED });
7192
await waitFor(() => expect(last()).toBeTruthy());

‎ui/packages/components/src/renderers/form/form.tsx‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3714,10 +3714,19 @@ ComponentRegistry.register('form',
37143714
// A host slot (objectstack#35889): an empty full-width element the form's
37153715
// host fills through a portal (`@object-ui/plugin-form` `formSlots.ts`,
37163716
// a master-detail form's collection at its field group). It holds no
3717-
// value and registers no control; `slotRef` is the host's callback.
3717+
// value and registers no control; `slotRef` is the host's callback. A
3718+
// labelled slot is a stop of the section navigation, like a heading.
37183719
if (type === 'form-slot') {
37193720
const { slotRef } = fieldProps as { slotRef?: React.Ref<HTMLDivElement> };
3720-
return <div key={name} ref={slotRef} className="col-span-full min-w-0 space-y-4" data-form-slot={name} />;
3721+
return (
3722+
<div
3723+
key={name}
3724+
ref={slotRef}
3725+
id={label ? sectionAnchorId(formInstanceId, name) : undefined}
3726+
className="col-span-full min-w-0 scroll-mt-2 space-y-4"
3727+
data-form-slot={name}
3728+
/>
3729+
);
37213730
}
37223731

37233732
// A field claimed by a HIDDEN section is not drawn (#6236) — the same
@@ -4485,7 +4494,7 @@ ComponentRegistry.register('form',
44854494
// the section navigation lists exactly these, so a section a predicate
44864495
// hides is absent from it by the same verdict that hid its row.
44874496
block.forEach((f, j) => {
4488-
if (f?.type === 'section-divider' && nodes[j] != null && f.label && typeof f.name === 'string') {
4497+
if ((f?.type === 'section-divider' || f?.type === 'form-slot') && nodes[j] != null && f.label && typeof f.name === 'string') {
44894498
drawnHeadings?.push({ key: f.name, label: String(f.label) });
44904499
}
44914500
});

‎ui/packages/plugin-form/src/MasterDetailForm.groupPlacement-35889.test.tsx‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function makeDataSource(parent: unknown, overrides: Record<string, unknown> = {}
3939
}
4040

4141
const details = [
42-
{ childObject: 'po_line', relationshipField: 'po', title: 'Order lines', columns: [{ key: 'qty', label: 'Qty', type: 'number' } as any] },
42+
{ childObject: 'po_line', relationshipField: 'po', title: 'Order lines', columns: [{ name: 'qty', label: 'Qty', type: 'number' } as any] },
4343
];
4444

4545
/** Whether `a` comes before `b` in document order. */
@@ -102,6 +102,32 @@ describe('MasterDetailForm — a group-placed collection sits at its group (obje
102102
expect(before(container.querySelector('[data-form-section="__section_totals"]')!, placed)).toBe(true);
103103
});
104104

105+
it('edit form: the loaded record\'s lines are drawn at the group, and only there', async () => {
106+
const parent = parentWith([HEADER_GROUP, LINES_GROUP, TOTALS_GROUP]);
107+
const child = { name: 'po_line', fields: { qty: { type: 'number', label: 'Qty' }, po: { type: 'master_detail', label: 'PO', reference: 'po' } } };
108+
const ds = makeDataSource(parent, {
109+
getObjectSchema: vi.fn(async (object: string) => (object === 'po_line' ? child : parent)),
110+
findOne: vi.fn().mockResolvedValue({ id: 'po1', ref: 'PO-7', memo: 'm' }),
111+
find: vi.fn().mockImplementation((object: string) =>
112+
Promise.resolve({ data: object === 'po_line' ? [{ id: 'l1', po: 'po1', qty: 3 }] : [] }),
113+
),
114+
});
115+
const { container } = render(
116+
<MasterDetailForm schema={{ objectName: 'po', mode: 'edit', recordId: 'po1', details } as any} dataSource={ds} />,
117+
);
118+
await waitFor(() => {
119+
const el = container.querySelector('[data-form-slot] [data-testid="md-collection"]');
120+
const values = el ? [...el.querySelectorAll('input')].map((input) => (input as HTMLInputElement).value) : [];
121+
if (!values.includes('3')) throw new Error('lines not placed');
122+
});
123+
const header = container.querySelector('[data-form-section="__section_header"]')!;
124+
const totals = container.querySelector('[data-form-section="__section_totals"]')!;
125+
const placed = container.querySelector('[data-form-slot] [data-testid="md-collection"]')!;
126+
expect(before(header, placed)).toBe(true);
127+
expect(before(placed, totals)).toBe(true);
128+
expect(container.querySelectorAll('[data-testid="md-collection"]')).toHaveLength(1);
129+
});
130+
105131
it('saves the record and its lines in one batch from the placed table', async () => {
106132
const batchTransaction = vi.fn().mockResolvedValue({ results: [{ id: 'po1' }] });
107133
const ds = makeDataSource(parentWith([HEADER_GROUP, LINES_GROUP, TOTALS_GROUP]), { batchTransaction });

‎ui/packages/plugin-form/src/ObjectForm.tsx‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import {
4949
sectionedGridLayout,
5050
} from './autoLayout';
5151
import { deriveFieldGroupSections, projectSectionDivider, resolveSectionCollapse, type FormFieldWithGroup } from './fieldGroups';
52-
import { formSlotRow, placeFormSlots, type FormSlotHandoff } from './formSlots';
52+
import { formSlotGroup, formSlotRow, placeFormSlots, type FormSlotHandoff } from './formSlots';
5353
import { FormSectionNav, useFormSectionNav } from './FormSectionNav';
5454
import { mergeCustomFields } from './customFieldsMerge';
5555
import { hasSectionGroupReference, resolveSectionGroupReferences } from './sectionGroups';
@@ -1691,9 +1691,18 @@ const SimpleObjectForm: React.FC<{ schema: LocalizedObjectFormSchema; dataSource
16911691
customFields: schema.customFields,
16921692
pool: sourceFields,
16931693
};
1694+
// A placed collection's row is named by its group, once per group, so the
1695+
// section navigation stops at it between the headings around it.
1696+
const namedSlotGroups = new Set<string>();
16941697
slotPlacement.sequence.forEach((item) => {
16951698
if ('slot' in item) {
1696-
if (slotHost) groupedFields.push(formSlotRow(item.slot, slotHost));
1699+
if (!slotHost) return;
1700+
const group = formSlotGroup(objectSchema?.fieldGroups, item.slot);
1701+
const label = group && !namedSlotGroups.has(group.key)
1702+
? sectionLabel(schema.objectName, group.key, group.label || group.key)
1703+
: '';
1704+
if (group) namedSlotGroups.add(group.key);
1705+
groupedFields.push(formSlotRow(item.slot, slotHost, label));
16971706
return;
16981707
}
16991708
const { section } = item;

‎ui/packages/plugin-form/src/TabbedForm.tsx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import { useRecordInvalidation } from './recordInvalidation';
4242
import { useReportedDirtyChange } from './formDirtyReport';
4343
import { useUploadGate, UploadGateProvider, UploadInFlightNotice } from './uploadGate';
4444
import { useFormChromeTranslation } from './formChrome';
45-
import { formSlotGroupIndex, formSlotRow, placeFormSlots, type FormSlotHandoff } from './formSlots';
45+
import { formSlotGroup, formSlotGroupIndex, formSlotRow, placeFormSlots, type FormSlotHandoff } from './formSlots';
4646

4747
export interface FormSectionConfig {
4848
/**
@@ -682,9 +682,9 @@ export const TabbedForm: React.FC<TabbedFormProps> = ({
682682
slotTab.group.fields.push(row);
683683
return;
684684
}
685-
const group = (objectSchema?.fieldGroups as Array<{ key?: string; label?: string }> | undefined)?.[at];
686-
const key = typeof group?.key === 'string' ? group.key : `__form_slot_tab__${item.slot.id}`;
687-
const label = typeof group?.key === 'string'
685+
const group = formSlotGroup(objectSchema?.fieldGroups, item.slot);
686+
const key = group ? group.key : `__form_slot_tab__${item.slot.id}`;
687+
const label = group
688688
? sectionLabel(schema.objectName, group.key, group.label || group.key)
689689
: item.slot.childObject;
690690
const tab: TabGroup = { key, label, fields: [row] };

‎ui/packages/plugin-form/src/formSlots.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ export function formSlotGroupIndex(fieldGroups: unknown, request: FormSlotReques
7070
return index === -1 ? null : index;
7171
}
7272

73+
/**
74+
* The declared group that places this collection (its key and authored
75+
* label), the name a form gives the collection's place; `null` when none does.
76+
*/
77+
export function formSlotGroup(fieldGroups: unknown, request: FormSlotRequest): { key: string; label?: string } | null {
78+
const index = formSlotGroupIndex(fieldGroups, request);
79+
const group = index === null ? undefined : (fieldGroups as Array<{ key?: unknown; label?: unknown }>)[index];
80+
if (typeof group?.key !== 'string') return null;
81+
return { key: group.key, label: typeof group.label === 'string' ? group.label : undefined };
82+
}
83+
7384
export type FormSlotSequenceItem<S> = { section: S } | { slot: FormSlotRequest };
7485

7586
/**
@@ -128,11 +139,15 @@ export function placeFormSlots<S>(
128139
*/
129140
export type FormSlotRow = FormField & { slotRef: (el: HTMLElement | null) => void };
130141

131-
/** The row a placed collection takes in the header form's field list. */
132-
export function formSlotRow(request: FormSlotRequest, host: FormSlotHost): FormSlotRow {
142+
/**
143+
* The row a placed collection takes in the header form's field list. A
144+
* `label` (its group's) makes the row a stop of the form's section
145+
* navigation, between the headings it sits between.
146+
*/
147+
export function formSlotRow(request: FormSlotRequest, host: FormSlotHost, label = ''): FormSlotRow {
133148
return {
134149
name: `__form_slot__${request.id}`,
135-
label: '',
150+
label,
136151
type: FORM_SLOT_ROW_TYPE,
137152
slotRef: host.refFor(request.id),
138153
} as FormSlotRow;

0 commit comments

Comments
 (0)