diff --git a/.changeset/37010-phone-card-formula-title-subtitle.md b/.changeset/37010-phone-card-formula-title-subtitle.md new file mode 100644 index 00000000000..1968ca1789e --- /dev/null +++ b/.changeset/37010-phone-card-formula-title-subtitle.md @@ -0,0 +1,10 @@ +--- +'@object-ui/plugin-grid': patch +--- + +fix(plugin-grid): a phone list card never repeats a formula title in its subtitle + +Clause-②: no +Metadata-protocol: none + +On a phone, a list card whose object names its records with a `formula` name field (a case's `display_title` = `case_number - subject`) showed the title's parts again in the subtitle when the view showed them as its leading columns, so the subtitle had no room for anything else. The subtitle now skips every shown column whose field the name field's formula reads (a read through a lookup, `record.account.name`, skips the lookup's column) and takes the next shown columns instead: `CASE-00037 - Demo case 29 — critical problem` over `紧急 · Polar Cargo`. A name field that is not a formula keeps the subtitle it had. diff --git a/ui/packages/plugin-grid/src/ObjectGrid.tsx b/ui/packages/plugin-grid/src/ObjectGrid.tsx index a14d70792ec..5fbd40d54cf 100644 --- a/ui/packages/plugin-grid/src/ObjectGrid.tsx +++ b/ui/packages/plugin-grid/src/ObjectGrid.tsx @@ -71,7 +71,7 @@ import { AlertDialogHeader, AlertDialogTitle, } from '@object-ui/components'; -import { captureUpdateUndoData, resolveConditionalFormatting, declaredNameField, buildExportFileName, resolveAffordance, resolveAffordanceGrant, resolveFieldAffordance, isObjectExportOffered, type SchemaLike, isExpandableFieldType, isUnmaterializedFieldType, readObjectSortability, isPlatformSortableField, filterPlatformSortableSort, filterRefusalSubject, FilterOperatorError, resolveRecordSourceConfig, resolveRecordSourceObjectName, partitionRowsByPredicate, csvBlob, readActionPayload } from '@object-ui/core'; +import { captureUpdateUndoData, resolveConditionalFormatting, declaredNameField, collectPredicateFieldRefs, buildExportFileName, resolveAffordance, resolveAffordanceGrant, resolveFieldAffordance, isObjectExportOffered, type SchemaLike, isExpandableFieldType, isUnmaterializedFieldType, readObjectSortability, isPlatformSortableField, filterPlatformSortableSort, filterRefusalSubject, FilterOperatorError, resolveRecordSourceConfig, resolveRecordSourceObjectName, partitionRowsByPredicate, csvBlob, readActionPayload } from '@object-ui/core'; import { usePermissions } from '@object-ui/permissions'; import { RECORD_OVERLAY_DEFAULT_WIDTH, @@ -4406,6 +4406,21 @@ export const ObjectGrid: React.FC = ({ // that titles the card, so a first column the name field displaced (the // Owner above) reads as an ordinary body row. const bodyColumns = displayColumns.filter((c) => c !== titleCol); + // objectstack#37010 — a name field that is a FORMULA titles the card with + // the fields it reads (`case_number - subject`), and a view shows those + // parts as columns of their own, usually its leading ones, since a table + // has no use for the concatenation. So the subtitle also skips every + // shown column whose field the formula reads: it never repeats the + // title's parts and takes the next shown columns instead. The reads are + // the formula's `record.` references as `collectPredicateFieldRefs` + // names them, the reader this grid's projection names a CEL expression's + // reads with; a read through a lookup (`record.account.name`) names the + // lookup, so the lookup's column is skipped too. A name field that is not + // a formula reads nothing here. + const nameFieldDef = nameFieldKey !== undefined ? objectFields?.[nameFieldKey] : undefined; + const titleReads = new Set( + nameFieldDef?.type === 'formula' ? collectPredicateFieldRefs([nameFieldDef.expression]) : [], + ); // objectui#10583 — a MASKED column (the rule that stamps `TableColumn.masked`) // is drawn only through its own `cell`, which draws the mask. The branches @@ -4425,7 +4440,8 @@ export const ObjectGrid: React.FC = ({ // read), when a column shows it, drawn through that column's own cell as // the desktop row draws it. The subtitle joins the first two other shown // columns' display values, each through its own cell, and skips a masked - // or empty value rather than drawing it. + // or empty value rather than drawing it, and a column the title's formula + // reads (`titleReads` above). const statusKey = detectStatusField(objectSchema); const statusCol = statusKey !== null ? bodyColumns.find((c) => c.accessorKey === statusKey) : undefined; // objectstack#34615 — the card's AMOUNT is the first shown body column whose @@ -4438,7 +4454,9 @@ export const ObjectGrid: React.FC = ({ const amountCol = bodyColumns.find( (c) => c !== statusCol && c.type === 'currency' && !isMaskedCardColumn(c.accessorKey), ); - const subtitleColumns = bodyColumns.filter((c) => c !== statusCol && c !== amountCol).slice(0, 2); + const subtitleColumns = bodyColumns + .filter((c) => c !== statusCol && c !== amountCol && !titleReads.has(c.accessorKey)) + .slice(0, 2); const isEmptyCardValue = (value: unknown) => value == null || value === ''; const cardCell = (col: ObjectGridColumnDraft, row: any): React.ReactNode => { const value = row[col.accessorKey]; diff --git a/ui/packages/plugin-grid/src/__tests__/phoneCardFormulaTitle-37010.test.tsx b/ui/packages/plugin-grid/src/__tests__/phoneCardFormulaTitle-37010.test.tsx new file mode 100644 index 00000000000..ee3a4afe7d1 --- /dev/null +++ b/ui/packages/plugin-grid/src/__tests__/phoneCardFormulaTitle-37010.test.tsx @@ -0,0 +1,151 @@ +/** + * 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#37010 — on a phone, a list card whose object names its records + * with a FORMULA name field never repeats the title in its subtitle. The + * formula (`display_title` = `case_number - subject`) titles the card, and the + * view shows its parts as its own leading columns. The subtitle skips every + * shown column whose field the formula reads and takes the next shown columns + * instead. A name field that is not a formula keeps today's subtitle. + * + * Driven through the real card renderer (`ObjectGrid` at 375 px), with the + * object schema served by the data source as a list page receives it. + * + * ── Directions on the base tree (predicted before the first run) ───────── + * formula title, parts as leading columns: subtitle is the next two RED + * formula title, formula column shown too: subtitle skips the parts RED + * control: a text name field keeps today's subtitle GREEN + */ +import { describe, it, expect, vi, afterEach, beforeAll } from 'vitest'; +import { render, cleanup, waitFor, screen, within } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import React from 'react'; + +import { ObjectGrid } from '../ObjectGrid'; +import { LocalizationProvider } from '@object-ui/i18n'; +import { ActionProvider, SchemaRendererProvider } from '@object-ui/react'; +import { registerAllFields } from '@object-ui/fields'; + +beforeAll(() => { + registerAllFields(); + if (!Element.prototype.scrollIntoView) { + Element.prototype.scrollIntoView = vi.fn() as unknown as Element['scrollIntoView']; + } +}); + +const ORIGINAL_INNER_WIDTH = window.innerWidth; + +function setWidth(px: number) { + Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: px }); +} + +afterEach(() => { + cleanup(); + setWidth(ORIGINAL_INNER_WIDTH); +}); + +const OBJECT = 'case_37010'; + +const FIELDS = { + id: { type: 'text' }, + case_number: { type: 'text', label: 'Case Number' }, + subject: { type: 'text', label: 'Subject' }, + status: { + type: 'select', + label: 'Status', + options: [ + { value: 'open', label: 'Open' }, + { value: 'closed', label: 'Closed' }, + ], + }, + priority: { + type: 'select', + label: 'Priority', + options: [ + { value: 'critical', label: '紧急' }, + { value: 'low', label: '低' }, + ], + }, + account: { type: 'text', label: 'Account' }, + display_title: { + type: 'formula', + label: 'Title', + returnType: 'text', + expression: { dialect: 'cel', source: "record.case_number + ' - ' + record.subject" }, + }, +}; + +const TITLE = 'CASE-00037 - Demo case 29 — critical problem'; + +const ROWS = [ + { + id: 'c37', + case_number: 'CASE-00037', + subject: 'Demo case 29 — critical problem', + status: 'open', + priority: 'critical', + account: 'Polar Cargo', + display_title: TITLE, + }, +]; + +/** The formula's parts lead the view, as a real case list shows them; the formula column itself is not shown. */ +const PART_COLUMNS = ['case_number', 'subject', 'status', 'priority', 'account'].map((field) => ({ field })); + +function renderGrid(nameField: string, columns: unknown[]) { + const dataSource = { + find: vi.fn(async () => ({ data: ROWS, total: ROWS.length, hasMore: false, pageSize: 50 })), + getObjectSchema: async (name: string) => ({ name, nameField, fields: structuredClone(FIELDS) }), + } as never; + return render( + + + + + + + , + ); +} + +async function phoneCard(nameField: string, columns: unknown[] = PART_COLUMNS) { + setWidth(375); + renderGrid(nameField, columns); + // The status chip reads its label off the object schema, so it proves the + // field defs (and with them the name field's formula) have arrived. + await waitFor(() => expect(screen.getByText('Open')).toBeInTheDocument()); + expect(document.querySelector('table'), 'the card layout, not the table').toBeNull(); + const cards = [...document.querySelectorAll('[data-testid="grid-mobile-card"]')] as HTMLElement[]; + expect(cards).toHaveLength(1); + return cards[0]; +} + +describe('the phone list card subtitle never repeats a formula title (objectstack#37010)', () => { + it('a formula name field over the two leading columns: the subtitle is the next two shown columns', async () => { + const card = await phoneCard('display_title'); + expect(within(card).getByTestId('grid-card-title').textContent).toBe(TITLE); + expect(within(card).getByTestId('grid-card-subtitle').textContent).toBe('紧急 · Polar Cargo'); + }); + + it('the formula column shown as well: the subtitle still skips the columns the formula reads', async () => { + const columns = ['display_title', 'case_number', 'subject', 'status', 'priority', 'account'].map((field) => ({ field })); + const card = await phoneCard('display_title', columns); + expect(within(card).getByTestId('grid-card-title').textContent).toBe(TITLE); + expect(within(card).getByTestId('grid-card-subtitle').textContent).toBe('紧急 · Polar Cargo'); + }); + + it('CONTROL: a name field that is not a formula keeps the first two other shown columns', async () => { + const card = await phoneCard('subject'); + expect(within(card).getByTestId('grid-card-title').textContent).toBe('Demo case 29 — critical problem'); + expect(within(card).getByTestId('grid-card-subtitle').textContent).toBe('CASE-00037 · 紧急'); + }); +});