Skip to content
Merged
15 changes: 15 additions & 0 deletions .changeset/35507-grid-entry-row-header-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@object-ui/fields': patch
'@object-ui/plugin-form': patch
---

fix(fields): the inline line grid draws no blank entry row beside "Add line", and each column header shows its field's help

Clause-②: no
Metadata-protocol: none

The inline line grid of a master-detail form always drew an empty line under the lines, directly above the "Add line" button. It had no copy or remove action and was never saved, so users read it as a broken line they could not delete. The grid no longer draws it: a grid with lines shows exactly its lines, and an empty grid says "No items yet — click "Add line" to begin." The keyboard path that line carried stays: "Add line" moves focus into the line it adds, and Enter in the last line adds a line and moves to the same column in it. An empty required grid that the form refused still draws one entry line, so the field's failure stays on the cells a user types into.

Each column header now shows its field's help as an info icon beside the label. The help appears on hover and on keyboard focus, and it is the icon's accessible name. The text is what the record form shows under the same field: `inlineHelpText`, else `description`, from the child object's definition as the server serves it, so help written by another package's `objectExtensions.fieldPatches` and the reader's language both apply. `GridField` takes the texts as a new `columnHelp` prop, and `columnHelpByField` builds them from a child object's fields. The master-detail form, the line-items panel and the record-body table editor pass them.

What does not change: what a save sends, the `allowAdd` / `allowDelete` / `maxRows` behaviour, and every metadata key, value and default. A master-detail entry whose relationship and every column type are authored loads no child schema, so its headers show no help, as it has no lookup autofill.
Original file line number Diff line number Diff line change
Expand Up @@ -3004,6 +3004,8 @@ describe('RecordDetailView.recordEditState-35599', () => {
const lines = editor(container, 'showcase_line.quote')!;
fireEvent.change(cells(lines, 'Quantity')[0], { target: { value: '5' } });
fireEvent.click(lines.querySelectorAll('button[aria-label="Remove row"]')[1] as HTMLElement);
// The new line comes from Add: the grid draws no blank entry line beside it (objectstack#35507).
fireEvent.click(lines.querySelector('[data-testid="line-items-add"]') as HTMLElement);
const products = cells(lines, 'Product');
fireEvent.change(products[products.length - 1], { target: { value: 'Crate' } });
const tasks = editor(container, 'showcase_task.quote')!;
Expand Down
4 changes: 2 additions & 2 deletions ui/packages/fields/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5417,8 +5417,8 @@ export * from './widgets/AutoNumberField.js';
export * from './widgets/UserField.js';
export * from './widgets/ObjectField.js';
export * from './widgets/VectorField.js';
export { evalArith, lookupAutofillPatch, computeRow, GridField, LineItemsField, UNTOUCHED_NEW_ROW, isUntouchedNewRow } from './widgets/GridField.js';
export type { ColumnCurrencies } from './widgets/GridField.js';
export { evalArith, lookupAutofillPatch, computeRow, GridField, LineItemsField, UNTOUCHED_NEW_ROW, isUntouchedNewRow, columnHelpByField } from './widgets/GridField.js';
export type { ColumnCurrencies, ColumnHelp } from './widgets/GridField.js';
export type { GridColumn } from './widgets/GridField.js';
export * from './widgets/lookupAutofill.js';
// objectstack#32770 (ADR-0150 D8 R1) — the grant's set-name column and the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,17 @@ describe('GridField: a cell edit renumbers no row (objectui#12286)', () => {
expect(next).toEqual([rows[0], { ...rows[1], description: 'B2' }]);
});

it('CONTROL: typing into the trailing entry row ADDS a line, and an add renumbers every row', () => {
const next = typeInto([{ description: 'A', position: 0 }, { description: 'B', position: 0 }], 2, 'C');
it('CONTROL: Add appends a line, and an add renumbers every row', () => {
// The trailing entry line this used to type into is gone (objectstack#35507);
// Add is the add, and it renumbers as the entry line did.
const onChange = vi.fn();
render(<GridField value={[{ description: 'A', position: 0 }, { description: 'B', position: 0 }]} onChange={onChange} field={field} />);
fireEvent.click(screen.getByTestId('line-items-add'));
const next: Row[] = onChange.mock.calls[0][0];
expect(next.map((r) => [r.description, r.position])).toEqual([
['A', 0],
['B', 1],
['C', 2],
[null, 2],
]);
});
});
160 changes: 136 additions & 24 deletions ui/packages/fields/src/widgets/GridField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import React from 'react';
import { UploadProvider } from './UploadProvider';
import { formatDate, formatDateTime } from '@object-ui/core';
import { GridField, LineItemsField, sumColumn, lookupAutofillPatch, UNTOUCHED_NEW_ROW, isUntouchedNewRow } from './GridField';
import { GridField, LineItemsField, sumColumn, lookupAutofillPatch, UNTOUCHED_NEW_ROW, isUntouchedNewRow, columnHelpByField } from './GridField';

