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/35955-header-menu-hide-draft.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@object-ui/types': patch
'@object-ui/components': patch
'@object-ui/plugin-grid': patch
'@object-ui/plugin-list': patch
---

fix(console): Hide column in the column-header menu is a list draft, like the eye in View settings

Clause-②: yes (widening) — one new optional runtime slot `DataTableSchema.onColumnHide(columnKey)` (refused by name on the zod mirror, like `onColumnsReorder`) and one new optional `ObjectGrid` prop `onColumnHide(field)`; nothing removed or renamed
Metadata-protocol: none

On a Console object list, picking "Hide column" in a column header's menu hid the column inside the table only: nothing above the table heard of it, so the list drew neither Discard nor Save and only a reload brought the column back. The same choice made with the eye in View settings was already a draft.

The table now reports the hidden column to its host (`DataTableSchema.onColumnHide`, supplied by `ObjectGrid` from its new `onColumnHide` prop), and `ListView` writes it through the same writer as the hide-fields eye: the host hears it on `onHiddenFieldsChange` (the Console holds it in the list's draft, so Discard brings the column back and Save keeps it hidden), and a column the reader added leaves their added fields, as unticking it does. A table with no host handler hides the column in its own state, as before.
3 changes: 3 additions & 0 deletions ui/packages/components/src/renderers/complex/data-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2670,6 +2670,9 @@ const DataTableRenderer = ({ schema }: { schema: DataTableSchema }) => {
}
setColumns(prev => prev.filter(c => c.accessorKey !== columnKey));
setContextMenu(null);
// objectstack#35955 — a host that keeps the columns shown owns the choice
// (the list holds it in its draft, as the hide-fields eye does).
schema.onColumnHide?.(columnKey);
};

