Fix height of card picker (#6274)

This commit is contained in:
Bram Kragten 2020-06-29 22:18:45 +02:00 committed by GitHub
parent e8996063dd
commit 31a0c53855
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,6 +33,7 @@ import { getCardStubConfig } from "../get-card-stub-config";
import { CardPickTarget, Card } from "../types"; import { CardPickTarget, Card } from "../types";
import { coreCards } from "../lovelace-cards"; import { coreCards } from "../lovelace-cards";
import { styleMap } from "lit-html/directives/style-map"; import { styleMap } from "lit-html/directives/style-map";
import "../../../../components/ha-circular-progress";
interface CardElement { interface CardElement {
card: Card; card: Card;
@ -49,19 +50,21 @@ export class HuiCardPicker extends LitElement {
public cardPicked?: (cardConf: LovelaceCardConfig) => void; public cardPicked?: (cardConf: LovelaceCardConfig) => void;
private _filter?: string; @property() private _filter = "";
private _unusedEntities?: string[]; private _unusedEntities?: string[];
private _usedEntities?: string[]; private _usedEntities?: string[];
private _width?: number; @property() private _width?: number;
private _height?: number; @property() private _height?: number;
private _filterCards = memoizeOne( private _filterCards = memoizeOne(
(cardElements: CardElement[], filter?: string): CardElement[] => { (cardElements: CardElement[], filter?: string): CardElement[] => {
if (filter) { if (!filter) {
return cardElements;
}
let cards = cardElements.map( let cards = cardElements.map(
(cardElement: CardElement) => cardElement.card (cardElement: CardElement) => cardElement.card
); );
@ -73,12 +76,10 @@ export class HuiCardPicker extends LitElement {
}; };
const fuse = new Fuse(cards, options); const fuse = new Fuse(cards, options);
cards = fuse.search(filter).map((result) => result.item); cards = fuse.search(filter).map((result) => result.item);
cardElements = cardElements.filter((cardElement: CardElement) => return cardElements.filter((cardElement: CardElement) =>
cards.includes(cardElement.card) cards.includes(cardElement.card)
); );
} }
return cardElements;
}
); );
protected render(): TemplateResult { protected render(): TemplateResult {
@ -98,9 +99,10 @@ export class HuiCardPicker extends LitElement {
@value-changed=${this._handleSearchChange} @value-changed=${this._handleSearchChange}
></search-input> ></search-input>
<div <div
id="content"
style=${styleMap({ style=${styleMap({
width: `${this._width}px`, width: this._width ? `${this._width}px` : "auto",
height: `${this._height}px`, height: this._height ? `${this._height}px` : "auto",
})} })}
> >
<div class="cards-container"> <div class="cards-container">
@ -165,24 +167,6 @@ export class HuiCardPicker extends LitElement {
this._loadCards(); this._loadCards();
} }
protected updated(changedProps) {
super.updated(changedProps);
// Store the width and height so that when we search, box doesn't jump
const div = this.shadowRoot!.querySelector("div")!;
if (!this._width) {
const width = div.clientWidth;
if (width) {
this._width = width;
}
}
if (!this._height) {
const height = div.clientHeight;
if (height) {
this._height = height;
}
}
}
private _loadCards() { private _loadCards() {
let cards: Card[] = coreCards.map((card: Card) => ({ let cards: Card[] = coreCards.map((card: Card) => ({
name: this.hass!.localize( name: this.hass!.localize(
@ -218,8 +202,30 @@ export class HuiCardPicker extends LitElement {
} }
private _handleSearchChange(ev: CustomEvent) { private _handleSearchChange(ev: CustomEvent) {
this._filter = ev.detail.value; const value = ev.detail.value;
this.requestUpdate();
if (!value) {
// Reset when we no longer filter
this._width = undefined;
this._height = undefined;
} else if (!this._width || !this._height) {
// Save height and width so the dialog doesn't jump while searching
const div = this.shadowRoot!.getElementById("content");
if (div && !this._width) {
const width = div.clientWidth;
if (width) {
this._width = width;
}
}
if (div && !this._height) {
const height = div.clientHeight;
if (height) {
this._height = height;
}
}
}
this._filter = value;
} }
static get styles(): CSSResult[] { static get styles(): CSSResult[] {