From f0512b1341ad63dccf99f97b9203e2dec80d3e14 Mon Sep 17 00:00:00 2001 From: adityajai119 Date: Fri, 20 Nov 2026 21:51:16 +0530 Subject: [PATCH] fix(picker-column): focus options in setFocus #30563 removed the separate assistiveFocusable element and moved the slider role and keyboard handling onto scrollEl (.picker-opts), but left setFocus() and the scroll aria-valuetext update reading assistiveFocusable. Point both at scrollEl and remove the unused field so setFocus() focuses the column and enters keyboard input mode again. resolves #30634 --- .../picker-column/picker-column.tsx | 7 +- .../picker-column/test/picker-column.spec.tsx | 71 +++++++++++++++++++ .../picker/test/keyboard-entry/picker.e2e.ts | 25 +++---- 3 files changed, 84 insertions(+), 19 deletions(-) diff --git a/core/src/components/picker-column/picker-column.tsx b/core/src/components/picker-column/picker-column.tsx index ea56f0711b2..5a8653ca7c9 100644 --- a/core/src/components/picker-column/picker-column.tsx +++ b/core/src/components/picker-column/picker-column.tsx @@ -31,7 +31,6 @@ export class PickerColumn implements ComponentInterface { private isColumnVisible = false; private parentEl?: HTMLIonPickerElement | null; private canExitInputMode = true; - private assistiveFocusable?: HTMLElement; private updateValueTextOnScroll = false; private scrollEndTimeout?: ReturnType; private centeredOption?: HTMLIonPickerColumnOptionElement; @@ -232,8 +231,8 @@ export class PickerColumn implements ComponentInterface { */ @Method() async setFocus() { - if (this.assistiveFocusable) { - this.assistiveFocusable.focus(); + if (this.scrollEl) { + this.scrollEl.focus(); } } @@ -633,7 +632,7 @@ export class PickerColumn implements ComponentInterface { * again before announcing the new value. The correct valuetext will be set on render. */ if (this.updateValueTextOnScroll) { - this.assistiveFocusable?.setAttribute('aria-valuetext', this.getOptionValueText(newActiveElement)); + scrollEl.setAttribute('aria-valuetext', this.getOptionValueText(newActiveElement)); } this.centeredOption = newActiveElement; diff --git a/core/src/components/picker-column/test/picker-column.spec.tsx b/core/src/components/picker-column/test/picker-column.spec.tsx index 479daba5352..a4136845d6b 100644 --- a/core/src/components/picker-column/test/picker-column.spec.tsx +++ b/core/src/components/picker-column/test/picker-column.spec.tsx @@ -1,5 +1,6 @@ import { h } from '@stencil/core'; import { newSpecPage } from '@stencil/core/testing'; +import { doc } from '@utils/browser'; import { PickerColumnOption } from '../../picker-column-option/picker-column-option'; import { PickerColumn } from '../picker-column'; @@ -111,4 +112,74 @@ describe('picker-column', () => { expect(pickerOpts.getAttribute('aria-valuetext')).toBe('My Text'); }); + + it('should focus the scrollable options element when setFocus is called', async () => { + const page = await newSpecPage({ + components: [PickerColumn], + template: () => , + }); + + const pickerCol = page.body.querySelector('ion-picker-column')!; + const pickerOpts = pickerCol.shadowRoot!.querySelector('.picker-opts')!; + const focusSpy = jest.spyOn(pickerOpts, 'focus'); + + await pickerCol.setFocus(); + + expect(focusSpy).toHaveBeenCalledTimes(1); + }); + + it('should update aria-valuetext on the scrollable options element while scrolling', async () => { + let ioCallback: IntersectionObserverCallback | undefined; + (global.IntersectionObserver as jest.Mock).mockImplementation((cb: IntersectionObserverCallback) => { + ioCallback = cb; + return { + observe: () => null, + unobserve: () => null, + disconnect: () => null, + }; + }); + + const originalElementsFromPoint = doc!.elementsFromPoint; + const originalRaf = global.requestAnimationFrame; + + const page = await newSpecPage({ + components: [PickerColumn, PickerColumnOption], + template: () => ( + + First + Second + + ), + }); + + jest.useFakeTimers(); + global.requestAnimationFrame = ((cb: FrameRequestCallback) => setTimeout(cb, 16)) as any; + + try { + const pickerCol = page.body.querySelector('ion-picker-column')!; + const pickerOpts = pickerCol.shadowRoot!.querySelector('.picker-opts')!; + pickerOpts.scroll = jest.fn(); + const options = pickerCol.querySelectorAll('ion-picker-column-option'); + + let centeredOption: Element = options[0]; + doc!.elementsFromPoint = jest.fn(() => [centeredOption]); + + ioCallback!([{ isIntersecting: true } as IntersectionObserverEntry], {} as IntersectionObserver); + jest.advanceTimersByTime(20); + + pickerOpts.dispatchEvent(new Event('scroll')); + jest.advanceTimersByTime(300); + + centeredOption = options[1]; + pickerOpts.dispatchEvent(new Event('scroll')); + jest.advanceTimersByTime(20); + + expect(pickerCol.value).toBe(1); + expect(pickerOpts.getAttribute('aria-valuetext')).toBe('Second'); + } finally { + global.requestAnimationFrame = originalRaf; + doc!.elementsFromPoint = originalElementsFromPoint; + jest.useRealTimers(); + } + }); }); diff --git a/core/src/components/picker/test/keyboard-entry/picker.e2e.ts b/core/src/components/picker/test/keyboard-entry/picker.e2e.ts index 35a66caa918..c49fdd54f99 100644 --- a/core/src/components/picker/test/keyboard-entry/picker.e2e.ts +++ b/core/src/components/picker/test/keyboard-entry/picker.e2e.ts @@ -7,7 +7,11 @@ import type { E2ELocator } from '@utils/test/playwright/page/utils/locator'; */ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => { test.describe(title('picker: keyboard entry'), () => { - test('should scroll to and update the value prop for a single column', async ({ page }) => { + test('should scroll to and update the value prop for a single column', async ({ page }, testInfo) => { + testInfo.annotations.push({ + type: 'issue', + description: 'https://github.com/ionic-team/ionic-framework/issues/30634', + }); await page.setContent( ` @@ -38,12 +42,10 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => ); const column = page.locator('ion-picker-column'); - - const colShadowRoot = await column.evaluateHandle((el) => el.shadowRoot); - const columnPickerOpts = await colShadowRoot.evaluateHandle((root) => root?.querySelector('.picker-opts')); - const ionChange = await page.spyOnEvent('ionChange'); - await columnPickerOpts.evaluate((el) => el && (el as HTMLElement).focus()); + + await column.evaluate((el: HTMLIonPickerColumnElement) => el.setFocus()); + await expect(column).toBeFocused(); await page.keyboard.press('Digit2'); @@ -106,11 +108,8 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => const firstIonChange = await (firstColumn as E2ELocator).spyOnEvent('ionChange'); const secondIonChange = await (secondColumn as E2ELocator).spyOnEvent('ionChange'); - const firstColShadowRoot = await firstColumn.evaluateHandle((el) => el.shadowRoot); - const columnPickerOpts = await firstColShadowRoot.evaluateHandle((root) => root?.querySelector('.picker-opts')); - // Focus first column - await columnPickerOpts.evaluate((el) => el && (el as HTMLElement).focus()); + await firstColumn.evaluate((el: HTMLIonPickerColumnElement) => el.setFocus()); await page.keyboard.press('Digit2'); @@ -161,12 +160,8 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => ); const column = page.locator('ion-picker-column'); - - const colShadowRoot = await column.evaluateHandle((el) => el.shadowRoot); - const columnPickerOpts = await colShadowRoot.evaluateHandle((root) => root?.querySelector('.picker-opts')); - const ionChange = await page.spyOnEvent('ionChange'); - await columnPickerOpts.evaluate((el) => el && (el as HTMLElement).focus()); + await column.evaluate((el: HTMLIonPickerColumnElement) => el.setFocus()); await page.keyboard.press('Digit0');