Skip to content
This repository was archived by the owner on Nov 4, 2026. It is now read-only.

Commit 81a2eb1

Browse files
claude[bot]claude
andauthored
fix(fields): one home for the datetime display convention (#7621)
* fix(fields): one home for the datetime display convention `formatDateTime` gains a named `'compact'` style — today's `DateTimeCellRenderer` face, byte-identical — and the cell renders through it instead of inlining its own pair of `Intl` option bags. The cell also reads `field` (it destructured `value` only), so `field.format` selects a datetime style the way it already did for `date`. `data-table`'s `formatCellValue` calls the same function for its datetime branch instead of a third, independently authored bag. `formatDateTime`'s signature is now `(value, style?, options?)`, matching `formatDate`; the `options` added in objectui#4272 moved to position three and every call site moved with it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KbJQ1y1J12nZxYzFWhP8Q3 * fix(core): formatDateTime keeps (value, options?); 'compact' rides in options.style Maintainer ruling B (objectui#7443, batch #34): the published signature formatDateTime(value, options?) is unchanged and the named 'compact' style is selected through options.style. DateDisplayOptions gains the optional key; the cell calls formatDateTime(date, { style, locale, t }) with the '||' default; the ten-plus in-repo call-site migrations the positional shape forced are reverted to origin/main byte for byte; every pin from ruling 1' is kept and an arity pin guards the signature. Changeset rewritten without the breaking paragraph. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KbJQ1y1J12nZxYzFWhP8Q3 --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent ecf1419 commit 81a2eb1

6 files changed

Lines changed: 597 additions & 34 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
'@object-ui/core': minor
3+
'@object-ui/fields': minor
4+
'@object-ui/components': patch
5+
---
6+
7+
One home for the `datetime` display convention (objectui#7443).
8+
9+
`formatDateTime` gains a named `'compact'` style, selected through
10+
`options.style` — the dense grid face, `7/4/2024 7:00 am` in `en-US` — which
11+
`DateTimeCellRenderer` used to build from its own inlined `Intl` option bags.
12+
The cell now reads `field.format` (it destructured `value` only, so a
13+
`datetime` field could not reach the style vocabulary a `date` field has) and
14+
renders through the shared function, and `data-table`'s `formatCellValue`
15+
calls `formatDateTime` instead of a third, independently authored option bag.
16+
Every existing cell renders byte-identically; `'compact'` is today's face
17+
named and rehoused, not a new one.
18+
19+
Additive, no signature change: `formatDateTime(value, options?)` is unchanged
20+
and `formatDateTime(v, { locale })` keeps meaning what it meant.
21+
`DateDisplayOptions` gains an optional `style` key (read by `formatDateTime`
22+
only; `formatDate` still takes its style positionally), and
23+
`formatDateTimeCompactParts` is a new export of `@object-ui/core`, re-exported
24+
by `@object-ui/fields`, returning the compact face as the two halves a grid
25+
cell paints separately. `@object-ui/components` changes no rendered output —
26+
the table's datetime cell is measured identical before and after in `en-US`,
27+
`zh` and `de-DE`.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
* objectui#7443 — the THIRD spelling of the datetime convention converges.
11+
*
12+
* `formatCellValue` sniffed ISO strings and built its own
13+
* `Intl.DateTimeFormat` with `{ year:'numeric', month:'short', day:'numeric',
14+
* hour:'2-digit', minute:'2-digit' }` — close to `formatDateTime` but
15+
* independently authored, so nothing kept the two in step. It now calls
16+
* `formatDateTime` (default style).
17+
*
18+
* ── The stop-condition this file answers ─────────────────────────────────
19+
* The ruling required `data-table`'s output measured BEFORE and AFTER, and a
20+
* separate line in the PR if a pixel changed. It did not: the bag it used to
21+
* build is the same bag `formatDateTime`'s default branch builds. These pins
22+
* are the measurement — `FORMER_DATETIME_BAG` below is the bag copied verbatim
23+
* from `origin/main`, and the rendered cell is asserted equal to it, so the
24+
* two can never silently diverge again either.
25+
*
26+
* ── The date-only half is deliberately NOT converged ─────────────────────
27+
* `formatDateTime` always carries a time and `formatDate`'s default drops the
28+
* year inside the current year, so routing the date-only branch through either
29+
* WOULD change what renders. #7443's subject is the datetime convention; the
30+
* date-only bag keeps its own spelling here and is pinned unchanged.
31+
*/
32+
import { describe, it, expect, afterEach } from 'vitest';
33+
import React from 'react';
34+
import { render, cleanup } from '@testing-library/react';
35+
import '@testing-library/jest-dom';
36+
import { ComponentRegistry } from '@object-ui/core';
37+
import { I18nProvider, useObjectTranslation } from '@object-ui/i18n';
38+
// Registers the renderers at module scope, NOT inside a `beforeAll` — there the
39+
// cold transform is billed to `hookTimeout` (objectui#3010/#3021).
40+
import '../renderers';
41+
42+
const INSTANT = '2024-07-04T07:00:00.000Z';
43+
const DATE_ONLY = '2024-07-04';
44+
45+
/** The bags `formatCellValue` inlined before this change, copied verbatim. */
46+
const FORMER_DATETIME_BAG: Intl.DateTimeFormatOptions = {
47+
year: 'numeric',
48+
month: 'short',
49+
day: 'numeric',
50+
hour: '2-digit',
51+
minute: '2-digit',
52+
};
53+
const FORMER_DATE_BAG: Intl.DateTimeFormatOptions = {
54+
year: 'numeric',
55+
month: 'short',
56+
day: 'numeric',
57+
};
58+
59+
const former = (iso: string, locale: string, bag: Intl.DateTimeFormatOptions) =>
60+
new Intl.DateTimeFormat(locale, bag).format(new Date(Date.parse(iso)));
61+
62+
/**
63+
* Reports the tag the table itself resolves. `formatCellValue` localizes from
64+
* `useTableTranslation().language`, so the expectation is built from THE SAME
65+
* tag the component read rather than from the one this file asked for — the
66+
* property under test is "identical to the former bag", and hard-coding a tag
67+
* the harness may not actually resolve would measure the harness instead.
68+
*/
69+
function LanguageProbe({ report }: { report: (language: string) => void }) {
70+
report(useObjectTranslation().language);
71+
return null;
72+
}
73+
74+
function renderTable(language: string, value: string) {
75+
const Component = ComponentRegistry.get('data-table')!;
76+
const schema = {
77+
type: 'data-table',
78+
columns: [{ header: 'When', accessorKey: 'when' }],
79+
data: [{ id: 'r1', when: value }],
80+
} as any;
81+
let resolved = '';
82+
const result = render(
83+
<I18nProvider config={{ defaultLanguage: language, detectBrowserLanguage: false }} persistLanguage={false}>
84+
<LanguageProbe report={(l) => { resolved = l; }} />
85+
<Component schema={schema} />
86+
</I18nProvider>,
87+
);
88+
return { ...result, language: () => resolved };
89+
}
90+
91+
const cellText = (container: HTMLElement) =>
92+
container.querySelector('tbody tr td')?.textContent ?? '';
93+
94+
afterEach(() => cleanup());
95+
96+
describe('the datetime cell is byte-identical before and after the convergence', () => {
97+
it.each(['en', 'de'])('%s — the rendered cell equals the former bag', (language) => {
98+
const { container, language: resolved } = renderTable(language, INSTANT);
99+
expect(cellText(container)).toBe(former(INSTANT, resolved(), FORMER_DATETIME_BAG));
100+
});
101+
102+
it('en renders the exact string the card recorded for this path', () => {
103+
const { container } = renderTable('en', INSTANT);
104+
expect(cellText(container)).toBe('Jul 4, 2024, 07:00 AM');
105+
});
106+
107+
it('the shared function and the former bag agree — that is why nothing moved', () => {
108+
for (const language of ['en', 'de', 'zh']) {
109+
const shared = new Date(INSTANT).toLocaleDateString(language, FORMER_DATETIME_BAG);
110+
expect(shared).toBe(former(INSTANT, language, FORMER_DATETIME_BAG));
111+
}
112+
});
113+
});
114+
115+
describe('the date-only cell is untouched', () => {
116+
it.each(['en', 'de'])('%s — still the date-only bag, with no time appended', (language) => {
117+
const { container, language: resolved } = renderTable(language, DATE_ONLY);
118+
expect(cellText(container)).toBe(former(DATE_ONLY, resolved(), FORMER_DATE_BAG));
119+
expect(cellText(container)).not.toMatch(/\d\d:\d\d/);
120+
});
121+
});
122+
123+
describe('non-date values are still returned untouched', () => {
124+
it('a plain string is not sniffed into a date', () => {
125+
const { container } = renderTable('en', 'not-a-date-at-all');
126+
expect(cellText(container)).toBe('not-a-date-at-all');
127+
});
128+
});

‎packages/components/src/renderers/complex/data-table.tsx‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { cn } from '../../lib/utils';
1212
import { resolveIcon } from '../action/resolve-icon';
1313
import { useGridFieldAuthoring } from '../../context/gridFieldAuthoring';
1414
import { describeIgnoredBind, describeNonArrayData } from './dataTableBindDiagnostic';
15-
import { ComponentRegistry, compareSortValues, evalRowPredicate, getSortValue } from '@object-ui/core';
15+
import { ComponentRegistry, compareSortValues, evalRowPredicate, formatDateTime, getSortValue } from '@object-ui/core';
1616
import type { DataTableSchema, TableSortItem, TableColumnType } from '@object-ui/types';
1717
import { SchemaRenderer, useRowPredicate, usePredicateScope } from '@object-ui/react';
1818
import { createSafeTranslation } from '@object-ui/i18n';
@@ -766,10 +766,22 @@ const DataTableRenderer = ({ schema }: { schema: DataTableSchema }) => {
766766
if (Number.isNaN(ts)) return value;
767767
const hasTime = value.includes('T');
768768
try {
769-
const fmt = new Intl.DateTimeFormat(language, hasTime
770-
? { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }
771-
: { year: 'numeric', month: 'short', day: 'numeric' });
772-
return fmt.format(new Date(ts));
769+
// The datetime half is `formatDateTime`'s DEFAULT style — the one home
770+
// for this convention (objectui#7443). It used to be a third,
771+
// independently authored `Intl.DateTimeFormat` bag here, close to but
772+
// not derived from the shared function. Byte-identical in en-US, zh and
773+
// de-DE, so no table cell changes.
774+
if (hasTime) return formatDateTime(new Date(ts), { locale: language });
775+
// The DATE-only half keeps its own bag on purpose: `formatDateTime`
776+
// always carries a time, and `formatDate`'s default drops the year in
777+
// the current year — routing this branch through either WOULD change
778+
// what renders. #7443's subject is the datetime convention; the
779+
// date-only divergence is recorded separately.
780+
return new Intl.DateTimeFormat(language, {
781+
year: 'numeric',
782+
month: 'short',
783+
day: 'numeric',
784+
}).format(new Date(ts));
773785
} catch {
774786
return value;
775787
}

‎packages/core/src/utils/date-display.ts‎

Lines changed: 97 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,50 @@
2929
* upper package re-exports it, so there is one home and nothing to drift.
3030
*
3131
* `@object-ui/fields` re-exports every symbol below under its original name,
32-
* so `formatDate` / `formatDateTime` / `formatRelativeDate` /
33-
* `DateDisplayOptions` keep working unchanged for `ObjectGrid`, `ObjectGantt`,
34-
* `plugin-dashboard`'s `recordFields` and the `date` cell renderer.
32+
* so `formatDate` / `formatDateTime` / `formatDateTimeCompactParts` /
33+
* `formatRelativeDate` / `DateDisplayOptions` keep working unchanged for
34+
* `ObjectGrid`, `ObjectGantt`, `plugin-dashboard`'s `recordFields` and the
35+
* `date` cell renderer.
36+
*
37+
* The `datetime` CELL face joined this file in objectui#7443. It used to be a
38+
* second convention inlined in `DateTimeCellRenderer`: two `Intl` option bags
39+
* for one field type, kept in step by nothing, while `date` had exactly one.
40+
* It is `formatDateTime`'s `'compact'` style now, byte-identical to what the
41+
* cell rendered before.
3542
*
3643
* Pure by construction (no React, no i18n): the only ambient inputs are `Intl`
3744
* and the clock, and the one phrase `Intl` cannot produce ("Overdue Nd") comes
3845
* in through the INJECTED `options.t`, the same way `buildDatasetFieldHelpers`
3946
* in `dataset-format.ts` takes `fieldLabel`.
4047
*/
4148

42-
/** Options shared by {@link formatDate} / {@link formatRelativeDate}. */
49+
/**
50+
* Options shared by {@link formatDate} / {@link formatRelativeDate} /
51+
* {@link formatDateTime}. One bag, and each function reads the keys it needs:
52+
* `dueLike` and `t` only matter on the relative path, `style` is read by
53+
* `formatDateTime` only (see below).
54+
*/
4355
export interface DateDisplayOptions {
4456
dueLike?: boolean;
4557
/** BCP-47 display locale (ADR-0053 tenant default); falls back to the runtime locale. */
4658
locale?: string;
4759
/** i18n translate fn for phrases `Intl` can't produce (the "Overdue Nd" wording). */
4860
t?: (key: string, params?: Record<string, unknown>) => string;
61+
/**
62+
* Named face, read by {@link formatDateTime}: `'compact'` is the dense grid
63+
* cell face (objectui#7443); anything else, or absent, is the default face.
64+
*
65+
* It rides here rather than in a second positional parameter because
66+
* `formatDateTime(value, options?)` is a PUBLISHED signature with `options`
67+
* in position two (objectui#4272). A positional `style` would have displaced
68+
* it: TypeScript would reject the old call, but a JavaScript caller would
69+
* silently hand its options bag to the style slot and lose its locale —
70+
* the #4272 defect again. {@link formatDate} still takes its style
71+
* positionally and does NOT read this key; the symmetric long-run shape
72+
* (both functions reading `options.style`) is additive on `formatDate` and
73+
* deliberately not part of #7443.
74+
*/
75+
style?: string;
4976
}
5077

5178
/**
@@ -136,22 +163,81 @@ export function formatDate(value: string | Date | number, style?: string, option
136163
});
137164
}
138165

166+
/**
167+
* The `'compact'` datetime face as the two halves a grid cell paints
168+
* separately — `7/4/2024` and `7:00 am` for `2024-07-04T07:00:00Z` in `en-US`.
169+
*
170+
* `formatDateTime(value, { style: 'compact', ...options })` is exactly
171+
* `date + ' ' + time` of what this returns, so a caller that wants the face
172+
* as ONE string and a caller that wants to style the halves differently
173+
* cannot drift apart. That drift is what objectui#7443 recorded:
174+
* `DateTimeCellRenderer` inlined these two option bags and never called this
175+
* module, so `datetime` had two display conventions while `date` had one —
176+
* the same shape as objectui#4576, which this repo has already paid for once.
177+
*
178+
* `null` for a value this module renders as `'—'`; the cell renders its own
179+
* empty state for those, so it never sees the dash.
180+
*/
181+
export function formatDateTimeCompactParts(
182+
value: string | Date | number,
183+
options?: DateDisplayOptions,
184+
): { date: string; time: string } | null {
185+
if (value === null || value === undefined || value === '') return null;
186+
const date = value instanceof Date ? value : new Date(value as any);
187+
if (!(date instanceof Date) || isNaN(date.getTime())) return null;
188+
189+
return {
190+
date: date.toLocaleDateString(options?.locale, {
191+
month: 'numeric',
192+
day: 'numeric',
193+
year: 'numeric',
194+
}),
195+
// `hour12` stays declared: this is the compact Airtable-style cell, and
196+
// the 12-hour face is its design, not a locale artefact. Locales that
197+
// write no am/pm marker simply ignore it.
198+
time: date.toLocaleTimeString(options?.locale, {
199+
hour: 'numeric',
200+
minute: '2-digit',
201+
hour12: true,
202+
}).toLowerCase(),
203+
};
204+
}
205+
139206
/**
140207
* Format datetime value.
141208
*
142-
* `options` mirrors {@link formatDate}'s and is optional, so an existing
143-
* caller that passes nothing keeps the exact runtime-default behavior it had.
144-
* Before objectui#4272 the parameter did not exist at all, which meant no
145-
* caller could localize this function however hard it tried — it always handed
146-
* `Intl` an `undefined` tag, i.e. the MACHINE's locale, which is neither of
147-
* the repo's two locale channels. Callers should pass the tag from
148-
* `useDisplayLocale()`.
209+
* `options.style` selects a named face:
210+
*
211+
* - `'compact'` — the dense grid face, `7/4/2024 7:00 am` in `en-US`. It is
212+
* what every `datetime` CELL renders, and what `DateTimeCellRenderer`
213+
* used to build from its own inlined `Intl` bags (objectui#7443).
214+
* - anything else, including absent — the verbose default,
215+
* `Jul 4, 2024, 07:00 AM` in `en-US`. Unchanged, and still what a
216+
* non-cell caller (dataset measure, gantt tooltip, data-table) gets.
217+
*
218+
* The signature is `(value, options?)`, unchanged: `style` is a key of
219+
* `options`, not a positional parameter, so every existing call —
220+
* `formatDateTime(v, { locale })` included — keeps meaning exactly what it
221+
* meant (see the note on `DateDisplayOptions.style` for why the positional
222+
* shape `formatDate` uses was refused here).
223+
*
224+
* `options` is optional, so a caller that passes nothing keeps the exact
225+
* runtime-default behavior it had. Before objectui#4272 the parameter did not
226+
* exist at all, which meant no caller could localize this function however
227+
* hard it tried — it always handed `Intl` an `undefined` tag, i.e. the
228+
* MACHINE's locale, which is neither of the repo's two locale channels.
229+
* Callers should pass the tag from `useDisplayLocale()`.
149230
*/
150231
export function formatDateTime(value: string | Date | number, options?: DateDisplayOptions): string {
151232
if (value === null || value === undefined || value === '') return '—';
152233
const date = value instanceof Date ? value : new Date(value as any);
153234
if (!(date instanceof Date) || isNaN(date.getTime())) return '—';
154235

236+
if (options?.style === 'compact') {
237+
const parts = formatDateTimeCompactParts(date, options);
238+
return parts ? `${parts.date} ${parts.time}` : '—';
239+
}
240+
155241
return date.toLocaleDateString(options?.locale, {
156242
year: 'numeric',
157243
month: 'short',

0 commit comments

Comments
 (0)