Skip to content

Commit

Permalink
Intercept default search shortcut and focus our search input on data …
Browse files Browse the repository at this point in the history
…table pages (#23209)

Intercept default search shortcut on data table pages
  • Loading branch information
jpbede authored Dec 11, 2024
1 parent ce5ce37 commit bf624f5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
11 changes: 10 additions & 1 deletion src/layouts/hass-tabs-subpage-data-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ import type { HomeAssistant, Route } from "../types";
import "./hass-tabs-subpage";
import type { PageNavigation } from "./hass-tabs-subpage";
import { showDataTableSettingsDialog } from "../components/data-table/show-dialog-data-table-settings";
import { KeyboardShortcutMixin } from "../mixins/keyboard-shortcut-mixin";

@customElement("hass-tabs-subpage-data-table")
export class HaTabsSubpageDataTable extends LitElement {
export class HaTabsSubpageDataTable extends KeyboardShortcutMixin(LitElement) {
@property({ attribute: false }) public hass!: HomeAssistant;

@property({ attribute: false }) public localizeFunc?: LocalizeFunc;
Expand Down Expand Up @@ -189,6 +190,14 @@ export class HaTabsSubpageDataTable extends LitElement {

@query("#sort-by-menu") private _sortByMenu!: HaMenu;

@query("search-input-outlined") private _searchInput!: HTMLElement;

protected supportedShortcuts(): SupportedShortcuts {
return {
f: () => this._searchInput.focus(),
};
}

private _showPaneController = new ResizeController(this, {
callback: (entries) => entries[0]?.contentRect.width > 750,
});
Expand Down
19 changes: 14 additions & 5 deletions src/mixins/keyboard-shortcut-mixin.ts
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 {};
}
};
6 changes: 4 additions & 2 deletions src/panels/config/automation/ha-automation-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,10 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
ev.detail.callback(this._config);
}

protected handleKeyboardSave() {
this._saveAutomation();
protected supportedShortcuts(): SupportedShortcuts {
return {
s: () => this._saveAutomation(),
};
}

static get styles(): CSSResultGroup {
Expand Down
6 changes: 4 additions & 2 deletions src/panels/config/scene/ha-scene-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1215,8 +1215,10 @@ export class HaSceneEditor extends SubscribeMixin(
}
}

protected handleKeyboardSave() {
this._saveScene();
protected supportedShortcuts(): SupportedShortcuts {
return {
s: () => this._saveScene(),
};
}

private get _sceneAreaIdWithUpdates(): string | undefined | null {
Expand Down
6 changes: 4 additions & 2 deletions src/panels/config/script/ha-script-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,8 +818,10 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
}
}

protected handleKeyboardSave() {
this._saveScript();
protected supportedShortcuts(): SupportedShortcuts {
return {
s: () => this._saveScript(),
};
}

static get styles(): CSSResultGroup {
Expand Down

0 comments on commit bf624f5

Please sign in to comment.