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
8 changes: 8 additions & 0 deletions .changeset/37890-phone-card-formula-amount.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@object-ui/plugin-grid': patch
---

Clause-②: no
Metadata-protocol: none

Phone list cards now put a line's total in the amount slot. The slot used to take only a column whose field type is literally `currency`, so on an order-line list the card showed the unit price (6,000.00) where the line total (10,800.00) belonged, and with no plain currency column there was no amount at all. A column now counts as money when its cell draws a number as currency: a `currency` field, a formula declaring `returnType: 'currency'`, or a summary declaring its currency (`currencyConfig`). The first such column the view shows is the amount, as before. A view that presents the field as another type, a masked column and a number-returning formula are still not promoted. Nothing changes on desktop.
25 changes: 20 additions & 5 deletions ui/packages/plugin-grid/src/ObjectGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { SchemaRenderer, isPermissionError, useDataScope, useNavigationOverlay,
// what stops a seventh site picking a convention of its own.
import { resolveGridCellRendering } from './cellRendererResolution';
import { isMaskedGridColumn } from './maskedColumn';
import { formatCurrency, formatDate, getBadgeColorClasses, getBadgeHexAppearance, FieldEditWidget, hasFieldEditWidget, hasInCellPanelEditWidget, DISCRETE_EDIT_TYPES, coerceToSafeValue, MaskedCellRenderer, withKnownObjects, RICH_TEXT_FIELD_TYPES } from '@object-ui/fields';
import { formatCurrency, formatDate, getBadgeColorClasses, getBadgeHexAppearance, FieldEditWidget, hasFieldEditWidget, hasInCellPanelEditWidget, DISCRETE_EDIT_TYPES, coerceToSafeValue, MaskedCellRenderer, withKnownObjects, RICH_TEXT_FIELD_TYPES, resolveFormulaValueType } from '@object-ui/fields';
import { useLocalization, useDisplayLocale } from '@object-ui/i18n';
// Two resolvers, two vocabularies — the repo spells the distinction into the
// NAMES (objectui#4167). `resolveInlineI18nLabel` is the spec's own
Expand Down Expand Up @@ -4429,14 +4429,29 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
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
// field type is `currency` (the column draft's resolved type, so a view
// that presents the field as another type keeps it a plain value). It
// never shares the subtitle's width: it gets a line of its own under the
// value draws as money (the column draft's resolved type, so a view that
// presents the field as another type keeps it a plain value). It never
// shares the subtitle's width: it gets a line of its own under the
// subtitle, drawn whole at the right edge, and the status chip moves to
// that line's left so the two never compete for one row. A masked amount
// is not promoted; it stays a subtitle candidate, which skips it.
//
// objectstack#37890 — money is a `currency` column, and also a `formula`
// or `summary` column whose cell draws a number as currency: a formula
// declaring `returnType: 'currency'` (an order line's total) or a summary
// declaring its currency. That is asked of `resolveFormulaValueType`, the
// rule the formula cell draws by, so the slot and the cell cannot disagree.
// The slot is chosen once for the whole list (every card keeps one shape),
// so the rule is asked how the column draws a NUMBER, not how one row's
// value happens to draw: only a numeric value is an amount.
const drawsAsMoney = (c: ObjectGridColumnDraft): boolean => {
if (c.type === 'currency') return true;
if (c.type !== 'formula' && c.type !== 'summary') return false;
const def = objectFields?.[c.accessorKey];
return def?.type === c.type && resolveFormulaValueType(def, 0) === 'currency';
};
const amountCol = bodyColumns.find(
(c) => c !== statusCol && c.type === 'currency' && !isMaskedCardColumn(c.accessorKey),
(c) => c !== statusCol && drawsAsMoney(c) && !isMaskedCardColumn(c.accessorKey),
);
const subtitleColumns = bodyColumns
.filter((c) => c !== statusCol && c !== amountCol && !titleReads.has(c.accessorKey))
Expand Down
102 changes: 102 additions & 0 deletions ui/packages/plugin-grid/src/__tests__/phoneCardAmount-34615.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,105 @@ describe('the phone list card draws its amount whole, on a line of its own (obje
expect(within(card).getByTestId('grid-card-subtitle').textContent).toBe('Wayne Enterprises · Sign by 11-27');
});
});

