Skip to content

Commit

Permalink
chore: current working state
Browse files Browse the repository at this point in the history
  • Loading branch information
forsti0506 committed Aug 25, 2022
1 parent 55f3e82 commit 2af210e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/cdk/a11y/key-manager/list-key-manager.ts
Expand Up @@ -52,6 +52,7 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
private _horizontal: 'ltr' | 'rtl' | null;
private _allowedModifierKeys: ListKeyManagerModifierKey[] = [];
private _homeAndEnd = false;
private _pageUpAndDown = {enabled: false, scrollValue: 10};

/**
* Predicate function that can be used to check whether an item should be skipped
Expand Down Expand Up @@ -196,6 +197,16 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
return this;
}

/**
* Configures the key manager to activate the first and last items
* respectively when the Home or End key is pressed.
* @param enabled Whether pressing the Home or End key activates the first/last item.
*/
withPageUpDown(enabled: boolean = true, scrollValue: number = 10): this {
this._pageUpAndDown = {enabled, scrollValue};
return this;
}

/**
* Sets the active item to the item at the index specified.
* @param index The index of the item to be set as active.
Expand Down Expand Up @@ -283,16 +294,16 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
}

case PAGE_UP:
if (isModifierAllowed) {
this.setFirstItemActive();
if (this._pageUpAndDown.enabled && isModifierAllowed) {
this._setActiveItemByIndex(this._activeItemIndex - this._pageUpAndDown.scrollValue, 1);
break;
} else {
return;
}

case PAGE_DOWN:
if (isModifierAllowed) {
this.setLastItemActive();
if (this._pageUpAndDown.enabled && isModifierAllowed) {
this._setActiveItemByIndex(this._activeItemIndex + this._pageUpAndDown.scrollValue, -1);
break;
} else {
return;
Expand Down
3 changes: 3 additions & 0 deletions src/material/select/select.spec.ts
Expand Up @@ -2421,11 +2421,14 @@ describe('MDC-based MatSelect', () => {
.withContext('Expected panel to be scrolled down.')
.toBeGreaterThan(0);

expect(fixture.componentInstance.select._keyManager.activeItemIndex).toBe(20);

dispatchKeyboardEvent(host, 'keydown', PAGE_UP);
fixture.detectChanges();

// 8px is the top padding of the panel.
expect(panel.scrollTop).withContext('Expected panel to be scrolled to the top').toBe(8);
expect(fixture.componentInstance.select._keyManager.activeItemIndex).toBe(10);
}));

it('should scroll to the bottom of the panel when pressing PAGE_DOWN', fakeAsync(() => {
Expand Down
1 change: 1 addition & 0 deletions src/material/select/select.ts
Expand Up @@ -914,6 +914,7 @@ export abstract class _MatSelectBase<C>
.withVerticalOrientation()
.withHorizontalOrientation(this._isRtl() ? 'rtl' : 'ltr')
.withHomeAndEnd()
.withPageUpDown()
.withAllowedModifierKeys(['shiftKey']);

this._keyManager.tabOut.pipe(takeUntil(this._destroy)).subscribe(() => {
Expand Down

0 comments on commit 2af210e

Please sign in to comment.