-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Intercept default search shortcut and focus our search input on data …
…table pages (#23209) Intercept default search shortcut on data table pages
- Loading branch information
Showing
5 changed files
with
36 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,35 @@ | ||
import type { LitElement } from "lit"; | ||
import type { Constructor } from "../types"; | ||
|
||
declare global { | ||
interface SupportedShortcuts { | ||
[key: string]: () => void; | ||
} | ||
} | ||
|
||
export const KeyboardShortcutMixin = <T extends Constructor<LitElement>>( | ||
superClass: T | ||
) => | ||
class extends superClass { | ||
private _keydownEvent = (event: KeyboardEvent) => { | ||
if ((event.ctrlKey || event.metaKey) && event.key === "s") { | ||
const supportedShortcuts = this.supportedShortcuts(); | ||
if ((event.ctrlKey || event.metaKey) && event.key in supportedShortcuts) { | ||
event.preventDefault(); | ||
this.handleKeyboardSave(); | ||
supportedShortcuts[event.key](); | ||
} | ||
}; | ||
|
||
public connectedCallback() { | ||
super.connectedCallback(); | ||
this.addEventListener("keydown", this._keydownEvent); | ||
window.addEventListener("keydown", this._keydownEvent); | ||
} | ||
|
||
public disconnectedCallback() { | ||
this.removeEventListener("keydown", this._keydownEvent); | ||
window.removeEventListener("keydown", this._keydownEvent); | ||
super.disconnectedCallback(); | ||
} | ||
|
||
protected handleKeyboardSave() {} | ||
protected supportedShortcuts(): SupportedShortcuts { | ||
return {}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters