Skip to content

Commit 4099ef2

Browse files
fix(list): "Add group field" asks which field, and offers only fields that can group (#36204) (#36223)
<!-- claude-projects-attribution: {"github_login":"os-sales"} --> _Requested by **Sales** · [project thread](https://claude.ai/code/project/chan_01D5bWFY3rFzgJcytD5gD4Uu?thread=cmsg_01D5bWFY3rFzgJcytD5gD4UuTGE2nV5qgwDdDHZ12i82BA)_ Fixes #36204 Clause-②: yes (widening) — `@object-ui/components`'s `GroupingEditor` gains the optional props `askFieldOnAdd`, `fieldOptionsLoading` and `fieldLabel`, and its `labels` the optional `noFields`; `@object-ui/i18n` gains `list.noGroupableFields` in every locale (zh-Hant regenerated); nothing removed or renamed. `@object-ui/plugin-list`'s package exports are unchanged. Metadata-protocol: none ## 维护者速读 - 列表里点「添加分组字段」:不带默认值,也不马上分组。字段下拉直接展开,选了才分组;不选关掉,列表不分组,也不出现「放弃」「保存」。 - 下拉只列单选(状态也是单选)、勾选、人员、关联记录,每种只含单值的。文本、数字、日期、多选都不列。先列这个视图的列(按列的顺序),再列对象里其它这类字段,负责人也在里面。HotCRM「全部客户」下拉是 行业、类型、健康度…母公司、是否活跃…客户负责人,没有 客户名称、年营收、日期。 - 对象里一个这类字段都没有:按钮灰掉,下面写「没有可以用来分组的字段」。字段类型还没读回来的一瞬间按钮也是灰的,但不显示这句话,不会误报。 - 已经按别的字段(比如名称)分组的视图照常显示,这一级仍能换、能删。分组仍是草稿(#35955),不写视图。 ## What changes The card was rewritten after this PR's first version (rewrite note 7072750584, claim amend 7073321855): the first version's "default to the first single select" and "suitable types first, others still selectable" are both gone. In both Group editors (the wide toolbar's Group panel and the 视图设置 menu's Group level): 1. **No default, no grouping on the click.** "Add group field" opens a row whose field dropdown is already open. The level is written only on a pick; closing the dropdown without one writes nothing. 2. **Only fields that can group** (`isGroupableField`): `select`, `radio`, `boolean`, `toggle`, `user`, `lookup`, `master_detail`, `tree`, and not `multiple: true`. Order: the view's columns in the reader's column order (`schema.columnState.order`), then the object's other fields of those types the reader may read and the definition does not hide, business fields before system ones (the Filter panel's tiers). 3. **None to offer:** the button is drawn disabled with `aria-describedby` on the reason 「没有可以用来分组的字段」 / "No field here can group the list". 4. **Still a draft** (#35955): the pick goes through `onGroupingChange` to the host's `listDraft.patch` and the URL, as before. Not changed, per the card: the group display, kanban, date grouping. A view already grouped by another kind of field keeps its level, shown under the field's label (`fieldLabel`), and the level can still be changed or removed. Code: - `@object-ui/components` `GroupingEditor`: `askFieldOnAdd` (the pending row, testid `grouping-field-pending`), `fieldOptionsLoading`, `labels.noFields` (disabled button + `grouping-no-fields`), `fieldLabel`. Without them the editor behaves as before. - `@object-ui/plugin-list` `ListView`: one `groupOptions` list and one `groupFieldLabel` for both editors; `ViewSettingsPopover` takes `groupOptions` / `groupOptionsLoading` from it (`groupFieldOptions` and `isGroupableField` are module helpers, not package exports). `allFields` carries `type` and `multiple` from the definition. - Platform checklist `records-forms.grid-personalization` rev 11 (after #36133's rev 10): steps[3] says the dropdown opens without grouping, lists only groupable fields, and Status is picked there. ### Small calls made in this thread (Sales 22:19:27Z rule) - **A field whose type is not loaded yet** (L2 should-fix): while the definition read is in flight, nothing is offered and the button is disabled *without* the reason, so it never says "nothing to group by" before the types are known. If there is no definition to read at all (a host with no `getObjectSchema`, or the read failed), no type is known and every column is offered, as before. Both pinned. - **"Status"**: the spec has no `status` type; a status is a `select`. `radio` counts as a single select. - **"Linked record"** includes `master_detail` and `tree` beside `lookup`; **"tick"** includes `toggle` beside `boolean`. - **Fields outside the columns**: the card says 「这个对象没有…」, so the object's other groupable fields are offered after the columns. The record owner is one (the maintainer's 「加上负责人」). Created by / updated by are offered only when the view shows them as a column; otherwise every object would have a person field and the disabled state could never appear. - **A field the reader may not read** is never offered; a level already holding one keeps showing its name, not its label (#11984). - **Second level**: the same list, less the fields levels use. ## Verification - `ui/packages/components/src/__tests__/grouping-editor-add-default-36204.test.tsx` (11): the click writes nothing and opens the dropdown; a pick writes the level; second level; Escape writes nothing; `noFields` disabled + described; loading disabled without a reason; enabled when options arrive; `fieldLabel`; three CONTROLs without the new props. - `ui/packages/plugin-list/src/__tests__/ListView.groupAddDefault-36204.test.tsx` (16), both editors: the click writes nothing, the dropdown is Industry, Owner, Stage, VIP, Parent account, Record owner (no name, text, date, number, multi-select, hidden field, unshown created-by), a pick writes the level with no `create`/`update`/`delete`; Escape; no groupable field → disabled + reason; second level; column order; created-by as a column is offered; a view grouped by name shows "Customer name" and can change; loading → disabled without reason, enabled once the definition answers; CONTROL with no definition offers every column; View settings labels a non-column pick (Record owner) on its level and on the menu's Group row; the helper functions. - Direction, measured: with `main`'s `grouping-editor.tsx`, `ListView.tsx` and `ViewSettingsPopover.tsx` swapped in, 22 of these 26 fail; the other four include the three editor CONTROLs. The View settings label pin added at `89c82900` fails with that commit's one-line host change reverted. - Updated where tests read the old first-column default: `ListView.harness` (#32631: fields typed, CONTROL re-reads the level options after the pick), `ListView.onGroupingChange-11860` (stub answers `getObjectSchema`; the add picks from the dropdown), `ListView.fieldListRead-11984` (the Group list offers `Status`/`Secret Status` only; the full-read control probes Group with the select), `ObjectView.listUrlState-11860` (no definition there, so every column, `priority` picked). - `vitest run` packages/plugin-list, components `grouping*` + `list-builders*`, app-shell `ObjectView.listUrlState-11860`, packages/i18n: 293 files, 4039 passed / 13 skipped. - `tsc --noEmit` and `tsconfig.test.json`: components, plugin-list, app-shell, i18n, exit 0. ESLint on changed files: 0 errors. `check-platform-checklist.mjs`: OK. Changeset checks: OK. ## Evidence (1440×900 @2x, zh-CN, light and dark; `/mnt/project-files/build-seat1/36204/`, index in its `README.md`) Console from this branch's `ui/`; every non-GET request recorded in the page. - Self-built 「客户」 (package `com.demo.kehu`), 「全部客户」 (客户名称, 所属行业, 负责人, 电话): `v2-1440-{light,dark}-kehu-all-1-after-add-dropdown.png` — the click opens the dropdown (所属行业, 负责人), list not grouped, no request. `-2-list-grouped.png` — after picking 所属行业: 4 groups of 3, 「放弃」「保存」; the only request is the grouped read `POST /api/v1/data/kehu_customer/query`. - 「联系方式」 (客户名称, 负责人, 电话, 签约日期): `v2-…-kehu-contact-1-after-add-dropdown.png` (负责人, then the non-column 所属行业), `-2-closed-no-pick.png` (Escape: no level, no 「放弃」「保存」, no request). - 拜访记录 (text, textarea, number, date, multi-select + platform owner): `v2-…-kehu-visit-*` — dropdown 负责人 only. - 便签 (`ownership: 'org'`, no owner; only text, textarea, number, date, multi-select): `v2-…-kehu-note-nofield-0-group-panel.png` — disabled, 「没有可以用来分组的字段」, a click does nothing. - HotCRM (`main` 6090c83) 客户 → 全部客户: `v2-…-hotcrm-accounts-1-after-add-dropdown.png` — 行业, 类型, 健康度, 商务能力, 付款周期, 销售区域, 母公司, 是否活跃, 审批状态, 来源线索, 客户分层, 客户细分, 客户负责人; no write, not grouped. `-2-list-grouped.png` — grouped by 行业, 「放弃」「保存」; only the grouped read. - `before-*` shots show `main` (one click groups by 客户名称, 12 groups of one). `v1/` holds this PR's superseded first version. ## Since the first reads (L1-b ACCEPT 7073310792, L2 FAIL 7073316679) The rework answers the L2's four must-fixes (default, filter, disabled state, rewrite note) and its should-fixes (untyped-field rule + test, checklist, changeset, body). Nits: the plugin-list members are out of Clause-② (not package-public); the harness CONTROL re-checks the level options after the pick. Heads: `4e90470e` (rework), `953c8cbf` (record owner offered, provenance only as a column; L1-b delta ACCEPT 7074055354, declaration L2 delta PASS 7074084200), `89c82900` (L2 S1: `ListView` hands `groupFieldLabel` to `ViewSettingsPopover`, which labels the menu's Group row and the level with it, so a non-column pick no longer reads as `owner_id`/`industry`; pinned; browser: 「联系方式」 pick 所属行业, the row reads 「分组 所属行业」, `v2-1440-{light,dark}-kehu-contact-3-menu-row-noncolumn.png`. L2 N4: the changeset headline is `fix(list)` like the title). ## Overlap `main` merged after #36133 (`0d8fc77b`): the one conflict was `records-forms.grid-personalization`, resolved by keeping #36133's steps[2] and rev 10 row; this PR's row is rev 11. ## Human Minimum - [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic. (Not touched.) - [x] 2. Every name I added uses the casing for its kind. - [x] 3. No formatter ran; `pnpm lint` passes (changed files: 0 errors). - [x] 4. One issue, one branch off an up-to-date `main`, one pull request. - [x] 5. A change that publishes carries a changeset, and both declaration lines are filled in here and in the changeset. - [x] 6. `packages/spec` touched: its generated artifacts are regenerated and committed. (Not touched.) - [x] 7. No hand edits to `content/docs/references/`, `packages/*/CHANGELOG.md` or `content/docs/releases/`. - [x] 8. Title, body and comments are in English. - [x] The *Before Submitting* list in CONTRIBUTING.md is done. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01L6bhgXeD1yUYqBJm8uzRrP <!-- signed: repo:objectstack#1 · os-sales · session_01L6bhgXeD1yUYqBJm8uzRrP --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent b57f778 commit 4099ef2

22 files changed

Lines changed: 796 additions & 53 deletions
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
'@object-ui/components': patch
3+
'@object-ui/plugin-list': patch
4+
'@object-ui/i18n': patch
5+
---
6+
7+
fix(list): "Add group field" asks which field, and offers only fields that can group
8+
9+
Clause-②: yes (widening) — `@object-ui/components`'s `GroupingEditor` gains the optional props `askFieldOnAdd`, `fieldOptionsLoading` and `fieldLabel`, and its `labels` the optional `noFields`; `@object-ui/i18n` gains the key `list.noGroupableFields` in every locale; nothing removed or renamed
10+
Metadata-protocol: none
11+
12+
On a list, 「添加分组字段」 / "Add group field" used to group at once by the list's first column, usually the record's name, so every record became a group of its own. Now, in the toolbar's Group panel and in the view settings menu's Group level alike:
13+
14+
- It adds no field by itself and does not group. The field dropdown opens, and the list is grouped once a field is picked there. Closing the dropdown without a pick leaves the list as it was.
15+
- The dropdown lists only the fields that can group: single select or radio (a status is one), tick (boolean or toggle), person, and linked record (lookup, master-detail, tree), each single-valued. Text, number, date and multi-select fields are not offered. The list's columns come first in the reader's column order, then the object's other groupable fields the reader may read, business fields before system ones such as the record owner. Created by and updated by are offered only when the view shows them as a column.
16+
- With no field that can group, the button is drawn disabled and says why: 「没有可以用来分组的字段」 / "No field here can group the list".
17+
- Until the object's field types are read, the button is disabled without a reason. When no definition can be read at all, every column is offered, as before.
18+
- A view already grouped by another kind of field keeps showing that grouping, and its level can still be changed or removed.
19+
- A second level follows the same rule among the fields no level uses yet.
20+
- The grouping is still a part of the list's draft: **Discard** and **Save** appear, and nothing is written.
21+
22+
`@object-ui/components`: with `askFieldOnAdd`, `GroupingEditor`'s "Add group field" opens a row whose field dropdown is already open and writes the level only on a pick. With no `fieldOptions` it draws the button disabled, explained by `labels.noFields`, or without a reason while `fieldOptionsLoading`. `fieldLabel(field)` labels a level whose field `fieldOptions` does not carry. Without these props the editor behaves as before.

‎docs/qa/platform-checklist/areas/records-forms.json‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3610,7 +3610,7 @@
36103610
"title": "Grid personalization persists across reload: column resize/reorder/pin, row-height, group-by (with API-true totals), row-color",
36113611
"since": "v16",
36123612
"status": "active",
3613-
"revision": 10,
3613+
"revision": 11,
36143614
"priority": "P2",
36153615
"surface": "browser",
36163616
"personas": [
@@ -3637,7 +3637,7 @@
36373637
"open showcase_task grid; screenshot",
36383638
"resize a column (reorder two columns only on a grid that sets reorderableColumns; there is no runtime pin control — acceptance[0]), and set a row height on the Row height level of the list's options menu (the toolbar's options trigger, then 行高 / Row height: three choices, 紧凑 / 标准 / 宽松 — Compact / Standard / Spacious, ui/packages/plugin-list/src/components/ViewSettingsPopover.tsx; a gallery's Row height level picks its card size the same way) — three modes, stored as rowHeight compact / medium / tall (the spec's five row heights compact/short/medium/tall/extra_tall narrow onto these three: objectui core normalize-list-view.ts DENSITY_MODE_TO_ROW_HEIGHT / ROW_HEIGHT_TO_DENSITY_MODE)",
36393639
"reload; confirm the column widths and row-height restored — they are kept in this browser for the signed-in user, localStorage list-layout:<userId>:showcase_task (perView.<view>.widths / rowHeight, objectstack#35955; before it the user's sys_user_preference ui.list.showcase_task row, objectui#12418). A column ORDER is not: since objectstack#35955 it is the list's unsaved draft, so a reload reopens on the view's own order unless it was saved into a view, with localStorage grid-columns-showcase_task[-<viewId>] as the component's cache; the old per-view override through dataSource.updateViewConfig is retired (objectui#12504). Also open the list in a FRESH browser context as the same user: the layout does NOT follow the user there — it opens on the view's own widths and row height, by design (objectstack#35955)",
3640-
"group-by status ('Add group field' pre-selects the first field, Title, and groups at once, so switch it to Status). A runtime grouping lives in the list's URL (uf__group, ui/packages/app-shell/src/views/objectViewListUrlState.ts), not in sys_user_preference: reloading that URL keeps it, a reload of the bare list URL reopens on the view's own grouping by design, and steps[6] reads it that way (QA run #33861). Confirm per-group headers appear and each group shows its record count — summarizeField (estimate_hours) is declared on the KANBAN config of task.view.ts, not on the grid, so grid groups carry counts; clear grouping and confirm the headers are removed cleanly",
3640+
"group-by status ('Add group field' adds no field and does not group: the field dropdown opens listing only the fields that can group, single select or radio, tick, person and linked record, so no Title, number or date, objectstack#36204; pick Status there. Closing it without a pick leaves the grid ungrouped, and a grid with no such field draws the button disabled with its reason). A runtime grouping lives in the list's URL (uf__group, ui/packages/app-shell/src/views/objectViewListUrlState.ts), not in sys_user_preference: reloading that URL keeps it, a reload of the bare list URL reopens on the view's own grouping by design, and steps[6] reads it that way (QA run #33861). Confirm per-group headers appear and each group shows its record count — summarizeField (estimate_hours) is declared on the KANBAN config of task.view.ts, not on the grid, so grid groups carry counts; clear grouping and confirm the headers are removed cleanly",
36413641
"cross-check each group's count against a direct per-group server count (GET /api/v1/data/showcase_task filtered to that status, read its total)",
36423642
"read the row-color rule the stock Grid view declares — rowColor on priority with the colors map low slate / medium blue / high amber / urgent red (examples/app-showcase/src/ui/views/task.view.ts); there is no runtime row-color authoring control — and confirm rows paint by it (useRowColor, the -100 tint of each name)",
36433643
"reload once more; confirm every personalization this item stores survives (column widths, row height); capture a screenshot per variant"
@@ -3749,6 +3749,12 @@
37493749
"date": "2026-11-21",
37503750
"change": "steps[2], acceptance[1] (verify), acceptance[4] (clause, verify, evidence) and source[2] re-pointed (objectstack#35955 PR3, Sales ruling D1 on #35955): column widths and row height are kept in this browser only, localStorage list-layout:<user id>:<object>, and the ui.list.<object> row is retired. Changed on purpose: they no longer follow the user to a fresh browser. Not loosened: they still survive a reload in the same browser and never reach another account",
37513751
"ref": "#35955"
3752+
},
3753+
{
3754+
"revision": 11,
3755+
"date": "2026-11-22",
3756+
"change": "steps[3] re-pointed (objectstack#36204): 'Add group field' no longer pre-selects the first column; it opens the field dropdown without grouping, the dropdown lists only fields that can group (single select or radio, tick, person, linked record), the grouping happens on the pick of Status, and with no such field the button is disabled with its reason. No clause changed.",
3757+
"ref": "#36204"
37523758
}
37533759
]
37543760
},

