Implement show_empty functionality for the markdown card (#21379)

* Implement show_empty functionality

* Implement show_empty functionality

* import fireEvent

* Order imports correctly

* Lint with prettier

* Unhide card when appropriate

* only run show code if card is hidden

* Commit coderabbit code cleanup

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* linting

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
illuzn 2024-07-31 18:28:45 +09:30 committed by GitHub
parent 0aa25dbed9
commit dbd84901f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import {
} from "lit";
import { customElement, property, state } from "lit/decorators";
import { classMap } from "lit/directives/class-map";
import { fireEvent } from "../../../common/dom/fire_event";
import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element";
import "../../../components/ha-card";
import "../../../components/ha-markdown";
@ -113,7 +114,15 @@ export class HuiMarkdownCard extends LitElement implements LovelaceCard {
if (changedProps.has("_config")) {
this._tryConnect();
}
const shouldBeHidden =
this._templateResult &&
this._config.show_empty === false &&
this._templateResult.result.length === 0;
if (shouldBeHidden !== this.hidden) {
this.style.display = shouldBeHidden ? "none" : "block";
this.toggleAttribute("hidden", shouldBeHidden);
fireEvent(this, "card-visibility-changed", { value: !shouldBeHidden });
}
const oldHass = changedProps.get("hass") as HomeAssistant | undefined;
const oldConfig = changedProps.get("_config") as
| MarkdownCardConfig

View File

@ -316,6 +316,7 @@ export interface MarkdownCardConfig extends LovelaceCardConfig {
card_size?: number;
entity_ids?: string | string[];
theme?: string;
show_empty?: boolean;
}
export interface MediaControlCardConfig extends LovelaceCardConfig {