const handleSelectAll = (checked: boolean) => {
Expand Down
15 changes: 15 additions & 0 deletions ui/packages/plugin-grid/src/ObjectGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,17 @@ export interface ObjectGridComponentProps extends ObjectGridExternalPaginationPr
* key reaches it.
*/
headerMenu?: boolean;
/**
* [objectstack#35955] Hears the column menu's "Hide column" with the
* column's field, as the HOST that keeps the columns shown asks for it (the
* list page holds the hidden fields in its draft, as its hide-fields eye
* does). The table hides the column either way. Absent ⇒ the hide lives in
* the table's own state alone, as before this prop existed.
*
* A RUNTIME prop, never authored metadata: the host decides it, and no node
* key reaches it.
*/
onColumnHide?: (field: string) => void;
/**
* [objectstack#36015] Opens a record that a lookup cell's chip names, in
* place, as the HOST that owns the page does it (the list page opens it in
Expand Down Expand Up @@ -851,6 +862,7 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
rowOpensRecord,
cellSelection: hostCellSelection,
headerMenu: hostHeaderMenu,
onColumnHide: hostOnColumnHide,
openReferencedRecord,
onBulkDelete,
onRowSelect,
Expand Down Expand Up @@ -3774,6 +3786,9 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
// on `DataTableSchema` and stays unwired, exactly as the RuntimeOnlyDeclared
// ledger records it. Which of the two declared spellings survives is still an
// open ruling and is deliberately NOT settled here.
// objectstack#35955 — the column menu's "Hide column", to the host that
// keeps the columns shown.
...(hostOnColumnHide ? { onColumnHide: hostOnColumnHide } : {}),
onColumnsReorder: (newColumns: any[]) => {
const order = newColumns
.map((c) => c?.accessorKey)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* 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#35955 — through the real `ObjectGrid` and its `data-table`, the
* column menu's "Hide column" reaches the host that keeps the columns shown
* (`onColumnHide`, with the column's field), so the list page can hold it in
* the list's draft the way the hide-fields eye does. Before, the table only
* dropped the column from its own state: the column went, nothing above the
* table heard of it, and the list drew neither Discard nor Save.
*
* The CONTROL: with no `onColumnHide` the column still goes, so the hide is
* the table's own, as before.
*/
import { describe, it, expect, vi, beforeAll } from 'vitest';
import { render, screen, waitFor, fireEvent, within } from '@testing-library/react';
import '@testing-library/jest-dom';
import React from 'react';

import { ObjectGrid } from '../ObjectGrid';
import { registerAllFields } from '@object-ui/fields';
import { ActionProvider } from '@object-ui/react';

registerAllFields();

beforeAll(() => {
if (!Element.prototype.hasPointerCapture) {
Element.prototype.hasPointerCapture = vi.fn(() => false) as any;
}
if (!Element.prototype.scrollIntoView) {
Element.prototype.scrollIntoView = vi.fn() as any;
}
});

function makeDataSource() {
return {
find: vi.fn(async () => ({
data: [{ id: '1', name: 'Row 0', stage: 'open', amount: 10 }],
total: 1,
})),
getObjectSchema: async (name: string) => ({
name,
fields: {
id: { type: 'text' },
name: { type: 'text', label: 'Name' },
stage: { type: 'text', label: 'Stage' },
amount: { type: 'number', label: 'Amount' },
},
}),
} as any;
}

const headers = (container: HTMLElement) =>
Array.from(container.querySelectorAll<HTMLElement>('thead th[data-column-key]')).map((th) =>
th.getAttribute('data-column-key'),
);

function mount(onColumnHide?: (field: string) => void) {
return render(
<ActionProvider>
<ObjectGrid
schema={{
type: 'object-grid',
objectName: 'crm_deal',
columns: ['name', 'stage', 'amount'].map((field) => ({ field })),
} as any}
dataSource={makeDataSource()}
headerMenu
{...(onColumnHide ? { onColumnHide } : {})}
/>
</ActionProvider>,
);
}

async function hideFromHeaderMenu(container: HTMLElement, field: string) {
const th = container.querySelector<HTMLElement>(`thead th[data-column-key="${field}"]`)!;
fireEvent.click(th);
fireEvent.click(within(screen.getByTestId('column-context-menu')).getByText('Hide column'));
}

describe('ObjectGrid — the column menu\'s Hide column reaches the host (objectstack#35955)', () => {
it('hands the host the hidden column\'s field, once, and the column goes', async () => {
const onColumnHide = vi.fn();
const { container } = mount(onColumnHide);
await waitFor(() => expect(screen.getByText('Row 0')).toBeInTheDocument());
expect(headers(container)).toEqual(expect.arrayContaining(['name', 'stage', 'amount']));

await hideFromHeaderMenu(container, 'stage');

expect(onColumnHide).toHaveBeenCalledTimes(1);
expect(onColumnHide).toHaveBeenCalledWith('stage');
await waitFor(() => expect(headers(container)).not.toContain('stage'));
});

it('CONTROL: with no host handler the column still goes, in the table alone', async () => {
const { container } = mount();
await waitFor(() => expect(screen.getByText('Row 0')).toBeInTheDocument());
await hideFromHeaderMenu(container, 'stage');
await waitFor(() => expect(headers(container)).not.toContain('stage'));
expect(headers(container)).toEqual(expect.arrayContaining(['name', 'amount']));
});
});
17 changes: 17 additions & 0 deletions ui/packages/plugin-list/src/ListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6675,6 +6675,17 @@ export const ListView = React.forwardRef<ListViewHandle, ListViewProps>(({
updateOfferedHiddenFields(hidden);
}
};
// objectstack#35955 — "Hide column" in the grid's column menu is the eye's
// own choice, made on one column: it goes through the same writer, so the
// host holds it in the list's draft (Discard brings the column back, Save
// keeps it hidden), and an added column leaves the reader's added fields as
// it does there. A key that names no column this list draws is the table's
// own business and is not reported.
const hideColumnFromMenu = (field: string) => {
if (offeredHiddenFields.has(field)) return;
if (!effectiveFields.some((column: unknown) => columnIdentity(column as any) === field)) return;
updateListedFields(new Set([...offeredHiddenFields, field]));
};
// Ticking an offered field adds it to the reader's list. A stale hidden
// entry with the same name (a column the view has since dropped) is cleared
// with it, or the added column would be drawn hidden.
Expand Down Expand Up @@ -8494,6 +8505,12 @@ export const ListView = React.forwardRef<ListViewHandle, ListViewProps>(({
// menu instead of sorting on a click.
? { cellSelection: true, headerMenu: true }
: {})}
{...(viewComponentSchema.type === 'object-grid'
// objectstack#35955 — the column menu's "Hide column" is the
// hide-fields eye's choice (`hideColumnFromMenu`), on the grid's
// host channel (a React prop, never a node key).
? { onColumnHide: hideColumnFromMenu }
: {})}
{...(ROW_OPENS_RECORD_VIEW_TYPES.has(viewComponentSchema.type) && rowOpensRecord !== undefined
// objectstack#33317, objectstack#33548 — whether the host's row
// click opens anything, on the view's host channel (a React
Expand Down
120 changes: 120 additions & 0 deletions ui/packages/plugin-list/src/__tests__/ListView.harness.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3416,6 +3416,126 @@ describe('ListView.setupChrome-12606', () => {
});
});

// ── ListView.headerMenuHide-35955 ───────────────────────────────────────────────

/**
* objectstack#35955 — "Hide column" in the grid's column menu is the same
* choice as the eye in the hide-fields lists. The grid hands the column's
* field back (`onColumnHide`, pinned through the real grid and table in
* plugin-grid's `ObjectGrid.headerMenuHide-35955.test.tsx`); here: the list
* writes it through the eye's own writer, so its host hears it on
* `onHiddenFieldsChange` (the console holds it in the list's draft, which is
* what draws Discard and Save) and the grid is handed the columns without it.
* An added column leaves the reader's added fields, as unticking it does. A
* key naming no column the list draws reports nothing.
*
* plugin-grid is not a dependency of plugin-list, so a stub `object-grid`
* captures what ListView hands it.
*/
describe('ListView.headerMenuHide-35955', () => {
restoreAfterBlock();

const OBJECT = 'meeting_35955';
const DEFINITION = {
name: OBJECT,
label: 'Meeting',
fields: {
name: { type: 'text', label: 'Name' },
meeting_date: { type: 'date', label: 'Date' },
location: { type: 'text', label: 'Location' },
room: { type: 'text', label: 'Room' },
},
};

let gridProps: any = null;
let previousGrid: ReturnType<typeof ComponentRegistry.get>;
beforeAll(() => {
previousGrid = ComponentRegistry.get('object-grid');
ComponentRegistry.register('object-grid', (props: any) => {
gridProps = props;
return <div data-testid="grid-stub" />;
});
});
afterAll(() => {
if (previousGrid) ComponentRegistry.register('object-grid', previousGrid);
});
beforeEach(() => { gridProps = null; });

const drawn = () => (gridProps?.columns ?? []).map((c: any) => (typeof c === 'string' ? c : c.field));

function mount(opts: { hiddenFields?: string[]; added?: string[] } = {}) {
const onHiddenFieldsChange = vi.fn();
const onAddedFieldsChange = vi.fn();
const dataSource = {
find: vi.fn().mockResolvedValue({ data: [{ id: '1', name: 'A' }], total: 1 }),
findOne: vi.fn(),
create: vi.fn(),
update: vi.fn(),
delete: vi.fn(),
getObjectSchema: vi.fn(async () => structuredClone(DEFINITION)),
};
const schema = {
type: 'list-view',
objectName: OBJECT,
viewType: 'grid',
columns: ['name', 'meeting_date', 'location', ...(opts.added ?? [])],
...(opts.hiddenFields ? { hiddenFields: opts.hiddenFields } : {}),
} as unknown as ListViewSchema;
render(
<SchemaRendererProvider dataSource={dataSource as never}>
<ListView
schema={schema}
dataSource={dataSource as never}
onHiddenFieldsChange={onHiddenFieldsChange}
onAddedFieldsChange={onAddedFieldsChange}
addedFields={opts.added ?? []}
/>
</SchemaRendererProvider>,
);
return { onHiddenFieldsChange, onAddedFieldsChange, dataSource };
}

async function hideFromMenu(field: string) {
await waitFor(() => expect(typeof gridProps?.onColumnHide).toBe('function'));
await act(async () => { gridProps.onColumnHide(field); });
}

it('hands the grid the column menu\'s hide, and reports it as the eye does: the hidden list, the column gone', async () => {
const { onHiddenFieldsChange } = mount();
await waitFor(() => expect(drawn()).toEqual(['name', 'meeting_date', 'location']));
await hideFromMenu('location');
expect(onHiddenFieldsChange.mock.calls.at(-1)![0]).toEqual(['location']);
await waitFor(() => expect(drawn()).toEqual(['name', 'meeting_date']));
});

it('adds to the columns already hidden, and hiding a hidden one again reports nothing', async () => {
const { onHiddenFieldsChange } = mount({ hiddenFields: ['meeting_date'] });
await waitFor(() => expect(drawn()).toEqual(['name', 'location']));
await hideFromMenu('location');
expect(onHiddenFieldsChange).toHaveBeenCalledTimes(1);
expect([...onHiddenFieldsChange.mock.calls[0][0]].sort()).toEqual(['location', 'meeting_date']);
await hideFromMenu('meeting_date');
expect(onHiddenFieldsChange).toHaveBeenCalledTimes(1);
});

it('a column the reader added leaves their added fields instead of being hidden', async () => {
const { onHiddenFieldsChange, onAddedFieldsChange } = mount({ added: ['room'] });
await waitFor(() => expect(drawn()).toContain('room'));
await hideFromMenu('room');
expect(onAddedFieldsChange.mock.calls.at(-1)![0]).toEqual([]);
expect(onHiddenFieldsChange).not.toHaveBeenCalled();
});

it('a key that names no column the list draws reports nothing', async () => {
const { onHiddenFieldsChange, onAddedFieldsChange } = mount();
await waitFor(() => expect(drawn()).toEqual(['name', 'meeting_date', 'location']));
await hideFromMenu('_row_number');
expect(onHiddenFieldsChange).not.toHaveBeenCalled();
expect(onAddedFieldsChange).not.toHaveBeenCalled();
expect(drawn()).toEqual(['name', 'meeting_date', 'location']);
});
});

// ── ListView.groupPickerUnstored-32631 ──────────────────────────────────────────

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ type _SummaryRowShapeWithoutLeadIsRefused = Expect<Equal<Declared<DataTableSchem
type _FloatingBarIsDeclared = Expect<IsDeclaredOn<'floatingBar'>>;
type _FloatingBarShape = Expect<Equal<Declared<DataTableSchema>['floatingBar'], React.ReactNode>>;

/**
* objectstack#35955 — the column menu's "Hide column" handler, `onColumnHide`:
* `data-table.tsx` calls it with the column's key; `ObjectGrid` supplies it.
*/
type _OnColumnHideIsDeclared = Expect<IsDeclaredOn<'onColumnHide'>>;
type _OnColumnHideShape = Expect<Equal<Declared<DataTableSchema>['onColumnHide'], ((columnKey: string) => void) | undefined>>;

describe('objectui#6882 — DataTableSchema declares the two keys data-table reads', () => {
/**
* The runtime half exists only so the compile-time pins above have a file
Expand Down
Loading
Loading