Skip to content

Commit 2da2979

Browse files
fix(fields): a click in the record form date box opens the calendar (#36522)
<!-- claude-projects-attribution: {"github_login":"os-steve"} --> _Requested by **Steve** · [project thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9JpRoUZGqtwqMU5k7b4C58D)_ Fixes #36457 Clause-②: no Metadata-protocol: none ## 维护者速读 - 业务用户在记录新建、编辑表单及复用同一控件的对话框里,点日期输入框本身就会弹出日历;以前只有点右侧小图标才弹,点框只出光标。 - 解决外部试用中用户把日期框当成坏了的体验问题(界面体验专题),替前线业务用户解决。 - 若出错,最坏是日期框点击后日历不弹或焦点位置不对,手输日期与日历图标仍照旧可用;可直接撤回此改动。 - 无 ## What changes `TemporalPicker` in `ui/packages/fields/src/widgets/date-picker/DatePickerField.tsx` (both the `date` and the `datetime` face, which share it): - Outside a grid cell, a click in the text box opens the calendar popover the calendar button opens. - The box keeps the focus when the popover opens from it (the popover's open auto-focus and the calendar's own `autoFocus` are skipped), so typing still edits the text; the text is still read on Enter or blur, as before. - A click or focus in the box is not treated as a click outside, so it does not close the popover. Enter commits the text and, when it names a date, closes the popover. Escape closes it and the focus stays in the box (Radix would otherwise move it to the calendar button). On close, the focus returns to the box only when nothing else took it. - Only a click opens it: focus arriving by Tab opens nothing, so tabbing through a form does not pop a calendar at every date field, and Tab moves on without entering the popover. - A11y: the box carries the popover relationship Radix gives the button: `aria-haspopup="dialog"`, `aria-expanded`, and `aria-controls` naming the popover while open. The button keeps its label; its `aria-controls` now names the same popover id. - The calendar button is unchanged. The grid's in-cell face (`inCell`, settled by #36023) is not touched: it draws no popover and its box gets none of the above. - Placeholder: `parseLocaleDateText` reads digit groups split by any non-digit, so `/`, `-`, `.` and spaces are all accepted. Left as it is, per the dispatch. ## Tests New top-level describe in the existing harness `DatePickerField-12622.test.tsx` (10 cases): a click in the box opens the popover, keeps focus in the box and wires `aria-haspopup` / `aria-expanded` / `aria-controls`; typing keeps it open and a typed date still writes, Enter commits and closes; a second click keeps it open; Escape closes and focus stays in the box; a calendar pick after a box click writes and closes; focus without a click opens nothing; a disabled box opens nothing; the button still opens it and moves focus into the calendar; the datetime face opens the same way with its time row; CONTROL: the in-cell face has no popover relationship and a click opens nothing. <details><summary>Local run (head 53e0952, under the shared verify lock)</summary> ``` pnpm --dir ui exec vitest run --maxWorkers=2 packages/fields/src/widgets/date-picker/ packages/fields/src/widgets/imeComposition-35996.test.tsx packages/fields/src/widgets/touchTarget.formControls-29072.test.tsx packages/fields/src/__tests__/inCellFaces-35688.test.tsx packages/fields/src/__tests__/inline-widget-aria-invalid-7126.test.tsx packages/fields/src/__tests__/date-picker-invalid-text-blocks-submit-27483.test.tsx packages/plugin-detail/src/__tests__/InlineFieldInput.datetimePicker-12759.test.tsx packages/app-shell/src/views/ScreenView.platformControls-26579.test.tsx Test Files 9 passed (9) Tests 141 passed | 4 skipped (145) os-verify-lock: VERDICT command-exit 0 ``` The extra files are every other suite that drives this widget (IME Enter, touch targets, in-cell faces, aria-invalid, invalid-text submit, record-page inline editing, screen view controls). </details> NOT MEASURED: `pnpm --filter @object-ui/fields type-check` locally, reason: it needs the `@object-ui/fields^...` dist closure (about fifty packages) built, under a verify lock that was contended for over an hour; `Type Check UI` in CI measures it. NOT MEASURED: ablation of the box click, reason: the first attempt was refused by `scripts/ablation-replace.mjs` before any test ran (the replacement text was not distinct, a no-op the tool caught and restored), and the corrected re-run did not reach the lock before this PR opened. The report on the card carries the reading if it lands. ## Acceptance notes - On a phone, the box's `inputMode="numeric"` still raises the soft keyboard on tap, now together with the calendar. Not changed here: switching keyboard off would cost keyboard entry, which the dispatch keeps. - Other non-grid hosts of this widget get the same behaviour: the record page's inline details editing (`InlineFieldInput`, via `FieldEditWidget`), the kanban required-fields dialog, the filter builder and screen view controls. All are form-like faces, not list cells. - Arrow-key entry from the box into the open calendar is not added; the calendar button remains the keyboard route into the calendar. ## Verification Read first: 1. `ui/packages/fields/src/widgets/date-picker/DatePickerField.tsx`, the `PopoverContent` handlers (`onOpenAutoFocus`, `onInteractOutside`, `onCloseAutoFocus`): the focus and dismissal logic is where a regression would hide. 2. The same file, the text box `onClick` and the `opensPopover` argument: confirms the in-cell face is untouched. 3. The new describe in `DatePickerField-12622.test.tsx`. Two-minute check: in the showcase console, open any object's New dialog with a date field and click inside the date box. Expect the calendar to open under the box with the cursor still in the box; type `2026-10-20` and press Enter; expect the value set and the calendar closed. Local gates: the 38 commands `node scripts/pm/dispatch-gates.mjs --commands` derived for this diff, plus `pnpm check:changeset-fixed`, all exit 0 except `pnpm check:type-check-debt` (NOT MEASURED: its closure build waits on the same contended lock; CI runs it) and `pnpm --dir ui exec vitest run scripts/` (left to CI's Test UI). 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01ADHkmRY38EZf38Yy3zthbY <!-- signed: repo:objectstack#2 · os-steve · session_01ADHkmRY38EZf38Yy3zthbY --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 316f757 commit 2da2979

3 files changed

Lines changed: 192 additions & 7 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@object-ui/fields': patch
3+
---
4+
5+
fix(fields): a click in a record form's date box opens the calendar
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
On a record form, and in the dialogs that reuse the same date and date-time widget, the calendar opened only from the small calendar button at the right of the box; a click in the box itself put a text cursor there and nothing else, and users took the field for broken. A click in the box now opens the same calendar popover. The box keeps the focus, so typing a date still works: the text is read on Enter or when the box loses focus, as before, and Enter also closes the popover. Escape closes it and leaves the focus in the box. Focus that arrives by Tab opens nothing, so tabbing through a form does not pop a calendar at each date field. The calendar button works as before. The box now carries the popover relationship the button carries (`aria-haspopup="dialog"`, `aria-expanded`, `aria-controls`). The grid's in-cell date editor, which already shows the calendar beneath the box, is unchanged, and the value written is unchanged.

‎ui/packages/fields/src/widgets/date-picker/DatePickerField-12622.test.tsx‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,122 @@ describe('DateTimePickerField — the popover picks the time (objectstack#32589)
334334
await waitFor(() => expect(document.body.querySelector('button[data-day]')).toBeNull());
335335
});
336336
});
337+
338+
describe('DatePickerField — a click in the text box opens the calendar (objectstack#36457)', () => {
339+
const popover = () => document.body.querySelector('[role="dialog"]') as HTMLElement | null;
340+
const clickBox = (input: HTMLInputElement) => {
341+
// A real click focuses the box first; fireEvent.click alone does not.
342+
act(() => input.focus());
343+
fireEvent.pointerDown(input);
344+
fireEvent.mouseDown(input);
345+
fireEvent.mouseUp(input);
346+
fireEvent.click(input);
347+
};
348+
349+
it('opens the popover, keeps the focus in the box, and names the popover on the box', async () => {
350+
const { container } = render(<DatePickerField value="2026-10-16" onChange={() => {}} field={FIELD} />);
351+
const input = textBox(container);
352+
expect(input.getAttribute('aria-haspopup')).toBe('dialog');
353+
expect(input.getAttribute('aria-expanded')).toBe('false');
354+
expect(input.getAttribute('aria-controls')).toBeNull();
355+
clickBox(input);
356+
await waitFor(() => expect(popover()).not.toBeNull());
357+
expect(popover()!.querySelector('button[data-day]')).not.toBeNull();
358+
expect(document.activeElement).toBe(input);
359+
expect(input.getAttribute('aria-expanded')).toBe('true');
360+
expect(input.getAttribute('aria-controls')).toBe(popover()!.id);
361+
expect(screen.getByTestId('date-picker-trigger').getAttribute('aria-controls')).toBe(popover()!.id);
362+
expect(screen.getByTestId('date-picker-trigger').getAttribute('aria-label')).toBe('Open calendar');
363+
});
364+
365+
it('typing keeps the popover open, and a typed date still writes; Enter commits and closes', async () => {
366+
const onChange = vi.fn();
367+
const { container } = render(<DatePickerField value="" onChange={onChange} field={FIELD} />);
368+
const input = textBox(container);
369+
clickBox(input);
370+
await waitFor(() => expect(popover()).not.toBeNull());
371+
fireEvent.change(input, { target: { value: '10/20/2026' } });
372+
expect(popover()).not.toBeNull();
373+
expect(document.activeElement).toBe(input);
374+
fireEvent.keyDown(input, { key: 'Enter' });
375+
expect(onChange).toHaveBeenCalledWith('2026-10-20');
376+
await waitFor(() => expect(popover()).toBeNull());
377+
expect(document.activeElement).toBe(input);
378+
});
379+
380+
it('a second click in the box leaves the popover open', async () => {
381+
const { container } = render(<DatePickerField value="2026-10-16" onChange={() => {}} field={FIELD} />);
382+
const input = textBox(container);
383+
clickBox(input);
384+
await waitFor(() => expect(popover()).not.toBeNull());
385+
clickBox(input);
386+
expect(popover()).not.toBeNull();
387+
});
388+
389+
it('Escape closes it and leaves the focus in the box', async () => {
390+
const { container } = render(<DatePickerField value="2026-10-16" onChange={() => {}} field={FIELD} />);
391+
const input = textBox(container);
392+
clickBox(input);
393+
await waitFor(() => expect(popover()).not.toBeNull());
394+
fireEvent.keyDown(input, { key: 'Escape' });
395+
await waitFor(() => expect(popover()).toBeNull());
396+
expect(document.activeElement).toBe(input);
397+
expect(input.getAttribute('aria-expanded')).toBe('false');
398+
});
399+
400+
it('a calendar pick after a click in the box writes the day and closes', async () => {
401+
const onChange = vi.fn();
402+
const { container } = render(<DatePickerField value="2026-10-16" onChange={onChange} field={FIELD} />);
403+
clickBox(textBox(container));
404+
const day = await waitFor(() => {
405+
const el = document.body.querySelector(`button[data-day="${new Date(2026, 9, 20).toLocaleDateString()}"]`);
406+
if (!el) throw new Error('calendar not open');
407+
return el as HTMLButtonElement;
408+
});
409+
act(() => {
410+
fireEvent.click(day);
411+
});
412+
expect(onChange).toHaveBeenCalledWith('2026-10-20');
413+
await waitFor(() => expect(popover()).toBeNull());
414+
});
415+
416+
it('focus arriving without a click (Tab) opens nothing', () => {
417+
const { container } = render(<DatePickerField value="2026-10-16" onChange={() => {}} field={FIELD} />);
418+
const input = textBox(container);
419+
act(() => input.focus());
420+
fireEvent.focus(input);
421+
expect(popover()).toBeNull();
422+
});
423+
424+
it('a disabled box opens nothing', () => {
425+
const { container } = render(<DatePickerField value="" onChange={() => {}} field={FIELD} disabled />);
426+
fireEvent.click(textBox(container));
427+
expect(popover()).toBeNull();
428+
});
429+
430+
it('the calendar button still opens it and moves the focus into the calendar', async () => {
431+
render(<DatePickerField value="2026-10-16" onChange={() => {}} field={FIELD} />);
432+
fireEvent.click(screen.getByTestId('date-picker-trigger'));
433+
await waitFor(() => expect(popover()).not.toBeNull());
434+
await waitFor(() => expect(popover()!.contains(document.activeElement)).toBe(true));
435+
});
436+
437+
it('the datetime face opens the same way, with its time row', async () => {
438+
const { container } = render(
439+
<DateTimePickerField value={new Date(2026, 10, 12, 9, 15).toISOString()} onChange={() => {}} field={DT_FIELD} />,
440+
);
441+
const input = textBox(container);
442+
clickBox(input);
443+
expect(await screen.findByTestId('date-picker-time')).toBeTruthy();
444+
expect(document.activeElement).toBe(input);
445+
});
446+
447+
it('CONTROL: the in-cell face is unchanged — no popover relationship on its box', () => {
448+
const { container } = render(<DatePickerField value="2026-10-16" onChange={() => {}} field={FIELD} inCell />);
449+
const input = textBox(container);
450+
expect(container.querySelector('[data-in-cell-face="panel"]')).not.toBeNull();
451+
expect(input.getAttribute('aria-haspopup')).toBeNull();
452+
fireEvent.click(input);
453+
expect(popover()).toBeNull();
454+
});
455+
});

