Skip to content

Commit eef23e7

Browse files
committed
fix(DateTimeInput): a press's deferred-click watchers leave with the field
A press defers the time-option show past the click that ends it with a one-shot `document` click listener. Nothing removed that listener when the field unmounted first, so a press that never became a click left a watcher standing that answered the next click anywhere in the document with work for a field that was gone. The watchers now register through one helper, and unmounting drops every watcher still pending.
1 parent c8a172b commit eef23e7

2 files changed

Lines changed: 101 additions & 13 deletions

File tree

‎packages/core/src/DateTimeInput/DateTimeInput.test.tsx‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,70 @@ describe('DateTimeInput', () => {
22612261
expect(timeInput).toHaveAttribute('aria-expanded', 'true');
22622262
});
22632263

2264+
it('drops the watcher of a press that never became a click when the field unmounts', () => {
2265+
// The press defers its show to a one-shot `document` click watcher, and a
2266+
// press that ends without a click leaves that watcher standing. It belongs
2267+
// to this field, so unmounting takes it along: otherwise the next click
2268+
// anywhere in the document runs work for a field that is gone. The Layer
2269+
// gesture counter also watches `click` on the document, in the capture
2270+
// phase and for the life of the page, so only the bubble phase is this
2271+
// field's business. The count comes from running the listener the document
2272+
// really holds, so this fails when the watcher survives, not when the spy
2273+
// misfires.
2274+
const add = document.addEventListener.bind(document);
2275+
const remove = document.removeEventListener.bind(document);
2276+
const isCapture = (options?: boolean | AddEventListenerOptions) =>
2277+
typeof options === 'object'
2278+
? Boolean(options.capture)
2279+
: Boolean(options);
2280+
let watchers = 0;
2281+
let fired = 0;
2282+
const wrapped = new Map<unknown, EventListener>();
2283+
const added = vi
2284+
.spyOn(document, 'addEventListener')
2285+
.mockImplementation((type, listener, options) => {
2286+
if (type !== 'click' || isCapture(options)) {
2287+
return add(type, listener, options);
2288+
}
2289+
watchers += 1;
2290+
const counting: EventListener = (event, ...rest) => {
2291+
fired += 1;
2292+
if (typeof listener === 'function') {
2293+
listener(event, ...rest);
2294+
}
2295+
};
2296+
wrapped.set(listener, counting);
2297+
return add(type, counting, options);
2298+
});
2299+
const removed = vi
2300+
.spyOn(document, 'removeEventListener')
2301+
.mockImplementation((type, listener, options) =>
2302+
remove(type, wrapped.get(listener) ?? listener, options),
2303+
);
2304+
2305+
try {
2306+
const {unmount} = render(
2307+
<DateTimeInput
2308+
label="Meeting"
2309+
value={'2026-03-15T09:00' as ISODateTimeString}
2310+
timeOptionInterval={60}
2311+
onChange={() => {}}
2312+
/>,
2313+
);
2314+
fireEvent.pointerDown(screen.getByLabelText('Meeting time'));
2315+
expect(watchers).toBeGreaterThan(0);
2316+
expect(fired).toBe(0);
2317+
2318+
unmount();
2319+
fireEvent.click(document.body);
2320+
expect(fired).toBe(0);
2321+
} finally {
2322+
wrapped.clear();
2323+
added.mockRestore();
2324+
removed.mockRestore();
2325+
}
2326+
});
2327+
22642328
it('activates the first option when the field has a date but no time', () => {
22652329
// The state you land in after picking a date and no time yet. With no
22662330
// active option, ArrowUp and Enter both did nothing at all.

‎packages/core/src/DateTimeInput/DateTimeInput.tsx‎

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,36 @@ function PointerDateTimeField({
11421142
// it, as BaseTypeahead does.
11431143
const timePointerActiveRef = useRef(false);
11441144

1145+
// The watchers carrying that deferral are one-shot listeners on `document`,
1146+
// and they belong to this field. Unmounting takes them with it: a watcher
1147+
// left standing answers the next click anywhere in the document with work for
1148+
// a field that is gone.
1149+
const timeClickWatchersRef = useRef<Set<EventListener> | null>(null);
1150+
1151+
const addTimeClickWatcher = useCallback((handler: () => void) => {
1152+
const watchers = timeClickWatchersRef.current ?? new Set<EventListener>();
1153+
timeClickWatchersRef.current = watchers;
1154+
const watcher: EventListener = () => {
1155+
// The `once` option removes this listener as the browser delivers it;
1156+
// deleting here is what keeps the set from holding every past press.
1157+
watchers.delete(watcher);
1158+
handler();
1159+
};
1160+
watchers.add(watcher);
1161+
document.addEventListener('click', watcher, {once: true});
1162+
}, []);
1163+
1164+
const clearTimeClickWatchers = useCallback(() => {
1165+
for (const watcher of timeClickWatchersRef.current ?? []) {
1166+
document.removeEventListener('click', watcher);
1167+
}
1168+
timeClickWatchersRef.current?.clear();
1169+
}, []);
1170+
1171+
// The Effect owns this field's `document` click watchers: its setup acquires
1172+
// nothing, its cleanup releases whatever a press left pending.
1173+
useEffect(() => clearTimeClickWatchers, [clearTimeClickWatchers]);
1174+
11451175
// Whether the highlight is still the one typing derived, or the user has
11461176
// since moved it themselves. Enter honours typed text only in the former
11471177
// case — otherwise the field would commit something other than the option
@@ -1150,14 +1180,10 @@ function PointerDateTimeField({
11501180

11511181
const markTimePointerActive = useCallback(() => {
11521182
timePointerActiveRef.current = true;
1153-
document.addEventListener(
1154-
'click',
1155-
() => {
1156-
timePointerActiveRef.current = false;
1157-
},
1158-
{once: true},
1159-
);
1160-
}, []);
1183+
addTimeClickWatcher(() => {
1184+
timePointerActiveRef.current = false;
1185+
});
1186+
}, [addTimeClickWatcher]);
11611187

11621188
// The time list is a manual popover: the browser's light dismiss counts a
11631189
// press in the time input as outside (a text input cannot be an invoker), so
@@ -1309,16 +1335,14 @@ function PointerDateTimeField({
13091335
// input must hold DOM focus or aria-activedescendant announces nothing.
13101336
timeInputRef.current?.focus();
13111337
if (timePointerActiveRef.current) {
1312-
document.addEventListener(
1313-
'click',
1314-
() =>
1315-
requestAnimationFrame(() => timePopover.show({skipAutoFocus: true})),
1316-
{once: true},
1338+
addTimeClickWatcher(() =>
1339+
requestAnimationFrame(() => timePopover.show({skipAutoFocus: true})),
13171340
);
13181341
} else {
13191342
timePopover.show({skipAutoFocus: true});
13201343
}
13211344
}, [
1345+
addTimeClickWatcher,
13221346
hasTimeOptions,
13231347
isEffectivelyDisabled,
13241348
timePopover,

0 commit comments

Comments
 (0)