Skip to content
This repository has been archived by the owner on Jul 6, 2021. It is now read-only.

Commit

Permalink
fix: allow overriding protected methods
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan committed Nov 20, 2019
1 parent a97527e commit 852bce2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"import/extensions": "off",
"import/prefer-default-export": "off",
"class-methods-use-this": "off",
"max-classes-per-file": "off",
"no-console": [ "error", { "allow": ["warn"] } ],
"no-plusplus": "off",
"no-underscore-dangle": "off"
Expand Down
27 changes: 21 additions & 6 deletions packages/roving-tabindex-mixin/roving-tabindex-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,28 @@ export interface RovingTabindexInterface {
focused: Element | null;
}

type ItemCondition = (item: Element) => boolean;
// Using a dumb `class` instead of `interface` or `type`
// to document protected methods that can be overridden
// https://github.com/microsoft/TypeScript/issues/25163
export abstract class RovingTabIndexClass extends LitElement {
protected _onKeydown?(event: KeyboardEvent): void;

protected _isNext?(key: string): boolean;

protected _isPrev?(key: string): boolean;

protected _focus?(idx: number): void;

protected _items?: HTMLElement[] = [];
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type LitBase = new (...args: any[]) => LitElement;
type LitBase = new (...args: any[]) => RovingTabIndexClass;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type LitRovingTabindex = new (...args: any[]) => LitElement & RovingTabindexInterface;
type LitRovingTabindex = new (...args: any[]) => RovingTabIndexClass & RovingTabindexInterface;

type ItemCondition = (item: Element) => boolean;

const isEnabled: ItemCondition = (item: Element) => !item.hasAttribute('disabled');

Expand All @@ -20,8 +35,6 @@ export const RovingTabindexMixin = <T extends LitBase>(base: T): LitRovingTabind
@property({ attribute: false, hasChanged: () => true })
protected _items: HTMLElement[] = [];

private _boundOnkeydown = this._onKeydown.bind(this);

focus() {
const first =
(this.querySelector('[tabindex="0"]') as HTMLElement) ||
Expand Down Expand Up @@ -58,7 +71,9 @@ export const RovingTabindexMixin = <T extends LitBase>(base: T): LitRovingTabind
protected firstUpdated(props: PropertyValues) {
super.firstUpdated(props);

this.addEventListener('keydown', this._boundOnkeydown);
this.addEventListener('keydown', (event: KeyboardEvent) => {
this._onKeydown(event);
});

const slot = this.renderRoot.querySelector('slot');
if (slot) {
Expand Down

0 comments on commit 852bce2

Please sign in to comment.