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
15 changes: 15 additions & 0 deletions .changeset/35688-cell-editor-layer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@object-ui/components': patch
'@object-ui/plugin-grid': patch
---

feat(grid): the inline cell editor floats over its cell, so the row keeps its height; a selected cell that is cut off shows its whole value

Clause-②: no
Metadata-protocol: none

In `data-table` (and so in every grid `ObjectGrid` draws):

- **The editor draws on a layer over the cell** instead of inside it. The layer's top-left corner sits on the cell's, it is at least as wide as the column and grows wider or taller where the editor needs it (an address form, a record picker, a multi-line text), and neither the row's height nor any column's width changes. Near the bottom of the window it opens upward, near the right edge it shifts left, and its height is capped at the space left, scrolling inside. A cell partly hidden behind a frozen column, a right-pinned column or the sticky header is scrolled clear first; scrolling the cell out of sight commits the edit, as a click outside does. The layer stays inside the cell's DOM, so click-outside, Escape, Enter / Tab and focus behave as before. Tab inside the layer goes where it went before: the select-first grid commits and moves, other grids let the browser move focus.
- **`ObjectGrid` sizes the layer per field type**: an address or location starts 22rem wide, a multi-line text, markdown, code or JSON 20rem, a lookup, master-detail or user 18rem, a multi-select, tags or checkboxes 16rem, a date, date-time, phone, URL, email or currency 14rem, anything else 10rem; never narrower than its cell.
- **On the select-first grid** (the list page), a selected cell whose value the column cuts off shows it in full on a layer over the cell, wrapped and scrolling past a few lines. Escape, an arrow key or a click elsewhere closes it; Enter or a double-click turns it into the editor in the same place and at least the same size. Enter on a checkbox cell flips it, as Space and a second click already did, instead of opening an editor.
14 changes: 13 additions & 1 deletion content/docs/frontend/plugins/plugin-grid.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,13 @@ through the host's data source (`dataSource.update`) with no callback to wire.
editor. There is no `editable` key on `ListColumn`.
- **Visual feedback**: editable cells show a hover state, and the input is
focused and selected when editing begins
- **The editor floats over its cell** (objectstack#35688): it opens on a layer
anchored to the cell's top-left corner, at least as wide as the column and
wider where its field type needs it (an address form, a record picker), so
the row keeps its height and no other column moves. Near the bottom of the
window the layer opens upward, near the right edge it shifts left, and a
cell half hidden behind a frozen column or the header is scrolled into view
first. Scrolling the cell out of sight commits the edit.

To own persistence in a React host, supply `onCellChange` as a **component
prop** — it is not a schema key.
Expand All @@ -383,6 +390,11 @@ object and the view does not declare `userActions.editInline: false`, and
- **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.
- **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
arrow key or a click elsewhere closes it; Enter or a double-click turns it
into the editor in the same place and at the same size.
- **Editing** starts on a double-click, Enter or F2 (the value kept, caret at
its end), or by typing, which replaces the value; an input method's
composition is kept whole. Backspace or Delete opens an empty editor and
Expand All @@ -393,7 +405,7 @@ object and the view does not declare `userActions.editInline: false`, and
elsewhere commits.
- **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
does a second click on it); Ctrl/Cmd+C copies; Ctrl/Cmd+V converts the text
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.
- **A cell that opens no editor says why** on the cell: the field is read-only,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/**
* 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.
*/

/**
* `data-table`'s cell layer (objectstack#35688): the editor, and the full
* content of a cut-off selected cell, draw on a layer over the cell instead of
* inside it, so the row keeps its height. Layout itself (row height 32, the
* flip at the viewport's edge, painting above frozen columns) is a browser
* reading on the PR; jsdom has no layout, so these pin the structure and the
* gestures.
*
* 1. The editor sits in a fixed-position layer inside its cell, the cell is
* raised above the sticky cells, and nothing is drawn in the cell's flow.
* An injected (host) editor goes on the same layer.
* 2. A selected cell whose content is cut off shows it in full on a layer;
* one that fits shows none.
* 3. Enter on that preview opens the editor in its place, at least the
* preview's size; Esc there does not bring the preview back.
* 4. Esc on the preview closes it and keeps the cell selected; an arrow key
* moves the selection and the preview with it.
* 5. Enter on a two-state cell flips it and opens no layer.
* 6. Scrolling the open editor's cell out of the table's box commits it.
*/
import React from 'react';
import { describe, it, expect, afterEach, beforeEach, vi } from 'vitest';
import { render, fireEvent, cleanup, act } from '@testing-library/react';
import '@testing-library/jest-dom';
import { ComponentRegistry } from '@object-ui/core';
// Registers `data-table` at module scope, not in a hook (objectui#3010).
import '../renderers';
// `react-page` reaches a dynamic `import('@object-ui/react-runtime')`: pay its load
// at collection, never inside a case's clocked window (`check:test-source-alias`).
import '@object-ui/react-runtime';
import { GridCellSelectionProvider } from '../context/gridCellSelection';

