Skip to content

Commit

Permalink
feat(navManager): add focusFirstLeft/focusFirstRight and fix navManag…
Browse files Browse the repository at this point in the history
…er e2e test on Mac
  • Loading branch information
divdavem committed Jan 23, 2024
1 parent 6fd64e9 commit 873ad1d
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 175 deletions.
2 changes: 2 additions & 0 deletions angular/demo/src/app/samples/navmanager/navmanager.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export class NavmanagerLineComponent {
ArrowRight: this.navManager.focusRight,
Home: this.navManager.focusFirst,
End: this.navManager.focusLast,
'Meta+ArrowLeft': this.navManager.focusFirstLeft,
'Meta+ArrowRight': this.navManager.focusFirstRight,
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export class NavmanagerLineComponent {
ArrowRight: this.navManager.focusRight,
Home: this.navManager.focusFirst,
End: this.navManager.focusLast,
'Meta+ArrowLeft': this.navManager.focusFirstLeft,
'Meta+ArrowRight': this.navManager.focusFirstRight,
},
selector: (divElement) => divElement.querySelectorAll('input,span'),
};
Expand Down
347 changes: 181 additions & 166 deletions core/src/services/navManager.spec.ts

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions core/src/services/navManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ export const createNavManager = () => {
const focusLast = ({event}: {event?: KeyboardEvent} = {}) => preventDefaultIfRelevant(focusIndex(elementsInDomOrder$().length - 1, -1), event);
const focusLeft = (...args: Parameters<typeof focusNext>) => (ancestorDirection() === 'rtl' ? focusNext : focusPrevious)(...args);
const focusRight = (...args: Parameters<typeof focusNext>) => (ancestorDirection() === 'rtl' ? focusPrevious : focusNext)(...args);
const focusFirstLeft = (...args: Parameters<typeof focusLast>) => (ancestorDirection() === 'rtl' ? focusLast : focusFirst)(...args);
const focusFirstRight = (...args: Parameters<typeof focusLast>) => (ancestorDirection() === 'rtl' ? focusFirst : focusLast)(...args);

const navManager = {
elementsInDomOrder$,
Expand All @@ -208,6 +210,8 @@ export const createNavManager = () => {
focusPrevious,
focusNext,
focusFirst,
focusFirstLeft,
focusFirstRight,
focusLast,
focusLeft,
focusRight,
Expand Down
22 changes: 15 additions & 7 deletions e2e/navmanager/navmanager.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ for (const sample of ['navmanager', 'navmanagerwithselector']) {
];

for (const {name, backwardKey, forwardKey, getLocator, keyPresses} of useCases) {
test(name, async ({page}) => {
test(name, async ({page, browserName}) => {
const navManagerPO = new NavManagerPO(page);
const navManagerLine = new NavManagerLinePO(getLocator(navManagerPO));
await navManagerLine.waitLoaded();
Expand Down Expand Up @@ -99,21 +99,29 @@ for (const sample of ['navmanager', 'navmanagerwithselector']) {
await expect.poll(() => getCaretPosition(page)).toEqual([0, 0]);
});

await test.step('fast forward', async () => {
await page.keyboard.press('End');
if (process.platform === 'darwin' && browserName === 'firefox' && backwardKey === 'ArrowRight') {
// Firefox has a bug with RTL on Mac: Meta+ArrowLeft and Meta+ArrowRight are swapped
// skip this test in this case
return;
}
const useMeta = process.platform === 'darwin' && browserName != 'chromium';
const fastForwardKey = useMeta ? `Meta+${forwardKey}` : 'End';
await test.step(`fast forward (${fastForwardKey})`, async () => {
await page.keyboard.press(fastForwardKey);
await expect.poll(() => getCaretPosition(page)).toEqual([value.length, value.length]);
await expect(currentItem).toBeFocused();
await page.keyboard.press('End');
await page.keyboard.press(fastForwardKey);
currentItem = items[4];
await expect(currentItem).toBeFocused();
await expect.poll(() => getCaretPosition(page)).toEqual([value.length, value.length]);
});

await test.step('fast backward', async () => {
await page.keyboard.press('Home');
const fastBackwardKey = useMeta ? `Meta+${backwardKey}` : 'Home';
await test.step(`fast backward (${fastBackwardKey})`, async () => {
await page.keyboard.press(fastBackwardKey);
await expect.poll(() => getCaretPosition(page)).toEqual([0, 0]);
await expect(currentItem).toBeFocused();
await page.keyboard.press('Home');
await page.keyboard.press(fastBackwardKey);
currentItem = items[0];
await expect(currentItem).toBeFocused();
await expect.poll(() => getCaretPosition(page)).toEqual([0, 0]);
Expand Down
2 changes: 2 additions & 0 deletions react/demo/src/app/samples/navmanager/Navmanager.route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const NavmanagerSampleLine = ({text}: {text: string}) => {
ArrowRight: navManager.focusRight,
Home: navManager.focusFirst,
End: navManager.focusLast,
'Meta+ArrowLeft': navManager.focusFirstLeft,
'Meta+ArrowRight': navManager.focusFirstRight,
},
}),
[navManager],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const NavmanagerSampleLine = ({text}: {text: string}) => {
ArrowRight: navManager.focusRight,
Home: navManager.focusFirst,
End: navManager.focusLast,
'Meta+ArrowLeft': navManager.focusFirstLeft,
'Meta+ArrowRight': navManager.focusFirstRight,
},
selector: (divElement) => divElement.querySelectorAll('input,span'),
}),
Expand Down
4 changes: 3 additions & 1 deletion svelte/demo/src/app/samples/navManager/NavManagerLine.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
import type {NavManagerItemConfig} from '@agnos-ui/svelte/services/navManager';
import {createNavManager} from '@agnos-ui/svelte/services/navManager';
const {directive, focusLeft, focusRight, focusFirst, focusLast} = createNavManager();
const {directive, focusLeft, focusRight, focusFirst, focusLast, focusFirstLeft, focusFirstRight} = createNavManager();
const navManagerConfig: NavManagerItemConfig = {
keys: {
ArrowLeft: focusLeft,
ArrowRight: focusRight,
Home: focusFirst,
End: focusLast,
'Meta+ArrowLeft': focusFirstLeft,
'Meta+ArrowRight': focusFirstRight,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
import type {NavManagerItemConfig} from '@agnos-ui/svelte/services/navManager';
import {createNavManager} from '@agnos-ui/svelte/services/navManager';
const {directive, focusLeft, focusRight, focusFirst, focusLast} = createNavManager();
const {directive, focusLeft, focusRight, focusFirst, focusLast, focusFirstLeft, focusFirstRight} = createNavManager();
const navManagerConfig: NavManagerItemConfig = {
keys: {
ArrowLeft: focusLeft,
ArrowRight: focusRight,
Home: focusFirst,
End: focusLast,
'Meta+ArrowLeft': focusFirstLeft,
'Meta+ArrowRight': focusFirstRight,
},
selector: (divElement) => divElement.querySelectorAll('input,span'),
};
Expand Down

0 comments on commit 873ad1d

Please sign in to comment.