/**
* objectstack#37890 — the amount slot takes any column whose value draws as
* money, not only a literal `currency` column: a formula declaring
* `returnType: 'currency'` and a summary declaring its currency are amounts
* too, so an order line's card shows the line total rather than the unit
* price. Measured on the base tree with this fixture: with `unit_price` shown
* first the slot read the unit price; with `total_price` first it still read
* the unit price and the total fell into the subtitle; with no plain currency
* column there was no slot at all.
*
* ── Directions on the base tree (measured before the fix) ────────────────
* formula total shown before the unit price → slot holds the total RED
* formula total as the only money column → slot holds the total RED
* currency summary as the only money column → slot holds it RED
* control: a view that presents the formula as text → not promoted GREEN
* control: a number-returning formula → not promoted GREEN
*/
describe('the phone card amount slot takes a formula or summary that draws as money (objectstack#37890)', () => {
const LINE_OBJECT = 'order_line_37890';
const LINE_FIELDS = {
id: { type: 'text' },
name: { type: 'text', label: 'Product' },
quantity: { type: 'number', label: 'Quantity' },
unit_price: { type: 'currency', label: 'Unit Price' },
total_price: {
type: 'formula',
label: 'Total',
returnType: 'currency',
expression: 'record.quantity * record.unit_price * (1 - record.discount)',
},
line_weight: { type: 'formula', label: 'Weight', returnType: 'number', expression: 'record.quantity * 3' },
lines_total: {
type: 'summary',
label: 'Lines Total',
summaryOperations: { object: 'order_item', field: 'amount', function: 'sum' },
currencyConfig: { currencyMode: 'dynamic' },
},
};
const LINE_ROWS = [
{ id: 'l1', name: 'Admin Training Workshop', quantity: 2, unit_price: 6000, total_price: 10800, line_weight: 6, lines_total: 777 },
];

async function lineCard(columns: unknown[]) {
setWidth(390);
const dataSource = {
find: vi.fn(async () => ({ data: LINE_ROWS, total: LINE_ROWS.length, hasMore: false, pageSize: 50 })),
getObjectSchema: async (name: string) => ({ name, nameField: 'name', fields: structuredClone(LINE_FIELDS) }),
} as never;
render(
<LocalizationProvider value={{ currency: 'CNY', locale: 'en' }}>
<ActionProvider>
<SchemaRendererProvider dataSource={dataSource}>
<ObjectGrid
schema={{ type: 'object-grid', objectName: LINE_OBJECT, columns, pagination: { pageSize: 50 } } as never}
dataSource={dataSource}
/>
</SchemaRendererProvider>
</ActionProvider>
</LocalizationProvider>,
);
// The quantity column draws through the number cell once the field defs
// arrive; the money cells below read the same defs.
await waitFor(() => expect(screen.getByTestId('grid-card-subtitle').textContent).toMatch(/^2\b/));
expect(document.querySelector('table'), 'the card layout, not the table').toBeNull();
return cardOf('Admin Training Workshop');
}
const fields = (...names: string[]) => names.map((field) => ({ field }));

it('takes a currency formula shown before the unit price: the card reads the line total', async () => {
const card = await lineCard(fields('name', 'quantity', 'total_price', 'unit_price'));
expect(within(card).getByTestId('grid-card-amount').textContent).toBe('CN¥10,800.00');
expect(within(card).getByTestId('grid-card-subtitle').textContent).toBe('2 · CN¥6,000.00');
});

it('takes a currency formula that is the only money column', async () => {
const card = await lineCard(fields('name', 'quantity', 'total_price'));
expect(within(card).getByTestId('grid-card-amount').textContent).toBe('CN¥10,800.00');
expect(within(card).getByTestId('grid-card-subtitle').textContent).toBe('2');
});

it('takes a summary that declares its currency', async () => {
const card = await lineCard(fields('name', 'quantity', 'lines_total'));
expect(within(card).getByTestId('grid-card-amount').textContent).toBe('CN¥777.00');
});

it('keeps the first money column in view order: a unit price shown first stays the amount', async () => {
const card = await lineCard(fields('name', 'quantity', 'unit_price', 'total_price'));
expect(within(card).getByTestId('grid-card-amount').textContent).toBe('CN¥6,000.00');
});

it('CONTROL: a number-returning formula is not an amount', async () => {
const card = await lineCard(fields('name', 'quantity', 'line_weight'));
expect(within(card).queryByTestId('grid-card-amount-line')).toBeNull();
expect(within(card).getByTestId('grid-card-subtitle').textContent).toBe('2 · 6');
});

it('CONTROL: a view that presents the currency formula as text keeps it a plain value', async () => {
const card = await lineCard([{ field: 'name' }, { field: 'quantity' }, { field: 'total_price', type: 'text' }]);
expect(within(card).queryByTestId('grid-card-amount-line')).toBeNull();
});
});
Loading