Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Focus on card search input when adding new card #23309

Merged
merged 4 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/panels/lovelace/editor/card-editor/hui-card-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ export class HuiCardPicker extends LitElement {

private _usedEntities?: string[];

public async focus(): Promise<void> {
const searchInput = this.renderRoot.querySelector("search-input");
if (searchInput) {
searchInput.focus();
} else {
await this.updateComplete;
this.focus();
}
}

private _filterCards = memoizeOne(
(cardElements: CardElement[], filter?: string): CardElement[] => {
if (!filter) {
Expand Down
12 changes: 10 additions & 2 deletions src/panels/lovelace/editor/card-editor/hui-dialog-create-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "@material/mwc-tab/mwc-tab";
import { mdiClose } from "@mdi/js";
import type { CSSResultGroup } from "lit";
import { css, html, LitElement, nothing } from "lit";
import { ifDefined } from "lit/directives/if-defined";
import { customElement, property, state } from "lit/decorators";
import { cache } from "lit/directives/cache";
import { classMap } from "lit/directives/class-map";
Expand Down Expand Up @@ -60,9 +61,15 @@ export class HuiCreateDialogCard

@state() private _currTabIndex = 0;

@state() private _narrow = false;

public async showDialog(params: CreateCardDialogParams): Promise<void> {
this._params = params;

this._narrow = matchMedia(
"all and (max-width: 450px), all and (max-height: 500px)"
).matches;
Comment on lines +69 to +71
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: we should make this a utility function at some point


const containerConfig = findLovelaceContainer(
params.lovelaceConfig,
params.path
Expand Down Expand Up @@ -120,7 +127,7 @@ export class HuiCreateDialogCard
.label=${this.hass!.localize(
"ui.panel.lovelace.editor.cardpicker.by_card"
)}
dialogInitialFocus
dialogInitialFocus=${ifDefined(this._narrow ? "" : undefined)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dialogInitialFocus=${ifDefined(this._narrow ? "" : undefined)}
.dialogInitialFocus=${Boolean(this._narrow)}

I think this is simpler

Copy link
Member

@bramkragten bramkragten Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not possible, the dialog searches for an attribute, not for a property

></mwc-tab>
<mwc-tab
.label=${this.hass!.localize(
Expand All @@ -133,6 +140,7 @@ export class HuiCreateDialogCard
this._currTabIndex === 0
? html`
<hui-card-picker
dialogInitialFocus=${ifDefined(this._narrow ? undefined : "")}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dialogInitialFocus=${ifDefined(this._narrow ? undefined : "")}
.dialogInitialFocus=${Boolean(this._narrow)}

.suggestedCards=${this._params.suggestedCards}
.lovelace=${this._params.lovelaceConfig}
.hass=${this.hass}
Expand All @@ -143,7 +151,7 @@ export class HuiCreateDialogCard
<hui-entity-picker-table
no-label-float
.hass=${this.hass}
.narrow=${true}
narrow
.entities=${this._allEntities(this.hass.states)}
@selected-changed=${this._handleSelectedChanged}
></hui-entity-picker-table>
Expand Down
Loading