const LONG = 'A long note that runs well past the width of its column';

const ROWS = [
{ id: '1', name: 'Alpha', note: LONG, done: false },
{ id: '2', name: 'Beta', note: 'Short', done: true },
];

// jsdom draws nothing: a text longer than ten characters reads as cut off.
const proto = HTMLElement.prototype;
const saved = {
scrollWidth: Object.getOwnPropertyDescriptor(proto, 'scrollWidth'),
clientWidth: Object.getOwnPropertyDescriptor(proto, 'clientWidth'),
};
beforeEach(() => {
Object.defineProperty(proto, 'scrollWidth', { configurable: true, get() { return (this.textContent ?? '').length * 10; } });
Object.defineProperty(proto, 'clientWidth', { configurable: true, get() { return 100; } });
});
afterEach(() => {
cleanup();
for (const [k, d] of Object.entries(saved)) {
if (d) Object.defineProperty(proto, k, d);
else delete (proto as unknown as Record<string, unknown>)[k];
}
});

function renderTable(extra: Record<string, unknown> = {}) {
const DataTable = ComponentRegistry.get('data-table')!;
const onCellChange = vi.fn();
const schema = {
type: 'data-table',
columns: [
{ header: 'Name', accessorKey: 'name' },
{ header: 'Note', accessorKey: 'note' },
{ header: 'Done', accessorKey: 'done', type: 'boolean' },
],
data: ROWS,
editable: true,
singleClickEdit: true,
searchable: false,
pagination: false,
onCellChange,
...extra,
};
const utils = render(
<GridCellSelectionProvider value={{}}>
<DataTable schema={schema} />
</GridCellSelectionProvider>,
);
const cell = (row: number, col: number) =>
utils.container.querySelectorAll('tbody tr')[row].querySelectorAll('td[data-grid-cell]')[col] as HTMLElement;
const catcher = () => utils.container.querySelector<HTMLInputElement>('[data-slot="cell-type-catcher"]')!;
const editorLayer = () => utils.container.querySelector<HTMLElement>('[data-slot="cell-editor-overlay"]');
const previewLayer = () => utils.container.querySelector<HTMLElement>('[data-slot="cell-preview-overlay"]');
return { ...utils, cell, catcher, editorLayer, previewLayer, onCellChange };
}

