Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions core/src/components/picker-column/picker-column.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof setTimeout>;
private centeredOption?: HTMLIonPickerColumnOptionElement;
Expand Down Expand Up @@ -232,8 +231,8 @@ export class PickerColumn implements ComponentInterface {
*/
@Method()
async setFocus() {
if (this.assistiveFocusable) {
this.assistiveFocusable.focus();
if (this.scrollEl) {
this.scrollEl.focus();
}
}

Expand Down Expand Up @@ -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;
Expand Down
71 changes: 71 additions & 0 deletions core/src/components/picker-column/test/picker-column.spec.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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: () => <ion-picker-column></ion-picker-column>,
});

const pickerCol = page.body.querySelector('ion-picker-column')!;
const pickerOpts = pickerCol.shadowRoot!.querySelector<HTMLElement>('.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: () => (
<ion-picker-column value={1}>
<ion-picker-column-option value={1}>First</ion-picker-column-option>
<ion-picker-column-option value={2}>Second</ion-picker-column-option>
</ion-picker-column>
),
});

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<HTMLElement>('.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();
}
});
});
25 changes: 10 additions & 15 deletions core/src/components/picker/test/keyboard-entry/picker.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
`
<ion-picker>
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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');

Expand Down
Loading