const columns = [
{ name: 'description', label: 'Description', type: 'text' as const },
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('GridField / LineItemsField — editable line items', () => {
it('editing a text cell emits the raw string', () => {
const onChange = vi.fn();
render(<GridField value={[{ description: '', amount: null }]} onChange={onChange} field={field} />);
// [0] = the data row ([1] would be the always-present trailing ghost row).
// [0] = the data row.
fireEvent.change(screen.getAllByLabelText('Description')[0], { target: { value: 'Taxi' } });
expect(onChange).toHaveBeenCalledWith([{ description: 'Taxi', amount: null }]);
});
Expand Down Expand Up @@ -369,24 +369,23 @@ describe('GridField / LineItemsField — editable line items', () => {
});
});

describe('trailing ghost row (start-with-one + auto-append)', () => {
it('renders a trailing empty row so an empty grid still has one input line', () => {
render(<GridField value={[]} onChange={() => {}} field={field} />);
// No "No items" empty-state in grid mode — the ghost row IS the first line.
expect(screen.getByText('Description')).toBeTruthy();
expect(screen.getAllByLabelText('Description')).toHaveLength(1); // just the ghost
// objectstack#35507 — the always-present trailing entry line is gone (see
// the top-level `no trailing entry row beside Add` pins). It is drawn only
// for an empty grid the form refused (objectui#3318), and there typing
// into it still materialises the line.
describe('the entry line of an empty grid the form refused', () => {
it('is drawn only once the form reports the failure', () => {
const { rerender } = render(<GridField value={[]} onChange={() => {}} field={field} />);
expect(screen.queryAllByLabelText('Description')).toHaveLength(0);
rerender(<GridField value={[]} onChange={() => {}} field={field} error="Lines is required" />);
expect(screen.getAllByLabelText('Description')).toHaveLength(1);
});

it('typing in the ghost row materialises a new row (no Add click needed)', () => {
it('typing into it materialises a new line', () => {
const onChange = vi.fn();
render(<GridField value={[{ description: 'A', amount: 1 }]} onChange={onChange} field={field} />);
const inputs = screen.getAllByLabelText('Description');
expect(inputs).toHaveLength(2); // data row + ghost
fireEvent.change(inputs[1], { target: { value: 'B' } }); // type in the ghost
expect(onChange).toHaveBeenCalledWith([
{ description: 'A', amount: 1 },
{ description: 'B', amount: null },
]);
render(<GridField value={[]} onChange={onChange} field={field} error="Lines is required" />);
fireEvent.change(screen.getByLabelText('Description'), { target: { value: 'B' } });
expect(onChange).toHaveBeenCalledWith([{ description: 'B', amount: null }]);
});
});

Expand Down Expand Up @@ -508,12 +507,12 @@ describe('GridField / LineItemsField — editable line items', () => {
]);
});

it('flags a required, empty cell on a real row (not the ghost row)', () => {
it('flags a required, empty cell on a real row', () => {
const reqField = { columns: [{ name: 'description', label: 'Description', type: 'text' as const, required: true }] } as any;
render(<GridField value={[{ description: '' }]} onChange={() => {}} field={reqField} />);
// The data row's required-empty cell is flagged...
expect(screen.getByTestId('line-items-invalid-0-description')).toBeTruthy();
// ...but the trailing ghost row (index 1) is not.
// ...and no line below it is drawn to flag (objectstack#35507).
expect(screen.queryByTestId('line-items-invalid-1-description')).toBeNull();
});
});
Expand All @@ -528,7 +527,7 @@ describe('GridField / LineItemsField — editable line items', () => {

it('renders a real upload control in the cell, not a text input', () => {
render(<GridField value={[{ description: 'Taxi', receipt: null }]} onChange={() => {}} field={fileField} />);
// One upload button per row (data row + ghost), backed by a native file input.
// One upload button per row, backed by a native file input.
expect(screen.getAllByRole('button', { name: 'Receipt' }).length).toBeGreaterThan(0);
expect(document.querySelector('input[type="file"]')).toBeTruthy();
expect(screen.queryByRole('textbox', { name: 'Receipt' })).toBeNull();
Expand Down Expand Up @@ -591,14 +590,14 @@ describe('GridField / LineItemsField — editable line items', () => {
expect(cell.getAttribute('aria-invalid')).toBeNull();
});

it('does not announce invalid on an optional empty file cell or on the ghost row', () => {
it('does not announce invalid on an optional empty file cell', () => {
const optFileField = {
columns: [{ name: 'receipt', label: 'Receipt', type: 'file' as const }],
} as any;
const { container } = render(
<GridField value={[{ receipt: null }]} onChange={() => {}} field={optFileField} />,
);
// Data row and ghost row each render a picker; none may announce invalid.
// The data row renders a picker; it may not announce invalid.
expect(container.querySelector('[aria-invalid="true"]')).toBeNull();
});

Expand All @@ -621,7 +620,7 @@ describe('GridField / LineItemsField — editable line items', () => {
const withAccept = {
columns: [{ name: 'receipt', label: 'Receipt', type: 'file' as const, accept: ['image/*', '.pdf'] }],
} as any;
render(<GridField value={[]} onChange={() => {}} field={withAccept} />);
render(<GridField value={[{ receipt: null }]} onChange={() => {}} field={withAccept} />);
const input = document.querySelector('input[type="file"]') as HTMLInputElement;
expect(input.getAttribute('accept')).toBe('image/*,.pdf');
});
Expand Down Expand Up @@ -791,8 +790,10 @@ describe('GridField — a new line starts on its defaults and waits to read red

it('Add and the entry line both start a line on the host\'s values', () => {
const onChange = vi.fn();
// The entry line is drawn for an empty grid the form refused
// (objectstack#35507), so the failure is handed in.
const { container } = render(
<GridField value={[]} onChange={onChange} field={taskColumns} newRowValues={{ status: 'pending' }} />,
<GridField value={[]} onChange={onChange} field={taskColumns} newRowValues={{ status: 'pending' }} error="Tasks is required" />,
);
fireEvent.click(screen.getByRole('button', { name: /Add line/i }));
expect(onChange).toHaveBeenLastCalledWith([{ name: null, status: 'pending', [UNTOUCHED_NEW_ROW]: true }]);
Expand Down Expand Up @@ -845,3 +846,114 @@ describe('GridField — a new line starts on its defaults and waits to read red
expect(screen.getByTestId('line-items-invalid-0-name')).toBeTruthy();
});
});

/**
* objectstack#35507 — no blank entry row beside "Add line". The grid used to
* append an always-present empty "ghost" line under the data rows; it carried
* no actions, produced no write, and sat directly above the Add button that
* does the same job, so users read it as a broken line they could not delete.
* It was drawn only when Add was drawn (both follow `allowAdd`), so dropping it
* leaves no grid without a way to add a line. The keyboard path it carried
* (Tab or Enter into an empty line and type) is kept by Add moving focus into
* the new line and by Enter on the last line appending one.
*/
describe('no trailing entry row beside Add (objectstack#35507)', () => {
function Stateful({ initial, f = field }: { initial: Record<string, unknown>[]; f?: any }) {
const [rows, setRows] = React.useState(initial);
return <GridField value={rows} onChange={setRows} field={f} />;
}

it('a grid with lines draws exactly its lines, and the Add button', () => {
render(<GridField value={[{ description: 'A', amount: 1 }]} onChange={() => {}} field={field} />);
expect(screen.getAllByLabelText('Description')).toHaveLength(1);
expect(screen.getByTestId('line-items-add')).toBeTruthy();
});

it('an empty grid draws no entry row, and names the Add button in its empty state', () => {
render(<GridField value={[]} onChange={() => {}} field={field} />);
expect(screen.queryAllByLabelText('Description')).toHaveLength(0);
expect(screen.getByText('No items yet — click “Add line” to begin.')).toBeTruthy();
});

it('CONTROL: with `allowAdd: false` there is neither an Add button nor an entry row, as before', () => {
render(<GridField value={[{ description: 'A', amount: 1 }]} onChange={() => {}} field={{ ...field, allowAdd: false }} />);
expect(screen.queryByTestId('line-items-add')).toBeNull();
expect(screen.getAllByLabelText('Description')).toHaveLength(1);
});

it('CONTROL: an edit emits exactly the lines, with no blank line appended, as before', () => {
const onChange = vi.fn();
render(<GridField value={[{ description: 'A', amount: 1 }]} onChange={onChange} field={field} />);
// `[0]`: the data line's cell, whatever else the grid draws below it.
fireEvent.change(screen.getAllByLabelText('Description')[0], { target: { value: 'B' } });
expect(onChange).toHaveBeenLastCalledWith([{ description: 'B', amount: 1 }]);
});

it('Add moves focus into the new line, so the keyboard keeps typing', async () => {
render(<Stateful initial={[{ description: 'A', amount: 1 }]} />);
fireEvent.click(screen.getByTestId('line-items-add'));
await waitFor(() => expect(screen.getAllByLabelText('Description')).toHaveLength(2));
await waitFor(() => expect(document.activeElement).toBe(screen.getAllByLabelText('Description')[1]));
});

it('Enter in the last line appends a line and moves to the same column in it', async () => {
render(<Stateful initial={[{ description: 'A', amount: 1 }]} />);
fireEvent.keyDown(screen.getByLabelText('Amount'), { key: 'Enter' });
await waitFor(() => expect(screen.getAllByLabelText('Amount')).toHaveLength(2));
await waitFor(() => expect(document.activeElement).toBe(screen.getAllByLabelText('Amount')[1]));
});

it('CONTROL: Enter in the last line appends nothing when adding is off', () => {
const onChange = vi.fn();
render(<GridField value={[{ description: 'A', amount: 1 }]} onChange={onChange} field={{ ...field, allowAdd: false }} />);
fireEvent.keyDown(screen.getByLabelText('Amount'), { key: 'Enter' });
expect(onChange).not.toHaveBeenCalled();
});
});

/**
* objectstack#35804 — each column header shows its field's help. A grid column
* carries no field definition, so the host that holds the child object's
* served definition (the one the record form reads, with other packages'
* `fieldPatches` applied and the help in the reader's language) hands the
* texts over through `columnHelpByField`, which reads each field through the
* form's own `fieldHelpText`.
*/
describe('column header help (objectstack#35804)', () => {
const help = { amount: 'Enter the tax-inclusive amount.' };

it('a column with help draws an info button whose accessible name is the help', () => {
render(<GridField value={[]} onChange={() => {}} field={field} columnHelp={help} />);
const button = screen.getByRole('button', { name: 'Enter the tax-inclusive amount.' });
expect(button.closest('th')!.textContent).toContain('Amount');
});

it('focusing the info button shows the help', async () => {
render(<GridField value={[]} onChange={() => {}} field={field} columnHelp={help} />);
fireEvent.focus(screen.getByRole('button', { name: 'Enter the tax-inclusive amount.' }));
expect((await screen.findByRole('tooltip')).textContent).toBe('Enter the tax-inclusive amount.');
});

it('the read-only table draws the same help', () => {
render(<GridField value={[{ description: 'A', amount: 1 }]} onChange={() => {}} field={field} readonly columnHelp={help} />);
expect(screen.getByRole('button', { name: 'Enter the tax-inclusive amount.' })).toBeTruthy();
});

it('CONTROL: a column without help draws no info button', () => {
render(<GridField value={[]} onChange={() => {}} field={field} columnHelp={help} />);
const descriptionHeader = screen.getByText('Description').closest('th')!;
expect(descriptionHeader.querySelector('button')).toBeNull();
});

it('columnHelpByField reads each field the way the record form does', () => {
expect(
columnHelpByField({
unit_price: { inlineHelpText: 'Inline help wins.', description: 'Not this.' },
quantity: { description: 'The served description.' },
notes: { inlineHelpText: '' },
bad: null,
}),
).toEqual({ unit_price: 'Inline help wins.', quantity: 'The served description.' });
expect(columnHelpByField(undefined)).toEqual({});
});
});
Loading
Loading