Skip to content

Commit 25b2e82

Browse files
committed
fix(ui): a placed master-detail collection's controls stay out of the header form
The slot sits inside the header's <form>, so the portaled collection is wrapped in a form of its own (noValidate, a disabled default button, submit prevented): the header's requestSubmit validates only its own fields, a line button without a type no longer submits the header, and Enter in a line cell submits nothing. The renderer's reveal of an errored field skips what a host drew into its slot rows. Claude-Session: https://claude.ai/code/session_01L6bhgXUMw7b8gUmNsyoXHR Co-authored-by: Claude <noreply@anthropic.com>
1 parent 11ee7b5 commit 25b2e82

4 files changed

Lines changed: 73 additions & 3 deletions

File tree

‎ui/packages/components/src/renderers/form/__tests__/form-invalid-scroll.test.tsx‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ function renderForm(fields: any[], defaultValues?: Record<string, unknown>) {
6363
const submit = () => fireEvent.click(screen.getByRole('button', { name: /create/i }));
6464

6565
describe('form renderer — invalid-submit feedback (#2793)', () => {
66+
it('reveals its own field, not a same-named field a host drew into a form-slot row (objectstack#35889)', async () => {
67+
// What a host draws there (an expanded line editor) is a form of its own.
68+
const slotRef = (el: HTMLElement | null) => {
69+
if (el && !el.firstChild) el.innerHTML = '<div data-field="title"><input data-testid="line-title" /></div>';
70+
};
71+
const { container } = renderForm([
72+
{ name: '__form_slot__d0', label: '', type: 'form-slot', slotRef },
73+
{ name: 'title', label: 'Title', type: 'input', required: true },
74+
]);
75+
const own = [...container.querySelectorAll<HTMLElement>('[data-field="title"]')].find((el) => !el.closest('[data-form-slot]'))!;
76+
const ownScroll = vi.fn();
77+
own.scrollIntoView = ownScroll;
78+
const slotted = container.querySelector<HTMLElement>('[data-form-slot] [data-field="title"]')!;
79+
const slottedScroll = vi.fn();
80+
slotted.scrollIntoView = slottedScroll;
81+
82+
submit();
83+
84+
await waitFor(() => expect(ownScroll).toHaveBeenCalled());
85+
expect(slottedScroll).not.toHaveBeenCalled();
86+
expect(document.activeElement).not.toBe(screen.getByTestId('line-title'));
87+
});
88+
6689
it('scrolls+focuses the first errored field on a missing required field, with no toast', async () => {
6790
const { container } = renderForm([
6891
{ name: 'title', label: 'Title', type: 'input', required: true },

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3317,7 +3317,15 @@ ComponentRegistry.register('form',
33173317
typeof CSS !== 'undefined' && typeof CSS.escape === 'function'
33183318
? CSS.escape(name)
33193319
: name.replace(/["\\]/g, '\\$&');
3320-
return root.querySelector<HTMLElement>(`[data-field="${escaped}"]`);
3320+
// Not a field a host drew into one of THIS form's `form-slot` rows (an
3321+
// expanded line editor there is a form of its own, objectstack#35889);
3322+
// that editor, inside a slot, still finds its own fields.
3323+
return (
3324+
[...root.querySelectorAll<HTMLElement>(`[data-field="${escaped}"]`)].find((el) => {
3325+
const slot = el.closest('[data-form-slot]');
3326+
return !slot || !(root as Node).contains(slot);
3327+
}) ?? null
3328+
);
33213329
};
33223330
const revealField = (name: string) => {
33233331
const target = fieldWrapper(name);

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('MasterDetailForm — a group-placed collection sits at its group (obje
7070
const totals = container.querySelector('[data-form-section="__section_totals"]')!;
7171
expect(header).toBeTruthy();
7272
expect(totals).toBeTruthy();
73-
expect(placed.closest('form')).toBe(header.closest('form'));
73+
expect(placed.closest('[data-form-slot]')!.closest('form')).toBe(header.closest('form'));
7474
expect(before(header, placed)).toBe(true);
7575
expect(before(placed, totals)).toBe(true);
7676
expect(placed.textContent).toContain('Order lines');
@@ -128,6 +128,28 @@ describe('MasterDetailForm — a group-placed collection sits at its group (obje
128128
expect(container.querySelectorAll('[data-testid="md-collection"]')).toHaveLength(1);
129129
});
130130

131+
it('a placed collection\'s controls are not the header form\'s: no native validation, no submit from a line button', async () => {
132+
const ds = makeDataSource(parentWith([HEADER_GROUP, LINES_GROUP, TOTALS_GROUP]));
133+
const { container } = render(
134+
<MasterDetailForm schema={{ objectName: 'po', mode: 'create', details } as any} dataSource={ds} />,
135+
);
136+
await waitFor(() => {
137+
if (!container.querySelector('[data-form-slot] [data-testid="md-collection"] button')) throw new Error('not placed');
138+
});
139+
const header = container.querySelector('[data-form-section="__section_header"]')!.closest('form')!;
140+
const slot = container.querySelector('[data-form-slot]')!;
141+
const controls = [...slot.querySelectorAll('input, select, textarea, button')] as HTMLInputElement[];
142+
expect(controls.length).toBeGreaterThan(0);
143+
// `requestSubmit()` validates, and a submit button submits, only the
144+
// controls the form owns.
145+
for (const control of controls) expect(control.form).not.toBe(header);
146+
147+
const submitted = vi.fn();
148+
header.addEventListener('submit', submitted);
149+
for (const button of slot.querySelectorAll('button')) fireEvent.click(button);
150+
expect(submitted).not.toHaveBeenCalled();
151+
});
152+
131153
it('saves the record and its lines in one batch from the placed table', async () => {
132154
const batchTransaction = vi.fn().mockResolvedValue({ results: [{ id: 'po1' }] });
133155
const ds = makeDataSource(parentWith([HEADER_GROUP, LINES_GROUP, TOTALS_GROUP]), { batchTransaction });

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,17 @@ interface MasterDetailLinesProps {
873873
* element through a portal, in place when no drawn layout claims it, and
874874
* nowhere while a claimed slot's element is not mounted (objectstack#35889).
875875
* Its own component, so a slot landing re-renders only what it moves.
876+
*
877+
* The slot element sits inside the header's `<form>`, so a portaled collection
878+
* is wrapped in a form of its own: its controls then belong to that form, not
879+
* the header's, exactly as they belonged to none below the form. The header's
880+
* `requestSubmit()` validates only its own fields (a line cell's bad input
881+
* never blocks Save with a browser bubble), a line button without a `type`
882+
* submits only the wrapper, which does nothing, and Enter in a cell finds the
883+
* wrapper's disabled default button and submits nothing.
876884
*/
885+
const preventSubmit = (event: React.FormEvent) => event.preventDefault();
886+
877887
const FormSlotPlacement: React.FC<{ store: FormSlotStore; id: string; children: React.ReactNode }> = ({
878888
store,
879889
id,
@@ -882,7 +892,14 @@ const FormSlotPlacement: React.FC<{ store: FormSlotStore; id: string; children:
882892
useSyncExternalStore(store.subscribe, store.getVersion, store.getVersion);
883893
const at = store.placement(id);
884894
if (at === 'inline') return <>{children}</>;
885-
return at ? createPortal(children, at) : null;
895+
if (!at) return null;
896+
return createPortal(
897+
<form noValidate onSubmit={preventSubmit} data-form-slot-owner="">
898+
<button type="submit" disabled hidden aria-hidden="true" tabIndex={-1} />
899+
{children}
900+
</form>,
901+
at,
902+
);
886903
};
887904

888905
/**

0 commit comments

Comments
 (0)