Skip to content

Commit

Permalink
Add support for helper text in form boolean (#22711)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramkragten authored Nov 7, 2024
1 parent 786b9ee commit 46f0e02
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions gallery/src/pages/components/ha-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ class DemoHaForm extends LitElement {
.computeError=${(error) => translations[error] || error}
.computeLabel=${(schema) =>
translations[schema.name] || schema.name}
.computeHelper=${() => "Helper text"}
@value-changed=${(e) => {
this.data[idx] = e.detail.value;
this.requestUpdate();
Expand Down
34 changes: 32 additions & 2 deletions src/components/ha-form/ha-form-boolean.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "@material/mwc-formfield";
import type { TemplateResult } from "lit";
import { html, LitElement } from "lit";
import type { CSSResultGroup, TemplateResult } from "lit";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property, query } from "lit/decorators";
import { fireEvent } from "../../common/dom/fire_event";
import type {
Expand All @@ -19,6 +19,8 @@ export class HaFormBoolean extends LitElement implements HaFormElement {

@property() public label!: string;

@property() public helper?: string;

@property({ type: Boolean }) public disabled = false;

@query("ha-checkbox", true) private _input?: HTMLElement;
Expand All @@ -37,6 +39,12 @@ export class HaFormBoolean extends LitElement implements HaFormElement {
.disabled=${this.disabled}
@change=${this._valueChanged}
></ha-checkbox>
<span slot="label">
<p class="primary">${this.label}</p>
${this.helper
? html`<p class="secondary">${this.helper}</p>`
: nothing}
</span>
</mwc-formfield>
`;
}
Expand All @@ -46,6 +54,28 @@ export class HaFormBoolean extends LitElement implements HaFormElement {
value: (ev.target as HaCheckbox).checked,
});
}

static get styles(): CSSResultGroup {
return css`
ha-formfield {
display: flex;
min-height: 56px;
align-items: center;
--mdc-typography-body2-font-size: 1em;
}
p {
margin: 0;
}
.secondary {
direction: var(--direction);
padding-top: 4px;
box-sizing: border-box;
color: var(--secondary-text-color);
font-size: 0.875rem;
font-weight: var(--mdc-typography-body2-font-weight, 400);
}
`;
}
}

declare global {
Expand Down

0 comments on commit 46f0e02

Please sign in to comment.