describe('data-table — the editor and a cut-off cell draw on a layer over the cell (objectstack#35688)', () => {
it('1. the editor sits in a fixed-position layer inside its cell, and the cell is raised', () => {
const t = renderTable();
fireEvent.click(t.cell(0, 0));
fireEvent.keyDown(t.catcher(), { key: 'Enter' });
const layer = t.editorLayer();
expect(layer).not.toBeNull();
// Inside the cell's DOM, so the grid's own `contains` reads hold.
expect(t.cell(0, 0).contains(layer)).toBe(true);
expect(layer!.querySelector('input')).toHaveValue('Alpha');
// Out of the cell's flow: Radix places the layer's wrapper `fixed`.
expect(layer!.closest<HTMLElement>('[data-radix-popper-content-wrapper]')!.style.position).toBe('fixed');
expect(t.cell(0, 0).className).toMatch(/\bz-30\b/);
expect(t.cell(1, 0).className).not.toMatch(/\bz-30\b/);
});

it('1b. a host-injected editor goes on the same layer', () => {
const t = renderTable({
renderCellEditor: ({ value, stage }: { value: unknown; stage: (v: unknown) => void }) => (
<input data-testid="host-editor" defaultValue={String(value)} onChange={(e) => stage(e.target.value)} />
),
});
fireEvent.click(t.cell(0, 0));
fireEvent.keyDown(t.catcher(), { key: 'Enter' });
expect(t.editorLayer()!.querySelector('[data-testid="host-editor"]')).not.toBeNull();
});

it('2. a selected cell that is cut off shows its whole content on a layer; one that fits shows none', () => {
const t = renderTable();
fireEvent.click(t.cell(0, 1));
expect(t.previewLayer()).toHaveTextContent(LONG);
expect(t.cell(0, 1).contains(t.previewLayer())).toBe(true);
// Its height cap keeps the space-available cap (one class, not two that merge away).
expect(t.previewLayer()!.className).toContain('max-h-[min(16rem,var(--radix-popper-available-height))]');
expect(t.previewLayer()!.className).not.toMatch(/(^|\s)max-h-64(\s|$)/);
fireEvent.click(t.cell(0, 0));
expect(t.previewLayer()).toBeNull();
});

it('3. Enter on the preview opens the editor in its place, at least its size; Esc does not bring it back', () => {
const t = renderTable();
fireEvent.click(t.cell(0, 1));
const preview = t.previewLayer()!;
vi.spyOn(preview, 'getBoundingClientRect').mockReturnValue({ width: 300, height: 72, top: 0, left: 0, right: 300, bottom: 72, x: 0, y: 0, toJSON: () => ({}) } as DOMRect);
fireEvent.keyDown(t.catcher(), { key: 'Enter' });
expect(t.previewLayer()).toBeNull();
const layer = t.editorLayer()!;
expect(layer.querySelector('input')).toHaveValue(LONG);
expect(layer.style.getPropertyValue('--cell-overlay-min-w')).toBe('300px');
expect(layer.style.getPropertyValue('--cell-overlay-min-h')).toBe('72px');
// The utility that reads the seed survives the per-type floor, which rides an inner box.
expect(layer.className).toContain('min-w-[max(var(--radix-popper-anchor-width),var(--cell-overlay-min-w,0px))]');
expect(layer.className).not.toMatch(/(^|\s)min-w-40(\s|$)/);
expect(layer.querySelector('.min-w-40')).not.toBeNull();
fireEvent.keyDown(layer.querySelector('input')!, { key: 'Escape' });
expect(t.editorLayer()).toBeNull();
expect(t.previewLayer()).toBeNull();
expect(t.cell(0, 1)).toHaveAttribute('data-selected-cell');
});

it('4. Esc closes the preview and keeps the selection; an arrow key moves on', () => {
const t = renderTable();
fireEvent.click(t.cell(0, 1));
fireEvent.keyDown(t.catcher(), { key: 'Escape' });
expect(t.previewLayer()).toBeNull();
expect(t.cell(0, 1)).toHaveAttribute('data-selected-cell');
fireEvent.click(t.cell(1, 1));
expect(t.previewLayer()).toBeNull();
fireEvent.keyDown(t.catcher(), { key: 'ArrowUp' });
expect(t.cell(0, 1)).toHaveAttribute('data-selected-cell');
expect(t.previewLayer()).toHaveTextContent(LONG);
});

it('5. Enter on a two-state cell flips it and opens no layer', () => {
const t = renderTable();
fireEvent.click(t.cell(0, 2));
fireEvent.keyDown(t.catcher(), { key: 'Enter' });
expect(t.editorLayer()).toBeNull();
expect(t.onCellChange).toHaveBeenCalledWith(0, 'done', true, expect.anything());
});

it('6. scrolling the open editor\'s cell out of the table\'s box commits it', () => {
const t = renderTable();
fireEvent.click(t.cell(0, 0));
fireEvent.keyDown(t.catcher(), { key: 'Enter' });
fireEvent.change(t.editorLayer()!.querySelector('input')!, { target: { value: 'Alpha 2' } });
// The table's scroll box, and a cell scrolled past its left edge.
const box = t.cell(0, 0).closest('table')!.parentElement!;
box.style.overflowX = 'auto';
vi.spyOn(box, 'getBoundingClientRect').mockReturnValue({ left: 0, top: 0, right: 100, bottom: 100, width: 100, height: 100, x: 0, y: 0, toJSON: () => ({}) } as DOMRect);
Object.defineProperty(box, 'clientHeight', { configurable: true, value: 100 });
vi.spyOn(t.cell(0, 0), 'getBoundingClientRect').mockReturnValue({ left: -200, top: 40, right: -50, bottom: 72, width: 150, height: 32, x: -200, y: 40, toJSON: () => ({}) } as DOMRect);
act(() => { box.dispatchEvent(new Event('scroll')); });
expect(t.editorLayer()).toBeNull();
expect(t.onCellChange).toHaveBeenCalledWith(0, 'name', 'Alpha 2', expect.anything());
});
});
Loading
Loading