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
12 changes: 12 additions & 0 deletions .changeset/27430-metric-tile-description-caption.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@object-ui/plugin-dashboard': patch
---

fix(plugin-dashboard): a dataset-bound `metric` tile captions its value with the widget's `description`, falling back to the measure label

Clause-②: no
Metadata-protocol: none

A `metric` dashboard tile drew the measure's label (for example "Total Amount") under its value, and the widget's `description`, which says what the number counts, only in the title's tooltip. When a widget declares a `description`, the tile now draws it as the caption under the value, in the measure label's place. A widget with no description (or an empty one) keeps the measure label as its caption. The measure label still rides in the title's tooltip, under the description, so it stays reachable at every tile width.

Both dashboard surfaces (`DashboardRenderer` and `DashboardGridLayout`) show the same caption. The tile keeps its two lines of text, and no new metadata key is read: `description` is the existing widget-level key of `DashboardWidgetSchema`.
7 changes: 3 additions & 4 deletions ui/packages/plugin-dashboard/src/DashboardRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1093,10 +1093,9 @@ const DashboardRendererInner = forwardRef<HTMLDivElement, DashboardRendererProps
// objectui#12528 — a dataset-bound METRIC tile draws no card header:
// the header (title, then a description paragraph under a rule) and
// the tile (value, then the measure label) stacked four lines for one
// number. The tile draws the title and the measure label on one line
// above the value instead, and the description is the title's tooltip.
// The spec declares no widget key to show it inline, so it shows
// nowhere else. Every other widget keeps the frame as it was.
// number. The tile draws the title above the value instead, and the
// description is the caption under it in place of the measure label
// (objectstack#27430). Every other widget keeps the frame as it was.
const datasetMetric = datasetBound && entryType === 'metric';
const resolvedTitle = tWidgetTitle(widget);
const resolvedDescription = tWidgetDescription(widget);
Expand Down
35 changes: 22 additions & 13 deletions ui/packages/plugin-dashboard/src/DatasetWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -629,16 +629,19 @@ export { chartConfigPresentation } from '@object-ui/core';
* bag: the metric sub-caption is retired at both ends (objectui#11389, ruling
* C, after objectstack's half in `@objectstack/spec` 17.7.0). A widget keeps
* one authored description, `widget.description`, which the dashboard surface
* draws as the card-header subtitle. If a caption under a metric's value
* returns, it returns as a declared widget-level key outside the options bag,
* never as a key inside it. The options census in `@objectstack/sdui-parser`
* draws as the card-header subtitle, or, for a metric tile, hands in through
* `heading` to draw as the caption under the value. That is the declared
* widget-level key outside the options bag; a caption is never read from a key
* inside it. The options census in `@objectstack/sdui-parser`
* reads this file's text, comments included, which is why this note does not
* spell the bag's member access.
*/
/**
* [objectui#12528] The heading a dashboard surface hands a dataset-bound
* metric tile instead of drawing its own card header: the widget's resolved
* title, and its resolved description, which becomes the title's tooltip.
* title, and its resolved description, which the tile draws as the caption
* under the value in place of the measure label (objectstack#27430) and
* carries in the title's tooltip.
*/
export interface DatasetMetricHeading {
title: string;
Expand All @@ -647,8 +650,10 @@ export interface DatasetMetricHeading {

/**
* The heading line of a metric tile: the title alone (objectui#12623). It owns
* the line, up to two lines; the measure label is drawn under the value, and
* rides in this tooltip so a tile too narrow to draw it still carries it.
* the line, up to two lines; the caption is drawn under the value, and the
* description and the measure label ride in this tooltip, so a tile too narrow
* to draw the caption, or one whose caption is the description, still carries
* the measure label.
*/
function MetricHeadingRow({ heading, label }: { heading: DatasetMetricHeading; label?: string }) {
return (
Expand Down Expand Up @@ -1379,22 +1384,26 @@ export function DatasetWidget({ widget, dataSource, heading, onForbidden }: {
// (objectui#7293), fed by the server's `subCaption` overlay and a client
// bundle limb; both ends are retired, and the spec never declared the key.
// An authored one now draws the parser's `unconsumed-widget-option` warning
// instead of a caption node. The card-header subtitle, `widget.description`,
// is the surface's to draw, not this tile's.
// instead of a caption node. The widget-level `widget.description` is the
// surface's to resolve: handed in through `heading`, it is the caption
// under the value, in place of the measure label, which then rides only in
// the title's tooltip (objectstack#27430).
const caption = heading?.description || measureLabel;
return (
// Positioned only while a re-read is in flight, for the refresh bar: the
// idle tile's markup is pinned byte-for-byte (`DatasetWidget.colorVariant`,
// `.subCaption`, `.unrenderedMeasures-8894` tests), and it moves by nothing.
<div className={cn('flex h-full w-full flex-col items-start justify-center gap-1 p-2', heading && cn('min-w-0', METRIC_TILE_CONTAINER_CLASS), state.refreshing && 'relative')}>
{refreshBar}
{/* objectui#12623 — with a heading, the title owns the first line, the
value never wraps, and the measure label is a line under it (the
shared rule in `./metricTile`). Without one, the tile's markup is
unchanged: the label is then the only text naming the value. */}
value never wraps, and the caption (the description, else the
measure label) is a line under it (the shared rule in
`./metricTile`). Without one, the tile's markup is unchanged: the
label is then the only text naming the value. */}
{heading && <MetricHeadingRow heading={heading} label={measureLabel} />}
<span className={cn(heading ? cn('font-semibold', METRIC_TILE_VALUE_CLASS) : 'text-2xl font-semibold tabular-nums', accentClass)}>{formatMeasure(value, f?.format, f?.currency, f?.percentScale, displayLocale)}</span>
{heading && measureLabel && (
<span className={METRIC_TILE_MEASURE_LABEL_CLASS} data-testid="dataset-metric-label">{measureLabel}</span>
{heading && caption && (
<span className={METRIC_TILE_MEASURE_LABEL_CLASS} data-testid="dataset-metric-label">{caption}</span>
)}
{delta && (
<div className="mt-0.5 flex min-w-0 flex-wrap items-center gap-x-2 gap-y-0.5 text-xs text-muted-foreground" data-testid="dataset-compare-trend">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
* that is wide enough; the width rules themselves are
* `metricTileLayout-12623.test.tsx`'s.
*
* objectstack#27430 draws an authored description as the caption under the
* value in place of the measure label: the first pin renders a metric that
* declares none, and the second asserts the description as that caption.
*
* CONTROL: a non-metric dataset widget (a table) keeps the card header, and
* its description paragraph, exactly as before.
*/
Expand Down Expand Up @@ -64,6 +68,9 @@ const METRIC = {
values: ['total_amount'],
};

// The same tile with no description: its caption is the measure label.
const METRIC_UNDESCRIBED = { ...METRIC, description: undefined };

function renderDashboard(...widgets: Record<string, unknown>[]) {
return render(
<I18nProvider config={{ defaultLanguage: 'en', detectBrowserLanguage: false }}>
Expand All @@ -74,7 +81,7 @@ function renderDashboard(...widgets: Record<string, unknown>[]) {

describe('DashboardRenderer — a dataset-bound metric tile on two lines (objectui#12528)', () => {
it('draws the title alone on the first line, then the value, then the measure label (objectui#12623)', async () => {
const { container } = renderDashboard(METRIC);
const { container } = renderDashboard(METRIC_UNDESCRIBED);
// The answered tile, not the loading one: a loading tile draws the same
// heading line, and only the answer names the measure label.
const value = await screen.findByText('1964500');
Expand All @@ -93,16 +100,20 @@ describe('DashboardRenderer — a dataset-bound metric tile on two lines (object
expect(screen.getAllByText('Total Pipeline')).toHaveLength(1);
});

it('draws no description paragraph, and carries the description (then the measure label) as the title tooltip', async () => {
it('draws no description paragraph: the description is the caption under the value, and leads the title tooltip (objectstack#27430)', async () => {
const { container } = renderDashboard(METRIC);
await screen.findByText('1964500');
const value = await screen.findByText('1964500');
const title = screen.getByTestId('dataset-metric-title');

// objectui#12623 — the measure label rides in the tooltip on its own line,
// the one place it survives a tile too narrow to draw it.
// the one place it survives a tile too narrow to draw it, and the one
// place it survives under a description caption.
expect(title).toHaveAttribute('title', 'Sum of open opportunity value\nTotal Amount');
expect(container.querySelector('p')).toBeNull();
expect(container.textContent).not.toContain('Sum of open opportunity value');
expect(value.nextElementSibling).toBe(screen.getByTestId('dataset-metric-label'));
expect(screen.getByTestId('dataset-metric-label')).toHaveTextContent('Sum of open opportunity value');
expect(container.textContent?.match(/Sum of open opportunity value/g)).toHaveLength(1);
expect(container.textContent).not.toContain('Total Amount');
});

it('CONTROL: a non-metric dataset widget keeps its card header and description paragraph', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/**
* 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#27430 row 2 — a dataset-bound `metric` tile captions its value
* with the widget's `description` when one is authored, and with the measure
* label otherwise.
*
* The tile drew the measure label ("Total Amount") under every value, and the
* widget's `description` (the spec's `DashboardWidget.description`, "Widget
* description text below the header"), which says what the number counts,
* only in the title's tooltip. Now the description is the caption, in the
* measure label's place: the tile keeps its two lines of text, and the
* measure label still rides in the title's tooltip, under the description.
*
* Pins, on both dashboard surfaces (`DashboardRenderer`, `DashboardGridLayout`):
* - a described metric: the caption under the value is the description, drawn
* once; the measure label is drawn nowhere but the tooltip;
* - an undescribed metric (and one whose description is empty): the caption is
* the measure label, as before;
* - an inline per-locale description map: the caption follows the UI language;
* - CONTROL: a dataset-bound table draws no metric caption (`DashboardRenderer`'s
* card-header description paragraph is pinned in
* `DashboardRenderer.metricTileDensity-12528.test.tsx`).
*
* No `dist/` is involved: the root `vitest.config.mts` aliases every
* `@object-ui/*` specifier to that package's `src/`, and the subjects are
* imported relatively.
*/

import * as React from 'react';
import { describe, it, expect, vi, afterEach } from 'vitest';
import { render, screen, cleanup } from '@testing-library/react';
import { I18nProvider } from '@object-ui/i18n';
import type { DashboardComponentSchema } from '@object-ui/types';
// Module scope, never inside a hook (AGENTS.md 测试纪律): both surfaces resolve
// non-dataset nodes through the registries these barrels populate.
import '@object-ui/components';
import { DashboardRenderer } from '../DashboardRenderer';
import { DashboardGridLayout } from '../DashboardGridLayout';

afterEach(cleanup);

const dashboard = (widgets: Record<string, unknown>[]): DashboardComponentSchema =>
({ type: 'dashboard', name: 'sales', widgets }) as unknown as DashboardComponentSchema;

const source = () => ({
queryDataset: vi.fn(async () => ({
rows: [{ total_amount: 1964500, stage: 'won' }],
fields: [
{ name: 'total_amount', label: 'Total Amount', type: 'number' },
{ name: 'stage', label: 'Stage', type: 'text' },
],
})),
});

const METRIC = {
id: 'total_pipeline_value',
type: 'metric',
title: 'Total Pipeline',
description: 'Open opportunities, all owners',
dataset: 'opportunity_metrics',
values: ['total_amount'],
};

type Surface = (schema: DashboardComponentSchema, dataSource: unknown) => React.ReactElement;

const SURFACES: Array<[string, Surface]> = [
['DashboardRenderer', (schema, dataSource) => <DashboardRenderer schema={schema} dataSource={dataSource} />],
['DashboardGridLayout', (schema, dataSource) => <DashboardGridLayout schema={schema} dataSource={dataSource} />],
];

const renderOn = (surface: Surface, schema: DashboardComponentSchema, language = 'en') =>
render(
<I18nProvider config={{ defaultLanguage: language, detectBrowserLanguage: false }}>
{surface(schema, source())}
</I18nProvider>,
);

describe.each(SURFACES)('%s — the metric tile caption (objectstack#27430)', (_name, surface) => {
it('draws an authored description as the caption under the value, once, in place of the measure label', async () => {
const { container } = renderOn(surface, dashboard([METRIC]));
const value = await screen.findByText('1964500');
const caption = screen.getByTestId('dataset-metric-label');

expect(value.nextElementSibling).toBe(caption);
expect(caption).toHaveTextContent('Open opportunities, all owners');
expect(container.textContent?.match(/Open opportunities, all owners/g)).toHaveLength(1);
// The measure label is no longer drawn; the title's tooltip still carries
// it, under the description.
expect(container.textContent).not.toContain('Total Amount');
expect(screen.getByTestId('dataset-metric-title')).toHaveAttribute(
'title',
'Open opportunities, all owners\nTotal Amount',
);
});

it.each([
['declares no description', undefined],
['declares an empty description', ''],
])('falls back to the measure label when the widget %s', async (_case, description) => {
renderOn(surface, dashboard([{ ...METRIC, description }]));
const value = await screen.findByText('1964500');

expect(value.nextElementSibling).toBe(screen.getByTestId('dataset-metric-label'));
expect(screen.getByTestId('dataset-metric-label')).toHaveTextContent('Total Amount');
});

it('follows the UI language for an inline per-locale description', async () => {
renderOn(
surface,
dashboard([{ ...METRIC, description: { en: 'Open opportunities, all owners', 'zh-CN': '所有负责人的未结商机' } }]),
'zh-CN',
);
await screen.findByText('1964500');

expect(screen.getByTestId('dataset-metric-label')).toHaveTextContent('所有负责人的未结商机');
});

it('CONTROL: a dataset-bound table draws no metric caption', async () => {
const { container } = renderOn(surface, dashboard([{
id: 'by_stage',
type: 'table',
title: 'Pipeline by Stage',
description: 'Open opportunity value at each sales stage',
dataset: 'opportunity_metrics',
dimensions: ['stage'],
values: ['total_amount'],
}]));

// The answered table: its cell draws the value.
await screen.findByText('Pipeline by Stage');
await screen.findAllByText(/1,?964,?500/);
expect(container.querySelector('[data-testid="dataset-metric-label"]')).toBeNull();
expect(container.querySelector('[data-testid="dataset-metric-heading"]')).toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@
* surfaces instead of the shared line; the width rules are
* `metricTileLayout-12623.test.tsx`'s.
*
* objectstack#27430 draws an authored description as the caption under the
* value in place of the measure label, so the first pin below renders a metric
* that declares none (its caption is the measure label), and the second keeps
* only the tooltip; the caption itself is
* `DashboardSurfaces.metricTileCaption-27430.test.tsx`'s.
*
* Pins:
* - both surfaces: the metric draws the title alone before the value and the
* measure label after it, the label once, the title once, the description
* measure label after it, the label once, the title once; the description
* then the label as the title's tooltip;
* - CONTROL, both surfaces: a dataset-bound table and chart keep the card
* header and draw no metric heading;
Expand Down Expand Up @@ -80,6 +86,9 @@ const METRIC = {
values: ['total_amount'],
};

// The same tile with no description: its caption is the measure label.
const METRIC_UNDESCRIBED = { ...METRIC, description: undefined };

const TABLE = {
id: 'by_stage_table',
type: 'table',
Expand Down Expand Up @@ -114,7 +123,7 @@ const renderOn = (surface: Surface, schema: DashboardComponentSchema) =>

describe.each(SURFACES)('%s — a dataset-bound metric tile on two lines (objectui#12573)', (_name, surface) => {
it('draws the title alone above the value and the measure label under it, each once (objectui#12623)', async () => {
const { container } = renderOn(surface, dashboard([METRIC]));
const { container } = renderOn(surface, dashboard([METRIC_UNDESCRIBED]));
// The answered tile: only the answer names the measure label.
const value = await screen.findByText('1964500');
const heading = screen.getByTestId('dataset-metric-heading');
Expand All @@ -129,12 +138,11 @@ describe.each(SURFACES)('%s — a dataset-bound metric tile on two lines (object
expect(screen.getAllByText('Total Pipeline')).toHaveLength(1);
});

it('carries the description, then the measure label, as the title tooltip, the description drawn nowhere else', async () => {
const { container } = renderOn(surface, dashboard([METRIC]));
it('carries the description, then the measure label, as the title tooltip', async () => {
renderOn(surface, dashboard([METRIC]));
await screen.findByText('1964500');

expect(screen.getByTestId('dataset-metric-title')).toHaveAttribute('title', 'Sum of open opportunity value\nTotal Amount');
expect(container.textContent).not.toContain('Sum of open opportunity value');
});

it.each([
Expand Down
9 changes: 5 additions & 4 deletions ui/packages/plugin-dashboard/src/metricTile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
* narrower than 160 px, 24 px from 160 px up. A longer one — a seven-digit
* money figure with cents — takes a size ladder from 14 px to 24 px whose
* steps start where that size fits (`metricTileValueClass`).
* - The measure label (dataset-bound tile only) is an 11 px muted line under
* the value, dropped below 160 px. The title's tooltip carries it, so it is
* still there at every width.
* - The caption (dataset-bound tile only) is an 11 px muted line under the
* value, dropped below 160 px: the widget's description when one is
* authored, else the measure label. The title's tooltip carries the measure
* label, so it is still there at every width and under a description.
*
* Width rules key off the TILE's own box (`@container` on the tile, `@[160px]:`
* variants), never the viewport: a dashboard grid, a page block and the
Expand Down Expand Up @@ -100,7 +101,7 @@ export function metricTileValueClass(value: string | number): string {
return `${METRIC_TILE_VALUE_BASE} ${rung ? rung.sizes : METRIC_TILE_VALUE_FINAL_SIZES}`;
}

/** The measure label: an 11 px muted line under the value, dropped below 160 px. */
/** The caption (description, else measure label): an 11 px muted line under the value, dropped below 160 px. */
export const METRIC_TILE_MEASURE_LABEL_CLASS = 'hidden max-w-full truncate text-[11px] text-muted-foreground @[160px]:block';

/**
Expand Down
Loading