‎ui/packages/fields/src/widgets/date-picker/DatePickerField.tsx‎

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ import { isImeKey } from '../imeKey.js';
9292
* today. Every pick goes through `fromDateTimeInputValue`, the same write the
9393
* text box uses. The `date` face is unchanged: no time row, and a day pick
9494
* closes the popover.
95+
*
96+
* ## A click in the text box opens the calendar too
97+
*
98+
* Outside a grid cell, a click in the text box opens the same popover its
99+
* calendar button opens, and the box keeps the focus: typing still edits the
100+
* text, Enter commits it and closes the popover, Escape closes it, and Tab
101+
* moves on without entering it. Only a click opens it: focus arriving by Tab
102+
* does not, so tabbing through a form never pops a calendar. Closing returns
103+
* the focus to the box when nothing else took it. The grid's in-cell face is
104+
* not this face: it shows the calendar beneath the box already.
95105
*/
96106
export function DatePickerField(props: FieldWidgetComponentProps<string>) {
97107
return <TemporalPicker {...props} withTime={false} />;
@@ -127,6 +137,11 @@ function TemporalPicker({
127137
const { t } = useFieldTranslation();
128138
const noticeId = React.useId();
129139
const [open, setOpen] = React.useState(false);
140+
// Whether the popover was opened by a click in the text box, which keeps
141+
// the focus there, rather than by the calendar button.
142+
const [openedFromBox, setOpenedFromBox] = React.useState(false);
143+
const panelId = React.useId();
144+
const boxRef = React.useRef<HTMLInputElement>(null);
130145
const hourRef = React.useRef<HTMLButtonElement>(null);
131146
const rootRef = React.useRef<HTMLDivElement>(null);
132147

@@ -188,13 +203,15 @@ function TemporalPicker({
188203
if (next !== nativeValue) onChange(stored);
189204
};
190205

206+
/** Reads the typed text; `false` when it names no date. */
191207
const commitText = () => {
192208
const parsed = parseLocaleDateText(text, locale, withTime);
193209
if (parsed === null) {
194210
setBadText(text);
195-
return;
211+
return false;
196212
}
197213
write(parsed);
214+
return true;
198215
};
199216

200217
/**
@@ -251,10 +268,14 @@ function TemporalPicker({
251268
? [domProps['aria-describedby'], noticeId].filter(Boolean).join(' ')
252269
: domProps['aria-describedby'];
253270

254-
/** The text box: the value as the reader's locale writes it, typeable. */
255-
const textBox = (className: string) => (
271+
/**
272+
* The text box: the value as the reader's locale writes it, typeable. With
273+
* `opensPopover` a click in it opens the calendar popover too.
274+
*/
275+
const textBox = (className: string, opensPopover = false) => (
256276
<Input
257277
{...domProps}
278+
ref={boxRef}
258279
type="text"
259280
inputMode="numeric"
260281
autoComplete="off"
@@ -274,13 +295,23 @@ function TemporalPicker({
274295
// Commit, and keep a bare Enter from submitting the form with
275296
// the text not yet read.
276297
e.preventDefault();
277-
commitText();
298+
if (commitText() && opensPopover) setOpen(false);
278299
}
279300
}}
301+
onClick={opensPopover ? (e) => {
302+
domProps.onClick?.(e);
303+
if (open || disabled) return;
304+
setOpenedFromBox(true);
305+
setOpen(true);
306+
} : domProps.onClick}
280307
disabled={disabled}
281308
// After the DOM spread, the objectui#3222 idiom `DateField` follows.
282309
aria-invalid={!!error || impossible || badText !== null}
283310
aria-describedby={describedBy}
311+
// The popover relationship Radix gives the calendar button.
312+
aria-haspopup={opensPopover ? 'dialog' : undefined}
313+
aria-expanded={opensPopover ? open : undefined}
314+
aria-controls={opensPopover && open ? panelId : undefined}
284315
/>
285316
);
286317

@@ -294,7 +325,8 @@ function TemporalPicker({
294325
defaultMonth={selected}
295326
selected={selected}
296327
onSelect={pickDay}
297-
autoFocus={!inCell}
328+
// Opened from the text box, the focus stays in the box.
329+
autoFocus={!inCell && !openedFromBox}
298330
className="[--cell-size:2.75rem] sm:[--cell-size:2rem]"
299331
/>
300332
{withTime ? (
@@ -396,7 +428,7 @@ function TemporalPicker({
396428
<div className="relative">
397429
{/* Below `sm` the two buttons inside grow to the touch floor, so the
398430
text stops short of both (2 x 44 px plus the gap). */}
399-
{textBox(cn(PHONE_TOUCH_HEIGHT, 'pr-24 sm:pr-16', domProps.className))}
431+
{textBox(cn(PHONE_TOUCH_HEIGHT, 'pr-24 sm:pr-16', domProps.className), true)}
400432
<div className="absolute inset-y-0 right-1 flex items-center gap-0.5">
401433
{!disabled && (text || nativeValue) ? (
402434
<Button
@@ -411,7 +443,14 @@ function TemporalPicker({
411443
<X className="h-3.5 w-3.5" />
412444
</Button>
413445
) : null}
414-
<Popover open={open} onOpenChange={setOpen}>
446+
<Popover
447+
open={open}
448+
onOpenChange={(next) => {
449+
// Only the calendar button and a dismissal come through here.
450+
if (next) setOpenedFromBox(false);
451+
setOpen(next);
452+
}}
453+
>
415454
<PopoverTrigger asChild>
416455
<Button
417456
type="button"
@@ -420,14 +459,31 @@ function TemporalPicker({
420459
className={cn('h-7 w-7', PHONE_TOUCH_SQUARE)}
421460
disabled={disabled}
422461
aria-label={t('fields.datePicker.openCalendar')}
462+
aria-controls={open ? panelId : undefined}
423463
data-testid="date-picker-trigger"
424464
>
425465
<CalendarDays className="h-4 w-4" />
426466
</Button>
427467
</PopoverTrigger>
428468
<PopoverContent
469+
id={panelId}
429470
className="w-auto p-0"
430471
align="end"
472+
onOpenAutoFocus={(e) => {
473+
if (openedFromBox) e.preventDefault();
474+
}}
475+
// A click or focus in the text box is not a click outside.
476+
onInteractOutside={(e) => {
477+
if (boxRef.current?.contains(e.target as Node)) e.preventDefault();
478+
}}
479+
onCloseAutoFocus={(e) => {
480+
if (!openedFromBox) return;
481+
// Not to the calendar button: back to the box, unless the
482+
// close moved the focus somewhere else on purpose.
483+
e.preventDefault();
484+
const active = document.activeElement;
485+
if (!active || active === document.body) boxRef.current?.focus();
486+
}}
431487
>
432488
{panel}
433489
</PopoverContent>

0 commit comments

Comments
 (0)