diff --git a/src/client/components/ToggleInputCard.ts b/src/client/components/ToggleInputCard.ts index 2cceb5296..b9e10bb03 100644 --- a/src/client/components/ToggleInputCard.ts +++ b/src/client/components/ToggleInputCard.ts @@ -1,4 +1,4 @@ -import { LitElement, html, nothing } from "lit"; +import { LitElement, PropertyValues, html, nothing } from "lit"; import { customElement, property } from "lit/decorators.js"; import { translateText } from "../Utils"; import { CARD_LABEL_CLASS, INPUT_CLASS, cardClass } from "./InputCardStyles"; @@ -28,6 +28,18 @@ export class ToggleInputCard extends LitElement { createRenderRoot() { return this; } + + // Autofocus + select the number input when the card is toggled on. Safe now + // that the input is always mounted (focusing a freshly-inserted one janked). + protected updated(changedProperties: PropertyValues) { + if (!changedProperties.has("checked")) return; + if (changedProperties.get("checked") === false && this.checked) { + const input = this.querySelector("input"); + input?.focus(); + input?.select(); + } + } + private toOptionalNumber( value: number | string | undefined, ): number | undefined { @@ -120,28 +132,32 @@ export class ToggleInputCard extends LitElement { - ${this.checked - ? html` -
- -
- ` - : nothing} + +
+ +
`; }