diff --git a/.changeset/38895-phone-related-list-card-columns.md b/.changeset/38895-phone-related-list-card-columns.md new file mode 100644 index 00000000000..700a689dc10 --- /dev/null +++ b/.changeset/38895-phone-related-list-card-columns.md @@ -0,0 +1,13 @@ +--- +'@object-ui/plugin-grid': patch +'@object-ui/plugin-detail': patch +--- + +fix(plugin-grid, plugin-detail): on a phone, a related tab's cards follow the relationship's columns and leave out the reference back to the page's record + +Clause-②: yes (widening) — new optional member only: `ObjectGridComponentProps.cardFields` on `@object-ui/plugin-grid`; nothing removed or renamed +Metadata-protocol: none + +On a phone, a record page's related tab draws each row as a card. Since 19.0.0-next.3 the card body read the child object's `highlightFields`, so the relationship's own `relatedListColumns` were ignored. The card also drew the lookup back to the record the page is showing: the same value on every card, and one the list never expands, so it showed as a raw id or an unresolved reference. A product's opportunity lines, for example, showed the product on every card and never the deal. + +The card body now reads the relationship's `relatedListColumns` when they are declared, and the child's `highlightFields` otherwise. This is the same order the tab's table follows. The reference back to the page's record is left out. A lookup on the card is one the list expands, so it shows the record's name. Other lists keep the `highlightFields` card, and the desktop table is unchanged. No metadata change is needed. diff --git a/ui/packages/plugin-detail/src/RelatedList.tsx b/ui/packages/plugin-detail/src/RelatedList.tsx index ce0f20240bf..01f15f47125 100644 --- a/ui/packages/plugin-detail/src/RelatedList.tsx +++ b/ui/packages/plugin-detail/src/RelatedList.tsx @@ -3986,6 +3986,24 @@ export const RelatedList: React.FC = ({ }, [tabSummaries, patchTabConditions], ); + /** + * objectstack#38895 — the fields the enlarged grid's phone card reads + * (`cardFields`): the list this relationship declares, as this list draws + * it — its `relatedListColumns`, else the child's `highlightFields` + * (ADR-0035 §3/§4) — through the same gates as the columns, so never the + * key back to the page's own record. Without either declaration the card + * keeps the grid's shown columns, which are this list's columns already. + */ + const declaresCardList = (Array.isArray(columns) && columns.length > 0) + || (Array.isArray((objectSchema as { highlightFields?: unknown } | undefined)?.highlightFields) + && ((objectSchema as { highlightFields: unknown[] }).highlightFields.length > 0)); + const cardFieldsKey = declaresCardList + ? effectiveColumns.map((c: any) => c.accessorKey || columnIdentity(c)).filter(Boolean).join(',') + : ''; + const cardFields = React.useMemo( + () => (cardFieldsKey === '' ? undefined : cardFieldsKey.split(',')), + [cardFieldsKey], + ); const enlargedGridHost = React.useMemo(() => { if (!enlargedGrid) return undefined; const heldBefore = currentPage * (effectivePageSize || 0); @@ -3998,10 +4016,13 @@ export const RelatedList: React.FC = ({ // tab's own query), and selection with the floating bar (bulk delete). // One growing window, no pager: `data` is every row loaded so far and // `rowCount` the match total, which the footer leads with. - const cardFace = previewFace ? { cardFace: true } : {}; + const cardHost = { + ...(previewFace ? { cardFace: true } : {}), + ...(cardFields ? { cardFields } : {}), + }; if (tabList) { return { - ...cardFace, + ...cardHost, manualPagination: true, rowCount: total ?? sortedData.length, page: 1, @@ -4023,7 +4044,7 @@ export const RelatedList: React.FC = ({ }; } return { - ...cardFace, + ...cardHost, manualPagination: true, rowCount, page: currentPage + 1, @@ -4036,7 +4057,7 @@ export const RelatedList: React.FC = ({ ...(onRowDelete ? { onDelete: handleDeleteRow } : {}), ...(writeTabColumnState ? { onColumnStateChange: writeTabColumnState } : {}), }; - }, [writeTabColumnState, enlargedGrid, windowed, total, currentPage, effectivePageSize, relatedData.length, hasMore, sortedData.length, paginatedData.length, activeSort, handleTableSort, objectSchema, onRowEdit, onRowDelete, handleDeleteRow, tabList, tabRecordCountLead, pickTabSummary, tabFindParams, handleBulkDeleteRows, tabBulkDelete, previewFace]); + }, [writeTabColumnState, enlargedGrid, windowed, total, currentPage, effectivePageSize, relatedData.length, hasMore, sortedData.length, paginatedData.length, activeSort, handleTableSort, objectSchema, onRowEdit, onRowDelete, handleDeleteRow, tabList, tabRecordCountLead, pickTabSummary, tabFindParams, handleBulkDeleteRows, tabBulkDelete, previewFace, cardFields]); // objectstack#35601 — a stateful child (tasks) is grouped by its status on // the related tab: open groups first, completed last (`statusGroupsOf`). diff --git a/ui/packages/plugin-grid/src/ObjectGrid.tsx b/ui/packages/plugin-grid/src/ObjectGrid.tsx index 1af13c1a2b6..93836371341 100644 --- a/ui/packages/plugin-grid/src/ObjectGrid.tsx +++ b/ui/packages/plugin-grid/src/ObjectGrid.tsx @@ -277,6 +277,18 @@ export interface ObjectGridComponentProps extends ObjectGridExternalPaginationPr * prop, never authored metadata. */ cardFace?: boolean; + /** + * [objectstack#38895] The fields the phone card's BODY reads, in priority + * order, in place of the object's `highlightFields`: its amount is the first + * that draws as money and its subtitle the next three, as for + * `highlightFields`. A related list passes the list its relationship + * declares (ADR-0035 §3/§4: the relationship's `relatedListColumns`, else the + * child's `highlightFields`), never the lookup back to the page's own + * record, which is the same value on every card. Absent: the object's + * `highlightFields`, else the shown columns. Only the card reads it; the + * table keeps its columns. A HOST prop, never authored metadata. + */ + cardFields?: readonly string[]; /** * [objectstack#35702] The host's own wording of the record count that leads * the footer row (the list's 「42 records」, plural forms included). Absent ⇒ @@ -979,6 +991,7 @@ export const ObjectGrid: React.FC = ({ onColumnSummaryChange, residentSummaryRow, cardFace: hostCardFace, + cardFields: hostCardFields, summaryRowLead, onColumnSummaryPick, summaryPickFields, @@ -4446,8 +4459,12 @@ export const ObjectGrid: React.FC = ({ // field-level security, so a field the viewer may not read is left out). // The fetches that feed this grid ask for those fields beside the name // field. An object that declares none keeps the shown columns' order. - const highlightNames = Array.isArray((objectSchema as any)?.highlightFields) - ? [...new Set(((objectSchema as any).highlightFields as unknown[]) + // objectstack#38895 — a host's `cardFields` (a related list: the list its + // relationship declares, minus the key back to the page's record) stands + // in for `highlightFields` and is read the same way. + const declaredCardFields: unknown = hostCardFields ?? (objectSchema as any)?.highlightFields; + const highlightNames = Array.isArray(declaredCardFields) + ? [...new Set((declaredCardFields as unknown[]) .filter((n): n is string => typeof n === 'string' && !!objectFields?.[n]))] : []; const followsHighlights = highlightNames.length > 0; diff --git a/ui/packages/plugin-grid/src/__tests__/relatedTabPhoneCard-38895.test.tsx b/ui/packages/plugin-grid/src/__tests__/relatedTabPhoneCard-38895.test.tsx new file mode 100644 index 00000000000..d3cdb6e47b8 --- /dev/null +++ b/ui/packages/plugin-grid/src/__tests__/relatedTabPhoneCard-38895.test.tsx @@ -0,0 +1,209 @@ +/** + * 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#38895 — on a phone, a record page's related tab draws each row + * as the list card (objectstack#28484), and the card's BODY follows the + * RELATIONSHIP's list, not the child object's `highlightFields` + * (objectstack#37891, which stays for every other list): the relationship's + * `relatedListColumns` when it declares them, else the child's + * `highlightFields` — the override chain of ADR-0035 §3/§4 the tab's table + * already follows. The lookup back to the page's own record is never a part: + * it is the same value on every card, and the list never expands it, so it + * could only draw a raw id. + * + * Driven through the real chain the tab draws: `RelatedList` (enlarged, a + * list of the parent record) → `object-grid` → its card renderer. The fixture + * is HotCRM's opportunity line on a product's page: `highlightFields` leads + * with the product (right for the line's own page and the deal's list), while + * the product relationship's `relatedListColumns` leads with the deal. + * + * ── Directions on the base tree (measured by reverting the fix) ────────── + * declared columns: the deal is a part, by its name RED + * declared columns: no part is the product's id RED + * no declared columns: highlightFields minus the product RED + * control: the tab's table at a desktop width keeps its columns GREEN + */ +import { describe, it, expect, vi, beforeAll, afterEach } from 'vitest'; +import { render, cleanup, waitFor, within } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import React from 'react'; + +vi.mock('@object-ui/permissions', async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + usePermissions: () => ({ isLoaded: false, checkField: () => true, getObjectApiOperations: () => undefined, can: () => true }), + }; +}); + +// Registers `object-grid`, the enlarged related tab's face. +import '../index'; +import { registerAllFields } from '@object-ui/fields'; +import { LocalizationProvider } from '@object-ui/i18n'; +import { ActionProvider, FilterScopeProvider, SchemaRendererProvider } from '@object-ui/react'; +import { RelatedList } from '../../../plugin-detail/src/RelatedList'; +import { RelatedListEnlargedContext } from '../../../plugin-detail/src/relatedListCompact'; +import { resetRelatedTabConditionsForTests } from '../../../plugin-detail/src/relatedTabConditions'; + +registerAllFields(); + +const ORIGINAL_INNER_WIDTH = window.innerWidth; +function setWidth(px: number) { + Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: px }); +} + +beforeAll(() => { + if (!Element.prototype.scrollIntoView) Element.prototype.scrollIntoView = vi.fn() as any; +}); + +afterEach(() => { + cleanup(); + setWidth(ORIGINAL_INNER_WIDTH); + resetRelatedTabConditionsForTests(); +}); + +const LINE_OBJECT = 'crm_opportunity_line_item_38895'; +const LINE_FIELDS = { + id: { type: 'text' }, + product_title: { type: 'text', label: 'Product Title' }, + crm_product: { type: 'lookup', reference: 'crm_product', label: 'Product' }, + crm_opportunity: { type: 'lookup', reference: 'crm_opportunity', label: 'Opportunity' }, + quantity: { type: 'number', label: 'Quantity', scale: 0 }, + unit_price: { type: 'currency', label: 'Sales Price' }, + total_price: { + type: 'formula', + label: 'Total', + returnType: 'currency', + expression: 'record.quantity * record.unit_price', + }, +}; +/** HotCRM's line: the product leads, for the line's own page and the deal's list. */ +const HIGHLIGHTS = ['crm_product', 'quantity', 'unit_price', 'total_price']; +/** HotCRM's `relatedListColumns` on the line's product relationship: the deal leads. */ +const PRODUCT_RELATED_COLUMNS = ['crm_opportunity', 'quantity', 'unit_price', 'total_price']; + +const PRODUCT_ID = 'TcoGX1WJ77TN-SKX'; +const DEALS: Record = { + o1: { id: 'o1', name: 'Globex Line Expansion' }, + o2: { id: 'o2', name: 'Lattice Student Success Platform' }, +}; +const LINES = [ + { id: 'l1', product_title: 'AI Agent Seat (Annual)', crm_product: PRODUCT_ID, crm_opportunity: 'o1', quantity: 8, unit_price: 1000, total_price: 8000 }, + { id: 'l2', product_title: 'AI Agent Seat (Annual)', crm_product: PRODUCT_ID, crm_opportunity: 'o2', quantity: 5, unit_price: 1000, total_price: 5000 }, +]; + +/** + * A backend that honours `$expand`, as the ObjectStack read path does. It + * holds the lines alone, so a lookup the list did not expand cannot be + * resolved by a second read and draws whatever the row carries. + */ +function makeDataSource() { + return { + find: vi.fn(async (object: string, params: { $expand?: string[] } = {}) => { + if (object !== LINE_OBJECT) return { data: [], total: 0 }; + const expand = new Set(params.$expand ?? []); + return { + data: LINES.map((line) => ({ + ...line, + ...(expand.has('crm_opportunity') ? { crm_opportunity: DEALS[line.crm_opportunity] } : {}), + ...(expand.has('crm_product') ? { crm_product: { id: PRODUCT_ID, name: 'AI Agent Seat (Annual)' } } : {}), + })), + total: LINES.length, + }; + }), + getObjectSchema: vi.fn(async (name: string) => ({ + name, + nameField: 'product_title', + highlightFields: HIGHLIGHTS, + fields: structuredClone(LINE_FIELDS), + })), + } as any; +} + +/** The product page's 「Opportunity Line Items」 tab. */ +async function renderProductLinesTab(opts: { width: number; columns?: string[] }) { + setWidth(opts.width); + const ds = makeDataSource(); + const utils = render( + + + + + + + + + + + , + ); + return { ...utils, ds }; +} + +const cards = () => Array.from(document.querySelectorAll('[data-testid="grid-mobile-card"]')); +const parts = (card: HTMLElement) => + within(card).queryAllByTestId('grid-card-subtitle-part').map((p) => p.textContent?.trim()); + +async function phoneCards(columns?: string[]) { + const g = await renderProductLinesTab({ width: 390, ...(columns ? { columns } : {}) }); + await waitFor(() => expect(cards()).toHaveLength(2), { timeout: 5000 }); + // The money cells read the field defs, so a dollar amount proves they landed. + await waitFor(() => expect(document.body.textContent).toContain('$')); + expect(document.querySelector('table'), 'the card layout, not the table').toBeNull(); + return { ...g, first: cards()[0], second: cards()[1] }; +} + +describe('the phone card of a related tab follows the relationship\'s list (objectstack#38895)', () => { + it('with relatedListColumns declared, each card names its deal and its quantity, never the page\'s product', async () => { + const { first, second, ds } = await phoneCards(PRODUCT_RELATED_COLUMNS); + // The list expands the deal it lists and never the key back to the page. + const lineReads = ds.find.mock.calls.filter((call: unknown[]) => call[0] === LINE_OBJECT); + const params = lineReads.at(-1)?.[1] as { $expand?: string[] }; + expect(params.$expand).toContain('crm_opportunity'); + expect(params.$expand ?? []).not.toContain('crm_product'); + + expect(parts(first)).toEqual(['Globex Line Expansion', 'Quantity 8', 'Total $8,000.00']); + expect(parts(second)).toEqual(['Lattice Student Success Platform', 'Quantity 5', 'Total $5,000.00']); + // The amount is the first money field of the relationship's list. + expect(within(first).getByTestId('grid-card-amount').textContent).toBe('$1,000.00'); + for (const card of [first, second]) { + expect(card.textContent, 'no raw id of the page\'s own record').not.toContain(PRODUCT_ID); + } + }); + + it('with no relatedListColumns, the card falls back to the child\'s highlightFields, minus the key back to the page', async () => { + const { first, second } = await phoneCards(); + expect(parts(first)).toEqual(['Quantity 8', 'Total $8,000.00']); + expect(within(first).getByTestId('grid-card-amount').textContent).toBe('$1,000.00'); + for (const card of [first, second]) { + expect(card.textContent, 'no raw id of the page\'s own record').not.toContain(PRODUCT_ID); + } + }); + + it('CONTROL: the same tab at a desktop width is the table of the relationship\'s columns', async () => { + await renderProductLinesTab({ width: 1440, columns: PRODUCT_RELATED_COLUMNS }); + await waitFor(() => expect(document.querySelector('table')).not.toBeNull(), { timeout: 5000 }); + await waitFor(() => expect(document.body.textContent).toContain('Globex Line Expansion')); + expect(cards()).toHaveLength(0); + const headers = Array.from(document.querySelectorAll('thead th')).map((th) => th.textContent?.trim() ?? ''); + for (const label of ['Opportunity', 'Quantity', 'Sales Price', 'Total']) { + expect(headers.some((h) => h.includes(label)), `a ${label} column`).toBe(true); + } + expect(headers.some((h) => h === 'Product'), 'no column for the key back to the page').toBe(false); + }); +});