An open source design system that's fully customizable and agent ready
TypeScript
fix(DateTimeInput): a press's deferred-click watchers leave with the field (#10155)
Aashish Mahato17e36e7481ee
diff --git a/.changeset/datetimeinput-a-press-that-never-becomes-a-click-0vlq.md b/.changeset/datetimeinput-a-press-that-never-becomes-a-click-0vlq.mdnew file mode 100644index 0000000000..ab7edf924f--- /dev/null+++ b/.changeset/datetimeinput-a-press-that-never-becomes-a-click-0vlq.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] DateTimeInput: a press that never becomes a click no longer leaves its document click watchers behind when the field unmounts (#10155)+@aashish254diff --git a/packages/core/src/DateTimeInput/DateTimeInput.test.tsx b/packages/core/src/DateTimeInput/DateTimeInput.test.tsxindex aa1947ff7d..8dfb17c2be 100644--- a/packages/core/src/DateTimeInput/DateTimeInput.test.tsx+++ b/packages/core/src/DateTimeInput/DateTimeInput.test.tsx@@ -2261,6 +2261,72 @@ describe('DateTimeInput', () => {expect(timeInput).toHaveAttribute('aria-expanded', 'true');});+ it('drops the watchers of a press that never became a click when the field unmounts', () => {+ // A press installs one-shot `document` click listeners that end its+ // deferral, from the input and from the wrapper, and a press that ends+ // without a click leaves them in place. They belong to this field, so+ // unmounting takes them along: otherwise the next click anywhere runs this+ // field's reset for a field that is gone. The deferred show is not one of+ // them — the input's own click installs it and delivers it in the same+ // dispatch, measured at three installed and three fired. The Layer gesture+ // counter also watches `click` on the document, in the capture phase and+ // for the life of the page, so only the bubble phase is this field's+ // business. The count comes from running the listener the document really+ // holds, so this fails when a watcher survives, not when the spy misfires.+ const add = document.addEventListener.bind(document);+ const remove = document.removeEventListener.bind(document);+ const isCapture = (options?: boolean | AddEventListenerOptions) =>+ typeof options === 'object'+ ? Boolean(options.capture)+ : Boolean(options);+ let watchers = 0;+ let fired = 0;+ const wrapped = new Map<unknown, EventListener>();+ const added = vi+ .spyOn(document, 'addEventListener')+ .mockImplementation((type, listener, options) => {+ if (type !== 'click' || isCapture(options)) {+ return add(type, listener, options);+ }+ watchers += 1;+ const counting: EventListener = (event, ...rest) => {+ fired += 1;+ if (typeof listener === 'function') {+ listener(event, ...rest);+ }+ };+ wrapped.set(listener, counting);+ return add(type, counting, options);+ });+ const removed = vi+ .spyOn(document, 'removeEventListener')+ .mockImplementation((type, listener, options) =>+ remove(type, wrapped.get(listener) ?? listener, options),+ );++ try {+ const {unmount} = render(+ <DateTimeInput+ label="Meeting"+ value={'2026-03-15T09:00' as ISODateTimeString}+ timeOptionInterval={60}+ onChange={() => {}}+ />,+ );+ fireEvent.pointerDown(screen.getByLabelText('Meeting time'));+ expect(watchers).toBeGreaterThan(0);+ expect(fired).toBe(0);++ unmount();+ fireEvent.click(document.body);+ expect(fired).toBe(0);+ } finally {+ wrapped.clear();+ added.mockRestore();+ removed.mockRestore();+ }+ });+it('activates the first option when the field has a date but no time', () => {// The state you land in after picking a date and no time yet. With no// active option, ArrowUp and Enter both did nothing at all.diff --git a/packages/core/src/DateTimeInput/DateTimeInput.tsx b/packages/core/src/DateTimeInput/DateTimeInput.tsxindex bb273d0c96..1721b29538 100644--- a/packages/core/src/DateTimeInput/DateTimeInput.tsx+++ b/packages/core/src/DateTimeInput/DateTimeInput.tsx@@ -1142,6 +1142,34 @@ function PointerDateTimeField({// it, as BaseTypeahead does.const timePointerActiveRef = useRef(false);+ // These one-shot `document` listeners belong to this field. Unmount removes+ // them. Otherwise the next click runs work for a field that is gone.+ const timeClickWatchersRef = useRef<Set<EventListener> | null>(null);++ const addTimeClickWatcher = useCallback((handler: () => void) => {+ const watchers = timeClickWatchersRef.current ?? new Set<EventListener>();+ timeClickWatchersRef.current = watchers;+ const watcher: EventListener = () => {+ // The `once` option removes the listener from the document. Delete it from+ // the set too, or the set keeps every past press.+ watchers.delete(watcher);+ handler();+ };+ watchers.add(watcher);+ document.addEventListener('click', watcher, {once: true});+ }, []);++ const clearTimeClickWatchers = useCallback(() => {+ for (const watcher of timeClickWatchersRef.current ?? []) {+ document.removeEventListener('click', watcher);+ }+ timeClickWatchersRef.current?.clear();+ }, []);++ // The Effect owns this field's `document` click watchers: its setup acquires+ // nothing, its cleanup releases whatever a press left pending.+ useEffect(() => clearTimeClickWatchers, [clearTimeClickWatchers]);+// Whether the highlight is still the one typing derived, or the user has// since moved it themselves. Enter honours typed text only in the former// case — otherwise the field would commit something other than the option@@ -1150,14 +1178,10 @@ function PointerDateTimeField({const markTimePointerActive = useCallback(() => {timePointerActiveRef.current = true;- document.addEventListener(- 'click',- () => {- timePointerActiveRef.current = false;- },- {once: true},- );- }, []);+ addTimeClickWatcher(() => {+ timePointerActiveRef.current = false;+ });+ }, [addTimeClickWatcher]);// The time list is a manual popover: the browser's light dismiss counts a// press in the time input as outside (a text input cannot be an invoker), so@@ -1309,16 +1333,14 @@ function PointerDateTimeField({// input must hold DOM focus or aria-activedescendant announces nothing.timeInputRef.current?.focus();if (timePointerActiveRef.current) {- document.addEventListener(- 'click',- () =>- requestAnimationFrame(() => timePopover.show({skipAutoFocus: true})),- {once: true},+ addTimeClickWatcher(() =>+ requestAnimationFrame(() => timePopover.show({skipAutoFocus: true})),);} else {timePopover.show({skipAutoFocus: true});}}, [+ addTimeClickWatcher,hasTimeOptions,isEffectivelyDisabled,timePopover,