Skip to content
17 changes: 17 additions & 0 deletions .changeset/35876-list-header-menu-multi-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@object-ui/components': patch
'@object-ui/plugin-grid': patch
'@object-ui/plugin-list': patch
'@object-ui/i18n': patch
---

fix(plugin-list): a list's column header opens a menu and keeps a multi-level sort; a sort chip opens the sort panel

Clause-②: yes (widening)
Metadata-protocol: none

On a list page (the grid `ListView` draws), a click on a column header no longer sorts at once. It opens the column menu: sort ascending, sort descending, hide the column (a column that does not sort offers hide alone). Enter, Space or ArrowDown on the focused header opens the same menu, the arrows move through it, Enter chooses, and Esc shuts it without touching the sort. With no sort or a single level, the column picked in the menu replaces the sort; with two levels or more it becomes the first level, the other levels keep their order after it, and a column already in the sort moves up with the picked direction. No level is dropped. The ↑1 / ↓2 marks on the headers are unchanged.

A sort chip under the view bar no longer reverses its direction on a click: it opens every sort level in one panel. Each level row in the sort builder now has a handle; dragging it, or ArrowUp / ArrowDown on the focused handle, reorders the levels.

New public surface, all additive: `GridHeaderMenuProvider` and `useGridHeaderMenu` from `@object-ui/components` (a host opts a data-table into the header menu; without it a header click sorts as before, so related lists, dashboard tables and every other host keep click-to-sort); the `headerMenu` runtime prop on `ObjectGrid`; and the `sortBuilder.reorder` key in every `@object-ui/i18n` locale. The column menu itself is now a menu (`role="menu"` entries, keyboard reachable) wherever it opens, including a right-click on a header and the design host's caret.
21 changes: 21 additions & 0 deletions content/docs/frontend/plugins/plugin-grid.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ is no top-level `sortable` switch.
}
```

A click on a sortable column header sorts by that column; another click on it
changes the direction.

**On an object's list page** the header opens a menu instead
(objectstack#35876), which its host turns on (the `headerMenu` component
prop, not a schema key): **Sort ascending**, **Sort descending** and **Hide
column**. A column that does not sort offers **Hide column** alone. The
header itself never changes the sort. Enter, Space or ArrowDown
on the focused header opens the same menu; the arrows move through it, Enter
chooses and Escape closes it without touching the sort.

- With no sort, or a single level, the column picked in the menu replaces the
sort.
- With two levels or more it becomes the first level and every other level
keeps its place after it; a column already in the sort moves up with the
picked direction. No level is dropped.
- The ↑1 / ↓2 marks on the headers show each level's place.
- A sort chip under the view bar opens every level in one panel (it does not
reverse the direction). Each level's handle reorders it: drag it to another
row, or press ArrowUp / ArrowDown on the focused handle.

### Filtering and search

There is no per-column filter key and no top-level `filterable` switch. A grid
Expand Down
25 changes: 20 additions & 5 deletions ui/e2e/list-condition-row-35549.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,16 @@ test.describe('list condition row under the view bar (objectstack#35549, objects
expect(await slotHeight(page)).toBe(0);
});

test('sort: Sort → Descending → Amount is three clicks; chips come before filter chips; a drag reorders; a header click shows as a chip; Reset restores', async ({ page }) => {
test('sort: Sort → Descending → Amount is three clicks; chips come before filter chips; a drag reorders; a handle in a chip panel reorders; a header menu pick shows as a chip; Reset restores', async ({ page }) => {
test.setTimeout(120_000);
await mount(page);
await expect.poll(() => count(page)).toBe('12');
await click(page, 'toolbar-sort-trigger');
await click(page, 'sort-panel-order-desc');
await click(page, 'sort-panel-fields-item-amount');
expect(await sortNow(page)).toEqual(['amount:desc']);
await expect(page.getByTestId('sort-chip-toggle-amount')).toHaveText('Amount');
await expect(page.getByTestId('sort-chip-toggle-amount')).toHaveAttribute('title', 'High → Low');
await expect(page.getByTestId('sort-chip-open-amount')).toHaveText('Amount');
await expect(page.getByTestId('sort-chip-open-amount')).toHaveAttribute('title', 'High → Low');
// a second level
await click(page, 'toolbar-sort-trigger');
await click(page, 'sort-panel-fields-item-stage');
Expand All @@ -354,15 +354,30 @@ test.describe('list condition row under the view bar (objectstack#35549, objects
// a real drag: the second sort chip onto the first
await page.getByTestId('sort-chip-stage').dragTo(page.getByTestId('sort-chip-amount'));
expect(await sortNow(page)).toEqual(['stage:asc', 'amount:desc']);
// a chip opens every level in one panel (objectstack#35876); a real drag of
// the second level's handle onto the first row puts it first
await click(page, 'sort-chip-open-stage');
const panel = page.getByTestId('sort-chip-panel');
await expect(panel.getByTestId('sort-builder-handle')).toHaveCount(2);
expect(await sortNow(page)).toEqual(['stage:asc', 'amount:desc']);
await panel.getByTestId('sort-builder-handle').nth(1).dragTo(panel.getByTestId('sort-builder-row').first());
await expect.poll(() => sortNow(page)).toEqual(['amount:desc', 'stage:asc']);
await page.keyboard.press('Escape');
// Reset puts the list back as saved: no sort, no filter
await click(page, 'condition-reset');
expect(await sortNow(page)).toEqual([]);
await expect.poll(() => count(page)).toBe('12');
expect(await slotHeight(page)).toBe(0);
// a click on a column header sorts, and the row shows it as a chip
// a click on a column header opens its menu and sorts nothing
// (objectstack#35876); the entry chosen there sorts, and the row shows it
// as a chip
await page.getByRole('columnheader', { name: /Amount/ }).click();
const columnMenu = page.getByTestId('column-context-menu');
await expect(columnMenu).toBeVisible();
expect(await sortNow(page)).toEqual([]);
await columnMenu.getByRole('menuitem', { name: 'Sort descending' }).click();
await style(page);
await expect(page.getByTestId('sort-chip-amount')).toBeVisible();
expect((await sortNow(page))[0]).toMatch(/^amount:/);
expect(await sortNow(page)).toEqual(['amount:desc']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('Data pillar Records tab (objectstack#34794)', () => {
const caret = within(header('title')).getByRole('button', { name: COLUMN_MENU });
await userEvent.click(caret);
const menu = screen.getByTestId('column-context-menu');
expect(within(menu).getAllByRole('button')).toHaveLength(3);
expect(within(menu).getAllByRole('menuitem')).toHaveLength(3);
// The caret's click selected nothing else and did not sort either.
expect(firstTitles()).toEqual(before);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,6 @@ const NOT_CONTROLS: Array<[file: string, key: string, reason: string]> = [
['packages/app-shell/src/views/studio-design/StudioDesignSurface.tsx', 'div · setRailOpen', SCRIM],
['packages/app-shell/src/views/studio-design/StudioRightSlot.tsx', 'div · onOverlayDismiss', SCRIM],
['packages/components/src/renderers/complex/data-table.tsx', 'div · stopPropagation', DRAG],
// The column header's context menu: its entries are buttons.
['packages/components/src/renderers/complex/data-table.tsx', 'div · stopPropagation', GUARD],
['packages/plugin-dashboard/src/DashboardRenderer.tsx', 'div · handleBackgroundClick', CANVAS],
['packages/plugin-dashboard/src/DashboardRenderer.tsx', 'div · handleBackgroundClick', CANVAS],
['packages/plugin-designer/src/ProcessDesigner.tsx', 'div · handleCanvasClick', CANVAS],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ describe('SortBuilder below sm (objectstack#29057)', () => {
);
}

it('lays a level out on three lines: the words, the field beside Remove, the direction', () => {
it('lays a level out on three lines: the words, the field beside Remove, the direction; the drag handle beside all three (objectstack#35876)', () => {
mount();
const row = screen.getByTestId('sort-builder-row');
expect(tokens(row)).toEqual(expect.arrayContaining(['flex', 'max-sm:grid', 'max-sm:grid-cols-[minmax(0,1fr)_auto]']));
expect(tokens(row)).toEqual(expect.arrayContaining(['flex', 'max-sm:grid', 'max-sm:grid-cols-[auto_minmax(0,1fr)_auto]']));
expect(tokens(screen.getByTestId('sort-builder-handle'))).toEqual(expect.arrayContaining(['max-sm:col-start-1', 'max-sm:row-span-3']));
const field = screen.getByTestId('sort-builder-field').parentElement!;
expect(tokens(field)).toEqual(expect.arrayContaining(['flex-1', 'max-sm:row-start-2']));
const direction = screen.getByTestId('sort-builder-direction').parentElement!;
expect(tokens(direction)).toEqual(expect.arrayContaining(['w-36', 'max-sm:col-span-2', 'max-sm:row-start-3', 'max-sm:w-auto']));
expect(tokens(direction)).toEqual(expect.arrayContaining(['w-36', 'max-sm:col-start-2', 'max-sm:col-span-2', 'max-sm:row-start-3', 'max-sm:w-auto']));
});

it('every control is 44 px below sm and keeps its desktop size', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ describe('SortBuilder honours `disabled` on a fields entry (objectui#11943)', ()
expect(onChange).toHaveBeenLastCalledWith([asc('title'), asc('status')]);
expect(fieldTriggers()[0]).toHaveTextContent('Title');

// Removed: the row's only button is its remove control.
// Removed: the row's remove control.
cleanup();
onChange = mount(FLAGGED, [asc('secret'), asc('status')]);
const row = screen.getByText('Sort by').parentElement as HTMLElement;
fireEvent.click(within(row).getByRole('button'));
fireEvent.click(within(row).getByRole('button', { name: /remove sort/i }));
expect(onChange).toHaveBeenLastCalledWith([asc('status')]);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* 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.
*/

/**
* objectstack#35876 — the order of the sort levels is the sort's priority,
* and each level's handle changes it: a drag onto another row, or ArrowUp /
* ArrowDown on the focused handle. A single level has nothing to reorder.
*/
import { describe, it, expect, vi, afterEach } from 'vitest';
import React from 'react';
import { cleanup, render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import { SortBuilder, type SortItem } from '../custom/sort-builder';

const FIELDS = [
{ value: 'name', label: 'Name' },
{ value: 'stage', label: 'Stage' },
{ value: 'amount', label: 'Amount' },
];
const THREE: SortItem[] = [
{ id: 'a', field: 'name', order: 'asc' },
{ id: 'b', field: 'stage', order: 'desc' },
{ id: 'c', field: 'amount', order: 'asc' },
];

afterEach(cleanup);

/** The builder under a host that keeps what it hands back, as a list does. */
function mount(initial: SortItem[]) {
const onChange = vi.fn();
function Host() {
const [value, setValue] = React.useState(initial);
return (
<SortBuilder
fields={FIELDS}
value={value}
onChange={(next) => {
onChange(next);
setValue(next);
}}
/>
);
}
render(<Host />);
return onChange;
}
const fieldsOf = (items: SortItem[]) => items.map((i) => `${i.field}:${i.order}`);

describe('SortBuilder — reordering the levels (objectstack#35876)', () => {
it('every level has a named handle: a keyboard control that is not a <button>, so Firefox can drag it', () => {
mount(THREE);
const handles = screen.getAllByRole('button', { name: 'Drag to reorder' });
expect(handles).toHaveLength(3);
for (const h of handles) {
expect(h).toHaveAttribute('draggable', 'true');
expect(h).toHaveAttribute('tabindex', '0');
expect(h.tagName).not.toBe('BUTTON');
}
});

it('ArrowUp on the second handle makes it the first level; ArrowDown on the first moves it down', () => {
const onChange = mount(THREE);
fireEvent.keyDown(screen.getAllByTestId('sort-builder-handle')[1], { key: 'ArrowUp' });
expect(fieldsOf(onChange.mock.calls.at(-1)![0])).toEqual(['stage:desc', 'name:asc', 'amount:asc']);
fireEvent.keyDown(screen.getAllByTestId('sort-builder-handle')[0], { key: 'ArrowDown' });
expect(fieldsOf(onChange.mock.calls.at(-1)![0])).toEqual(['name:asc', 'stage:desc', 'amount:asc']);
});

it('an arrow past either end changes nothing', () => {
const onChange = mount(THREE);
fireEvent.keyDown(screen.getAllByTestId('sort-builder-handle')[0], { key: 'ArrowUp' });
fireEvent.keyDown(screen.getAllByTestId('sort-builder-handle')[2], { key: 'ArrowDown' });
expect(onChange).not.toHaveBeenCalled();
});

it('a drag of the third handle onto the first row puts that level first', () => {
const onChange = mount(THREE);
const handle = screen.getAllByTestId('sort-builder-handle')[2];
const first = screen.getAllByTestId('sort-builder-row')[0];
fireEvent.dragStart(handle);
fireEvent.dragOver(first);
fireEvent.drop(first);
fireEvent.dragEnd(handle);
expect(fieldsOf(onChange.mock.calls.at(-1)![0])).toEqual(['amount:asc', 'name:asc', 'stage:desc']);
});

it('a drop with no drag of this list in flight changes nothing and is left to the host', () => {
const onChange = mount(THREE);
const hostDrop = vi.fn();
const row = screen.getAllByTestId('sort-builder-row')[0];
row.parentElement!.addEventListener('drop', hostDrop);
fireEvent.drop(row);
expect(onChange).not.toHaveBeenCalled();
expect(hostDrop).toHaveBeenCalled();
});

it('a single level: the handle is drawn but disabled, and drags nothing', () => {
mount([THREE[0]]);
const handle = screen.getByTestId('sort-builder-handle');
expect(handle).toHaveAttribute('aria-disabled', 'true');
expect(handle).toHaveAttribute('tabindex', '-1');
expect(handle).toHaveAttribute('draggable', 'false');
});
});
42 changes: 42 additions & 0 deletions ui/packages/components/src/context/gridHeaderMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 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.
*/

/**
* GridHeaderMenu — a data-table whose column header opens the column menu
* instead of sorting, opted into by a HOST, never by metadata
* (objectstack#35876).
*
* Inside a provider whose value is `true`, a click on a sortable column's
* header (or Enter, Space or ArrowDown while the header has focus) opens the
* column menu: sort ascending, sort descending, hide the column. The header
* itself never changes the sort. Outside a provider (`false`, the default)
* the header keeps its click-to-sort behaviour, so every host that does not
* mount this keeps what it had.
*
* `ObjectGrid` mounts it around the table it draws when its host asks for it
* (`ListView`, the list page, does).
*/

import React from 'react';

const GridHeaderMenuContext = React.createContext<boolean>(false);

export function GridHeaderMenuProvider({
value,
children,
}: {
value: boolean;
children: React.ReactNode;
}): React.ReactElement {
return <GridHeaderMenuContext.Provider value={value}>{children}</GridHeaderMenuContext.Provider>;
}

/** Whether a host asked this table's column headers to open the column menu. */
export function useGridHeaderMenu(): boolean {
return React.useContext(GridHeaderMenuContext);
}
Loading
Loading