Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .changeset/36015-list-cell-click-only-selects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@object-ui/components': patch
'@object-ui/fields': patch
'@object-ui/plugin-grid': patch
---

fix(grid): in a list cell a single click only selects, and the email and phone cells draw no copy button

Clause-②: no
Metadata-protocol: none

On a grid with the select-first cell model (the console's list page), a plain click on a link a cell draws used to follow it: an email opened the mail program, a phone number the dialler, a URL a new tab, a lookup or user chip that record's page, and an image thumbnail its lightbox. The cell was not selected, so a reader who meant to select or edit the cell left the list instead. The email and phone cells also drew a copy button beside the value, easy to hit while selecting.

A plain click on any of these now selects the cell and opens nothing. To open one: Ctrl/Cmd-click it (as before), press the open button the selected cell shows on hover (only on the selected cell, never on the row hover), or press Ctrl/Cmd+Enter on the selected cell. A cell of several chips opens the first one that way. A double-click on the link edits the cell, as on any other cell. The name link is the one exception and opens the record on a single click, as before. The copy buttons are not drawn inside such a grid; Ctrl/Cmd+C copies the selected cell.

Outside the select-first model nothing changes: the record page, related lists, phone cards, kanban cards and every grid whose host does not turn on `cellSelection` keep their links and copy buttons.

For renderer authors: a link (`a[href]`) inside a select-first cell is now treated as part of the cell's value. Mark the one link a single click must still follow with `data-opens-on-click`, a non-link control that opens what the value points at with `data-cell-opener`, and a copy control the grid should not draw with `data-cell-copy`.
15 changes: 11 additions & 4 deletions content/docs/frontend/plugins/plugin-grid.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,14 @@ object and the view does not declare `userActions.editInline: false`, and
`singleClickEdit` is not read there.

- **A click selects** a cell (a ring around it); it neither edits nor opens the
record. A link a cell draws (the name, a lookup, a phone or an email) keeps
its own click. On a touch screen a tap opens the record.
record. Only the name link keeps its own click (it opens the record). Every
other link a cell draws (an email, a phone number, a URL, a file, a lookup or
user chip, an image thumbnail) is selected by a click too (objectstack#36015);
to open it, Ctrl/Cmd-click it, press the **Open** button the selected cell
shows on hover, or press Ctrl/Cmd+Enter. A cell of several chips opens the
first one that way. The email and phone cells draw no copy button here:
Ctrl/Cmd+C copies the selected cell. On a touch screen a tap opens the
record.
- **A cut-off value shows in full** when its cell is selected: a long text, a
multi-line text or a list of tags that the column truncates opens on a
layer over the cell, wrapped, scrolling inside past a few lines. Escape, an
Expand All @@ -425,8 +431,9 @@ object and the view does not declare `userActions.editInline: false`, and
- **Selected cell keys:** the arrows and Tab move the selection; Space is typed
like any other key and opens the editor (a checkbox cell flips instead, as
do Enter and a second click on it); Ctrl/Cmd+C copies; Ctrl/Cmd+V converts the text
to the field's type and commits it; Escape clears the selection. The record
opens from the name link, or a tap on a touch screen.
to the field's type and commits it; Ctrl/Cmd+Enter opens the link the cell
draws; Escape clears the selection. The record opens from the name link, or
a tap on a touch screen.
- **A cell that opens no editor says why** on the cell: the field is read-only,
calculated or not editable in a list, the reader may not edit the field, the
object or the record, the view turned editing off, or the record is locked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
* 3. Space is typed like any key and opens the editor, never the record
* (the maintainer's correction on #35683); a two-state cell flips on
* Space or on a second click instead, and commits.
* 4. A link the cell draws keeps its own click; a double-click on the cell
* around it edits.
* 4. A plain click on a link the cell draws selects the cell and follows
* nothing; a Ctrl/Cmd-click follows it, and a double-click on it edits
* (objectstack#36015). The title link (`data-opens-on-click`) keeps its
* click. The selected cell's open button and Ctrl/Cmd+Enter open the
* link; a copy button a value draws is not drawn.
* 5. A cell that opens no editor says why on the cell: the host's sentence,
* else the table's own.
* 6. A touch tap opens the record and edits nothing.
Expand Down Expand Up @@ -48,11 +51,23 @@ const ROWS = [
function renderTable({
selection = {},
extra = {},
}: { selection?: GridCellSelection | null; extra?: Record<string, unknown> } = {}) {
withTitle = false,
}: { selection?: GridCellSelection | null; extra?: Record<string, unknown>; withTitle?: boolean } = {}) {
const DataTable = ComponentRegistry.get('data-table')!;
const onRowClick = vi.fn();
const onCellChange = vi.fn();
const onSiteClick = vi.fn();
const onTitleClick = vi.fn();
// The title link a host marks as the one a click follows (objectstack#36015).
const titleColumn = {
header: 'Title',
accessorKey: 'title',
cell: (_value: unknown, row: { id: string }) => (
<a href={`/record/${row.id}`} data-opens-on-click="" onClick={(e) => { e.preventDefault(); onTitleClick(row.id); }}>
Record {row.id}
</a>
),
};
const schema = {
type: 'data-table',
columns: [
Expand All @@ -63,9 +78,13 @@ function renderTable({
header: 'Site',
accessorKey: 'site',
cell: (value: string) => (
<a href={`https://${value}`} onClick={(e) => { e.preventDefault(); onSiteClick(value); }}>{value}</a>
<span>
<a href={`https://${value}`} onClick={(e) => { e.preventDefault(); onSiteClick(value); }}>{value}</a>
<button type="button" data-cell-copy="">Copy site</button>
</span>
),
},
...(withTitle ? [titleColumn] : []),
],
data: ROWS,
editable: true,
Expand All @@ -85,7 +104,7 @@ function renderTable({
utils.container.querySelectorAll('tbody tr')[row].querySelectorAll('td[data-grid-cell]')[col] as HTMLElement;
const editor = () => utils.container.querySelector<HTMLInputElement>('td[data-editing-cell] input');
const catcher = () => utils.container.querySelector<HTMLInputElement>('[data-slot="cell-type-catcher"]');
return { ...utils, cell, editor, catcher, onRowClick, onCellChange, onSiteClick };
return { ...utils, cell, editor, catcher, onRowClick, onCellChange, onSiteClick, onTitleClick };
}

describe('data-table select-first — a click selects (objectstack#35684)', () => {
Expand Down Expand Up @@ -186,20 +205,78 @@ describe('data-table select-first — Space types; a two-state cell flips', () =
});
});

describe('data-table select-first — a link in a cell keeps its click', () => {
it('a click on the link follows it and selects nothing; a double-click beside it edits', () => {
describe('data-table select-first — a click on a link the cell draws only selects (objectstack#36015)', () => {
it('a plain click selects the cell and follows nothing; a Ctrl or Cmd click follows the link', () => {
const t = renderTable();
const link = screen.getByText('a.example');
const plain = fireEvent.click(link);
expect(t.onSiteClick).not.toHaveBeenCalled();
// The browser's own navigation was cancelled too.
expect(plain).toBe(false);
expect(t.cell(0, 3)).toHaveAttribute('data-selected-cell');
expect(t.onRowClick).not.toHaveBeenCalled();
expect(document.activeElement).toBe(t.catcher());
fireEvent.click(link, { ctrlKey: true });
expect(t.onSiteClick).toHaveBeenCalledTimes(1);
fireEvent.click(link, { metaKey: true });
expect(t.onSiteClick).toHaveBeenCalledTimes(2);
});

it('a double-click on the link edits, as on any cell', () => {
const t = renderTable();
fireEvent.click(screen.getByText('a.example'));
expect(t.onSiteClick).toHaveBeenCalledWith('a.example');
expect(t.cell(0, 3)).not.toHaveAttribute('data-selected-cell');
fireEvent.doubleClick(screen.getByText('a.example'));
expect(t.editor()).toBeNull();
// The cell around the link.
expect(t.editor()).toHaveValue('a.example');
expect(t.onSiteClick).not.toHaveBeenCalled();
});

it('control: the title link (data-opens-on-click) keeps its click and selects nothing', () => {
const t = renderTable({ withTitle: true });
fireEvent.click(screen.getByText('Record 1'));
expect(t.onTitleClick).toHaveBeenCalledWith('1');
expect(t.cell(0, 4)).not.toHaveAttribute('data-selected-cell');
});

it('the selected cell draws an open button that follows the link; an unselected cell draws none', () => {
const t = renderTable();
expect(t.cell(0, 3).querySelector('[data-testid="cell-open-button"]')).toBeNull();
fireEvent.click(t.cell(0, 3));
const open = t.cell(0, 3).querySelector<HTMLButtonElement>('[data-testid="cell-open-button"]')!;
expect(open).toHaveAccessibleName('Open');
expect(open.className).toContain('flex');
fireEvent.click(open);
expect(t.onSiteClick).toHaveBeenCalledWith('a.example');
// The cell stays selected.
expect(t.cell(0, 3)).toHaveAttribute('data-selected-cell');
fireEvent.doubleClick(t.cell(0, 3));
});

it('a selected cell with nothing to open hides its open button', () => {
const t = renderTable();
fireEvent.click(t.cell(0, 0));
const open = t.cell(0, 0).querySelector<HTMLButtonElement>('[data-testid="cell-open-button"]')!;
expect(open.className).toContain('hidden');
expect(open.className).not.toMatch(/(^|\s)flex(\s|$)/);
});

it('Ctrl or Cmd+Enter on the selected cell follows the link; plain Enter edits', () => {
const t = renderTable();
fireEvent.click(t.cell(0, 3));
fireEvent.keyDown(t.catcher()!, { key: 'Enter', ctrlKey: true });
expect(t.onSiteClick).toHaveBeenCalledTimes(1);
fireEvent.keyDown(t.catcher()!, { key: 'Enter', metaKey: true });
expect(t.onSiteClick).toHaveBeenCalledTimes(2);
expect(t.editor()).toBeNull();
fireEvent.keyDown(t.catcher()!, { key: 'Enter' });
expect(t.editor()).toHaveValue('a.example');
});

it('a copy button a value draws is hidden in the select-first table, and only there', () => {
const t = renderTable();
expect(t.cell(0, 3).className).toContain('[&_[data-cell-copy]]:hidden');
cleanup();
const control = renderTable({ selection: null, extra: { keyboardNavigation: true } });
expect(control.cell(0, 3).className).not.toContain('[data-cell-copy]');
});
});

describe('data-table select-first — a cell that opens no editor says why', () => {
Expand Down
88 changes: 87 additions & 1 deletion ui/packages/components/src/renderers/complex/data-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { DisabledReasonMenuLabel, menuItemReasonAria, useDisabledReason } from '
import { useGridFieldAuthoring } from '../../context/gridFieldAuthoring';
import { useGridCellSelection } from '../../context/gridCellSelection';
import { useGridHeaderMenu } from '../../context/gridHeaderMenu';
import { caretToEnd, defaultTyping, defaultValueFromText, firstEditorControl, isCellWidgetTarget, isCommandLetter, isImeKey, isPrintableKey } from './dataTableCellSelection';
import { caretToEnd, cellOpenerAt, defaultTyping, defaultValueFromText, firstCellOpener, firstEditorControl, isCellWidgetTarget, isCommandLetter, isImeKey, isPrintableKey } from './dataTableCellSelection';
import { useHorizontalOverflow, HOVER_SCROLLBAR_CLASSES } from '../../custom/use-horizontal-overflow';
import { ActiveListRow, listRecordIdOf } from '../../custom/record-list-face';
import { describeIgnoredBind, describeNonArrayData } from './dataTableBindDiagnostic';
Expand Down Expand Up @@ -69,6 +69,7 @@ import {
AlertCircle,
Check,
Loader2,
SquareArrowOutUpRight,
} from 'lucide-react';
import {
DropdownMenu,
Expand Down Expand Up @@ -1418,6 +1419,53 @@ function CellNotice({ text, onDismiss }: { text: string; onDismiss: () => void }
);
}

/**
* objectstack#36015 — the selected cell's "open" button: drawn only while the
* cell draws something that opens (a link, a referenced record's chip, an
* image), shown when the pointer is over the cell. A plain click on that
* thing only selected the cell; this opens it. It takes no focus, so typing
* carries on in the cell, and the keyboard's way is Ctrl/Cmd+Enter.
*/
function CellOpenButton({ label, onOpen }: { label: string; onOpen: (cell: HTMLElement) => void }) {
const ref = useRef<HTMLButtonElement>(null);
const [cell, setCell] = useState<HTMLElement | null>(null);
const [opens, setOpens] = useState(false);
useLayoutEffect(() => {
const host = ref.current?.closest<HTMLElement>('td') ?? null;
setCell(host);
if (!host) return undefined;
// A referenced record's name is read after the cell first draws, and its
// chip becomes a link only then.
const measure = () => setOpens(firstCellOpener(host) !== null);
measure();
if (typeof MutationObserver === 'undefined') return undefined;
const observer = new MutationObserver(measure);
observer.observe(host, { childList: true, subtree: true, attributes: true, attributeFilter: ['href'] });
return () => observer.disconnect();
}, []);
return (
<button
ref={ref}
type="button"
tabIndex={-1}
aria-label={label}
title={label}
data-testid="cell-open-button"
className={cn(
'absolute right-1 top-1/2 z-20 size-5 -translate-y-1/2 items-center justify-center rounded border bg-background text-muted-foreground opacity-0 shadow-sm transition-opacity hover:text-foreground group-hover/cell:opacity-100',
opens ? 'flex' : 'hidden',
)}
onMouseDown={(e) => e.preventDefault()}
onClick={(e) => {
e.stopPropagation();
if (cell) onOpen(cell);
}}
>
<SquareArrowOutUpRight className="size-3" aria-hidden="true" />
</button>
);
}

/**
* objectstack#35685 — why a list cell's save was refused, said on that cell:
* the server's reasons, one line each, and, when the refusal names a field the
Expand Down Expand Up @@ -3638,6 +3686,22 @@ const DataTableRenderer = ({ schema }: { schema: DataTableSchema }) => {

const factsOf = (row: any, col: TableColumn) => cellSelection?.describeCell?.(row, col.accessorKey) ?? null;

// objectstack#36015 — while set, a click on a cell's opener follows it: the
// open button or Ctrl/Cmd+Enter is clicking it on the reader's behalf.
const followingOpenerRef = useRef(false);
/** Opens what the cell draws (its first opener); `false` when it draws none. */
const openCellOpener = (cell: Element): boolean => {
const opener = firstCellOpener(cell);
if (!opener) return false;
followingOpenerRef.current = true;
try {
opener.click();
} finally {
followingOpenerRef.current = false;
}
return true;
};

/**
* objectstack#36023 — why the open editor's value may not be committed, or
* `null`: first what the browser itself cannot read in the editor's input (a
Expand Down Expand Up @@ -3817,6 +3881,11 @@ const DataTableRenderer = ({ schema }: { schema: DataTableSchema }) => {
return;
}
setCellNotice(null);
if (onCatcher && e.key === 'Enter' && (e.ctrlKey || e.metaKey) && !e.altKey && !e.shiftKey) {
// objectstack#36015 — Ctrl/Cmd+Enter opens what the selected cell draws.
if (openCellOpener(e.currentTarget)) e.preventDefault();
return;
}
const next = navigationTarget(e, rowIndex, colIndex);
if (next) {
e.preventDefault();
Expand Down Expand Up @@ -5044,6 +5113,9 @@ const DataTableRenderer = ({ schema }: { schema: DataTableSchema }) => {
// reader deselected with Esc keeps the focus with no ring.
selectFirst && "outline-none",
selectFirst && !isFrozen && "relative",
// objectstack#36015 — a single click selects, and Ctrl/Cmd+C copies
// the selected cell, so the copy buttons a value draws are not drawn.
selectFirst && "group/cell [&_[data-cell-copy]]:hidden",
!autosaves && hasPendingChange && "font-semibold text-amber-700 dark:text-amber-400",
cellError !== undefined && "ring-2 ring-inset ring-destructive",
isFrozen && ['sticky z-10 bg-background', STICKY_ROW_TINT],
Expand All @@ -5066,6 +5138,17 @@ const DataTableRenderer = ({ schema }: { schema: DataTableSchema }) => {
// where its column does.
...(pinnedRightOffset ? { right: pinnedRightOffset } : {}),
}}
onClickCapture={selectFirst ? (e) => {
// objectstack#36015 — a plain click on a link, a referenced
// record's chip or an image the cell draws selects the cell and
// opens nothing. A modifier click opens it as it always did.
if (isEditing || followingOpenerRef.current || isTouchPress(e)) return;
if (cellOpenerAt(e.target, e.currentTarget) === null) return;
if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.button !== 0) return;
e.preventDefault();
e.stopPropagation();
selectCell(rowIndex, colIndex);
} : undefined}
onMouseDown={selectFirst ? (e) => {
// The double-click is the edit gesture here, so its second
// press does not select the word under the pointer.
Expand Down Expand Up @@ -5195,6 +5278,9 @@ const DataTableRenderer = ({ schema }: { schema: DataTableSchema }) => {
}}
/>
)}
{isSelectedCell && !isEditing && (
<CellOpenButton label={t('table.open')} onOpen={openCellOpener} />
)}
{cellNotice?.rowIndex === rowIndex && cellNotice.colIndex === colIndex && (
<CellNotice text={cellNotice.text} onDismiss={() => setCellNotice(null)} />
)}
Expand Down
Loading
Loading