‎ui/packages/app-shell/src/views/ObjectView.listUrlState-11860.test.tsx‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,19 @@ async function openGroupPanel() {
758758
await settle();
759759
}
760760

761+
/**
762+
* "Add group field", then the field. This harness's data source answers no
763+
* `getObjectSchema`, so the list knows no field's type and the button opens
764+
* the field dropdown instead of adding a level (objectstack#36204): the level
765+
* is written once a field is picked there.
766+
*/
767+
async function addGroupLevel(name: string) {
768+
fireEvent.click(screen.getByTestId('grouping-add'));
769+
const listbox = await screen.findByRole('listbox');
770+
// No definition either for the labels: the options are the field names.
771+
fireEvent.click(within(listbox).getByRole('option', { name }));
772+
}
773+
761774
/** The Group level's own Clear, inside the Display menu. */
762775
function clearGrouping() {
763776
const panel = screen.getByTestId('view-settings-content');
@@ -787,7 +800,7 @@ describe('the toolbar grouping is in the URL too (objectui#11860)', () => {
787800
const host = await mountHost(['/elsewhere', `${VIEW}/all`]);
788801
expect(groupedBy()).toBeUndefined();
789802
await openGroupPanel();
790-
fireEvent.click(screen.getByTestId('grouping-add'));
803+
await addGroupLevel('priority');
791804
await settle();
792805
const written = groupParamOf(host);
793806
expect(written?.fields).toHaveLength(1);
@@ -808,7 +821,7 @@ describe('the toolbar grouping is in the URL too (objectui#11860)', () => {
808821
it('the grouping a user set on a link survives a reload of that link', async () => {
809822
const host = await mountHost([`${VIEW}/all`]);
810823
await openGroupPanel();
811-
fireEvent.click(screen.getByTestId('grouping-add'));
824+
await addGroupLevel('priority');
812825
await settle();
813826
const url = `${host.pathname()}${host.search()}`;
814827
const field = groupParamOf(host).fields[0].field;
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* "Add group field" adds no field by itself (objectstack#36204).
11+
*
12+
* It used to add the first field no level used, at once: in a list the first
13+
* column, a name as a rule, so the first click grouped the records one per
14+
* group. With `askFieldOnAdd` the click writes nothing and opens a row whose
15+
* field dropdown is already open; the level is written only once a field is
16+
* picked there, and closing the dropdown without a pick writes nothing.
17+
* With no field to offer the button stays, disabled, and `labels.noFields`
18+
* says why; while `fieldOptionsLoading` it is disabled without a reason.
19+
*
20+
* DIRECTION: every pin but the CONTROLs fails on the old editor (the button
21+
* wrote the first field at once, no pending row existed, and with no option
22+
* the button was simply not drawn). The CONTROLs are the old behaviour, kept
23+
* for a host that passes none of the new props.
24+
*/
25+
import { describe, it, expect, vi } from 'vitest';
26+
import React from 'react';
27+
import { render, screen, fireEvent, waitFor, within } from '@testing-library/react';
28+
import '@testing-library/jest-dom';
29+
import { GroupingEditor, type GroupingConfigValue, type GroupingEditorProps } from '../custom/grouping-editor';
30+
31+
const FIELD_OPTIONS = [
32+
{ value: 'status', label: 'Status' },
33+
{ value: 'owner', label: 'Owner' },
34+
{ value: 'vip', label: 'VIP' },
35+
];
36+
37+
function renderEditor(value: GroupingConfigValue | undefined, extra: Partial<GroupingEditorProps> = {}) {
38+
const onChange = vi.fn();
39+
const props: GroupingEditorProps = { value, onChange, fieldOptions: FIELD_OPTIONS, askFieldOnAdd: true, ...extra };
40+
const utils = render(<GroupingEditor {...props} />);
41+
return { ...utils, onChange, props };
42+
}
43+
44+
const labels = (options: HTMLElement[]) => options.map((o) => o.textContent ?? '');
45+
46+
describe('"Add group field" opens the field dropdown and writes nothing until a pick', () => {
47+
it('opens a row with its dropdown open, writes nothing, and hides the add button', async () => {
48+
const { onChange } = renderEditor(undefined);
49+
fireEvent.click(screen.getByTestId('grouping-add'));
50+
expect(onChange).not.toHaveBeenCalled();
51+
expect(screen.getByTestId('grouping-field-pending')).toBeInTheDocument();
52+
expect(screen.queryByTestId('grouping-add')).toBeNull();
53+
// No level yet: the list is not grouped.
54+
expect(screen.queryByTestId('grouping-field-0')).toBeNull();
55+
const listbox = await screen.findByRole('listbox');
56+
expect(labels(within(listbox).getAllByRole('option'))).toEqual(['Status', 'Owner', 'VIP']);
57+
});
58+
59+
it('a pick writes the level and the row becomes that level', async () => {
60+
const { onChange, rerender, props } = renderEditor(undefined);
61+
fireEvent.click(screen.getByTestId('grouping-add'));
62+
const listbox = await screen.findByRole('listbox');
63+
fireEvent.click(within(listbox).getAllByRole('option').find((o) => o.textContent === 'Owner')!);
64+
expect(onChange).toHaveBeenCalledTimes(1);
65+
const written = onChange.mock.calls[0][0];
66+
expect(written).toStrictEqual({ fields: [{ field: 'owner', order: 'asc', collapsed: false }] });
67+
rerender(<GroupingEditor {...props} value={written} />);
68+
await waitFor(() => expect(screen.queryByTestId('grouping-field-pending')).toBeNull());
69+
expect(screen.getByTestId('grouping-field-0')).toHaveTextContent('Owner');
70+
expect(screen.getByTestId('grouping-add')).toBeInTheDocument();
71+
});
72+
73+
it('a second level offers only the fields no level uses and appends', async () => {
74+
const { onChange } = renderEditor({ fields: [{ field: 'status', order: 'desc', collapsed: true }] });
75+
fireEvent.click(screen.getByTestId('grouping-add'));
76+
const listbox = await screen.findByRole('listbox');
77+
expect(labels(within(listbox).getAllByRole('option'))).toEqual(['Owner', 'VIP']);
78+
fireEvent.click(within(listbox).getAllByRole('option').find((o) => o.textContent === 'VIP')!);
79+
expect(onChange.mock.calls[0][0]).toStrictEqual({
80+
fields: [
81+
{ field: 'status', order: 'desc', collapsed: true },
82+
{ field: 'vip', order: 'asc', collapsed: false },
83+
],
84+
});
85+
});
86+
87+
it('closing the dropdown without a pick drops the row and writes nothing', async () => {
88+
const { onChange } = renderEditor(undefined);
89+
fireEvent.click(screen.getByTestId('grouping-add'));
90+
const listbox = await screen.findByRole('listbox');
91+
fireEvent.keyDown(listbox, { key: 'Escape' });
92+
await waitFor(() => expect(screen.queryByTestId('grouping-field-pending')).toBeNull());
93+
expect(onChange).not.toHaveBeenCalled();
94+
expect(screen.getByTestId('grouping-add')).toBeInTheDocument();
95+
});
96+
97+
it('CONTROL: without `askFieldOnAdd` the first unused field is added at once, as before', () => {
98+
const { onChange } = renderEditor(undefined, { askFieldOnAdd: undefined });
99+
fireEvent.click(screen.getByTestId('grouping-add'));
100+
expect(onChange.mock.calls[0][0]).toStrictEqual({
101+
fields: [{ field: 'status', order: 'asc', collapsed: false }],
102+
});
103+
});
104+
});
105+
106+
describe('no field to offer: the button stays, disabled, and says why', () => {
107+
it('with `labels.noFields` the button is disabled and described by the reason', () => {
108+
const { onChange } = renderEditor(undefined, { fieldOptions: [], labels: { noFields: 'Nothing can group this' } });
109+
const button = screen.getByTestId('grouping-add');
110+
expect(button).toBeDisabled();
111+
const reason = screen.getByTestId('grouping-no-fields');
112+
expect(reason).toHaveTextContent('Nothing can group this');
113+
expect(button).toHaveAttribute('aria-describedby', reason.id);
114+
fireEvent.click(button);
115+
expect(onChange).not.toHaveBeenCalled();
116+
expect(screen.queryByTestId('grouping-field-pending')).toBeNull();
117+
});
118+
119+
it('while the options load the button is disabled without a reason', () => {
120+
renderEditor(undefined, { fieldOptions: [], fieldOptionsLoading: true, labels: { noFields: 'Nothing can group this' } });
121+
const button = screen.getByTestId('grouping-add');
122+
expect(button).toBeDisabled();
123+
expect(button).not.toHaveAttribute('aria-describedby');
124+
expect(screen.queryByTestId('grouping-no-fields')).toBeNull();
125+
});
126+
127+
it('once the options arrive the button is enabled', async () => {
128+
const { rerender, props } = renderEditor(undefined, { fieldOptions: [], fieldOptionsLoading: true });
129+
expect(screen.getByTestId('grouping-add')).toBeDisabled();
130+
rerender(<GroupingEditor {...props} fieldOptions={FIELD_OPTIONS} fieldOptionsLoading={false} />);
131+
expect(screen.getByTestId('grouping-add')).toBeEnabled();
132+
fireEvent.click(screen.getByTestId('grouping-add'));
133+
expect(await screen.findByRole('listbox')).toBeInTheDocument();
134+
});
135+
136+
it('CONTROL: with no option and no `noFields`, no add button is drawn, as before', () => {
137+
renderEditor(undefined, { fieldOptions: [], askFieldOnAdd: undefined });
138+
expect(screen.queryByTestId('grouping-add')).toBeNull();
139+
expect(screen.queryByTestId('grouping-no-fields')).toBeNull();
140+
});
141+
});
142+
143+
describe('a level grouped by a field the host does not offer', () => {
144+
it('shows the label `fieldLabel` gives', () => {
145+
renderEditor({ fields: [{ field: 'customer_name', order: 'asc', collapsed: false }] }, {
146+
fieldLabel: (f) => (f === 'customer_name' ? 'Customer name' : undefined),
147+
});
148+
expect(screen.getByTestId('grouping-field-0')).toHaveTextContent('Customer name');
149+
});
150+
151+
it('CONTROL: without `fieldLabel` it shows the field name', () => {
152+
renderEditor({ fields: [{ field: 'customer_name', order: 'asc', collapsed: false }] });
153+
expect(screen.getByTestId('grouping-field-0')).toHaveTextContent('customer_name');
154+
});
155+
});

0 commit comments

Comments
 (0)