Add type expandable for ha-form (#14197)

* Add type group for ha-form

* rename to expandable

* Add aria level

* apply suggestions
This commit is contained in:
Paul Bottein 2022-10-28 17:49:59 +02:00 committed by GitHub
parent 449c1f2469
commit 68e94d7222
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 173 additions and 127 deletions

View File

@ -0,0 +1,87 @@
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property } from "lit/decorators";
import type { HomeAssistant } from "../../types";
import "./ha-form";
import type {
HaFormDataContainer,
HaFormElement,
HaFormExpandableSchema,
HaFormSchema,
} from "./types";
@customElement("ha-form-expandable")
export class HaFormExpendable extends LitElement implements HaFormElement {
@property({ attribute: false }) public hass!: HomeAssistant;
@property({ attribute: false }) public data!: HaFormDataContainer;
@property({ attribute: false }) public schema!: HaFormExpandableSchema;
@property({ type: Boolean }) public disabled = false;
@property() public computeLabel?: (
schema: HaFormSchema,
data?: HaFormDataContainer
) => string;
@property() public computeHelper?: (schema: HaFormSchema) => string;
protected render(): TemplateResult {
return html`
<ha-expansion-panel outlined .expanded=${Boolean(this.schema.expanded)}>
<div
slot="header"
role="heading"
aria-level=${this.schema.headingLevel?.toString() ?? "3"}
>
${this.schema.icon
? html` <ha-icon .icon=${this.schema.icon}></ha-icon> `
: this.schema.iconPath
? html` <ha-svg-icon .path=${this.schema.iconPath}></ha-svg-icon> `
: null}
${this.schema.title}
</div>
<div class="content">
<ha-form
.hass=${this.hass}
.data=${this.data}
.schema=${this.schema.schema}
.disabled=${this.disabled}
.computeLabel=${this.computeLabel}
.computeHelper=${this.computeHelper}
></ha-form>
</div>
</ha-expansion-panel>
`;
}
static get styles(): CSSResultGroup {
return css`
:host {
display: flex !important;
flex-direction: column;
}
:host ha-form {
display: block;
}
.content {
padding: 12px;
}
ha-expansion-panel {
display: block;
--expansion-panel-content-padding: 0;
border-radius: 6px;
}
ha-svg-icon,
ha-icon {
color: var(--secondary-text-color);
}
`;
}
}
declare global {
interface HTMLElementTagNameMap {
"ha-form-expandable": HaFormExpendable;
}
}

View File

@ -9,6 +9,7 @@ import "./ha-form-boolean";
import "./ha-form-constant"; import "./ha-form-constant";
import "./ha-form-float"; import "./ha-form-float";
import "./ha-form-grid"; import "./ha-form-grid";
import "./ha-form-expandable";
import "./ha-form-integer"; import "./ha-form-integer";
import "./ha-form-multi_select"; import "./ha-form-multi_select";
import "./ha-form-positive_time_period_dict"; import "./ha-form-positive_time_period_dict";

View File

@ -12,7 +12,8 @@ export type HaFormSchema =
| HaFormMultiSelectSchema | HaFormMultiSelectSchema
| HaFormTimeSchema | HaFormTimeSchema
| HaFormSelector | HaFormSelector
| HaFormGridSchema; | HaFormGridSchema
| HaFormExpandableSchema;
export interface HaFormBaseSchema { export interface HaFormBaseSchema {
name: string; name: string;
@ -34,6 +35,17 @@ export interface HaFormGridSchema extends HaFormBaseSchema {
schema: readonly HaFormSchema[]; schema: readonly HaFormSchema[];
} }
export interface HaFormExpandableSchema extends HaFormBaseSchema {
type: "expandable";
name: "";
title: string;
icon?: string;
iconPath?: string;
expanded?: boolean;
headingLevel?: 1 | 2 | 3 | 4 | 5 | 6;
schema: readonly HaFormSchema[];
}
export interface HaFormSelector extends HaFormBaseSchema { export interface HaFormSelector extends HaFormBaseSchema {
type?: never; type?: never;
selector: Selector; selector: Selector;
@ -86,7 +98,9 @@ export interface HaFormTimeSchema extends HaFormBaseSchema {
export type SchemaUnion< export type SchemaUnion<
SchemaArray extends readonly HaFormSchema[], SchemaArray extends readonly HaFormSchema[],
Schema = SchemaArray[number] Schema = SchemaArray[number]
> = Schema extends HaFormGridSchema ? SchemaUnion<Schema["schema"]> : Schema; > = Schema extends HaFormGridSchema | HaFormExpandableSchema
? SchemaUnion<Schema["schema"]>
: Schema;
export interface HaFormDataContainer { export interface HaFormDataContainer {
[key: string]: HaFormData; [key: string]: HaFormData;

View File

@ -9,7 +9,6 @@ import { fireEvent } from "../../../../common/dom/fire_event";
import { computeDomain } from "../../../../common/entity/compute_domain"; import { computeDomain } from "../../../../common/entity/compute_domain";
import { domainIcon } from "../../../../common/entity/domain_icon"; import { domainIcon } from "../../../../common/entity/domain_icon";
import { capitalizeFirstLetter } from "../../../../common/string/capitalize-first-letter"; import { capitalizeFirstLetter } from "../../../../common/string/capitalize-first-letter";
import { LocalizeFunc } from "../../../../common/translations/localize";
import "../../../../components/ha-form/ha-form"; import "../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../components/ha-form/types"; import type { SchemaUnion } from "../../../../components/ha-form/types";
import type { HomeAssistant } from "../../../../types"; import type { HomeAssistant } from "../../../../types";
@ -45,50 +44,76 @@ export class HuiTileCardEditor
this._config = config; this._config = config;
} }
private _mainSchema = [{ name: "entity", selector: { entity: {} } }] as const; private _schema = memoizeOne(
(entity: string, icon?: string, entityState?: HassEntity) =>
private _appearanceSchema = memoizeOne(
(
localize: LocalizeFunc,
entity: string,
icon?: string,
entityState?: HassEntity
) =>
[ [
{ name: "entity", selector: { entity: {} } },
{ {
name: "", name: "",
type: "grid", type: "expandable",
iconPath: mdiPalette,
title: this.hass!.localize(
`ui.panel.lovelace.editor.card.tile.appearance`
),
schema: [ schema: [
{ name: "name", selector: { text: {} } },
{ {
name: "icon", name: "",
selector: { type: "grid",
icon: { schema: [
placeholder: icon || entityState?.attributes.icon, { name: "name", selector: { text: {} } },
fallbackPath: {
!icon && !entityState?.attributes.icon && entityState name: "icon",
? domainIcon(computeDomain(entity), entityState) selector: {
: undefined, icon: {
placeholder: icon || entityState?.attributes.icon,
fallbackPath:
!icon && !entityState?.attributes.icon && entityState
? domainIcon(computeDomain(entity), entityState)
: undefined,
},
},
}, },
{
name: "color",
selector: {
select: {
options: [
{
label: this.hass!.localize(
`ui.panel.lovelace.editor.card.tile.default_color`
),
value: "default",
},
...Array.from(THEME_COLORS).map((color) => ({
label: capitalizeFirstLetter(color),
value: color,
})),
],
},
},
},
] as const,
},
] as const,
},
{
name: "",
type: "expandable",
title: this.hass!.localize(
`ui.panel.lovelace.editor.card.tile.actions`
),
iconPath: mdiGestureTap,
schema: [
{
name: "tap_action",
selector: {
"ui-action": {},
}, },
}, },
{ {
name: "color", name: "icon_tap_action",
selector: { selector: {
select: { "ui-action": {},
options: [
{
label: localize(
`ui.panel.lovelace.editor.card.tile.default_color`
),
value: "default",
},
...Array.from(THEME_COLORS).map((color) => ({
label: capitalizeFirstLetter(color),
value: color,
})),
],
},
}, },
}, },
{ {
@ -102,21 +127,6 @@ export class HuiTileCardEditor
] as const ] as const
); );
private _actionsSchema = [
{
name: "tap_action",
selector: {
"ui-action": {},
},
},
{
name: "icon_tap_action",
selector: {
"ui-action": {},
},
},
] as const;
protected render(): TemplateResult { protected render(): TemplateResult {
if (!this.hass || !this._config) { if (!this.hass || !this._config) {
return html``; return html``;
@ -126,14 +136,7 @@ export class HuiTileCardEditor
| HassEntity | HassEntity
| undefined; | undefined;
const mainSchema = this._mainSchema; const schema = this._schema(this._config.entity, this._config.icon, entity);
const appareanceSchema = this._appearanceSchema(
this.hass.localize,
this._config.entity,
this._config.icon,
entity
);
const actionsSchema = this._actionsSchema;
const data = { const data = {
color: "default", color: "default",
@ -141,55 +144,13 @@ export class HuiTileCardEditor
}; };
return html` return html`
<div class="container"> <ha-form
<div class="group"> .hass=${this.hass}
<ha-form .data=${data}
.hass=${this.hass} .schema=${schema}
.data=${data} .computeLabel=${this._computeLabelCallback}
.schema=${mainSchema} @value-changed=${this._valueChanged}
.computeLabel=${this._computeLabelCallback} ></ha-form>
@value-changed=${this._valueChanged}
></ha-form>
</div>
<div class="group">
<ha-expansion-panel>
<div slot="header">
<ha-svg-icon .path=${mdiPalette}></ha-svg-icon>
${this.hass!.localize(
`ui.panel.lovelace.editor.card.tile.appearance`
)}
</div>
<div class="content">
<ha-form
.hass=${this.hass}
.data=${data}
.schema=${appareanceSchema}
.computeLabel=${this._computeLabelCallback}
@value-changed=${this._valueChanged}
></ha-form>
</div>
</ha-expansion-panel>
</div>
<div class="group">
<ha-expansion-panel>
<div slot="header">
<ha-svg-icon .path=${mdiGestureTap}></ha-svg-icon>
${this.hass!.localize(
`ui.panel.lovelace.editor.card.tile.actions`
)}
</div>
<div class="content">
<ha-form
.hass=${this.hass}
.data=${data}
.schema=${actionsSchema}
.computeLabel=${this._computeLabelCallback}
@value-changed=${this._valueChanged}
></ha-form>
</div>
</ha-expansion-panel>
</div>
</div>
`; `;
} }
@ -204,10 +165,7 @@ export class HuiTileCardEditor
} }
private _computeLabelCallback = ( private _computeLabelCallback = (
schema: schema: SchemaUnion<ReturnType<typeof this._schema>>
| SchemaUnion<typeof this._mainSchema>
| SchemaUnion<ReturnType<typeof this._appearanceSchema>>
| SchemaUnion<typeof this._actionsSchema>
) => { ) => {
switch (schema.name) { switch (schema.name) {
case "color": case "color":
@ -230,20 +188,6 @@ export class HuiTileCardEditor
display: flex; display: flex;
flex-direction: column; flex-direction: column;
} }
.group:not(:last-child) {
margin-bottom: 12px;
}
.content {
padding: 12px;
}
ha-expansion-panel {
--expansion-panel-content-padding: 0;
border: 1px solid var(--divider-color);
border-radius: 6px;
}
ha-svg-icon {
color: var(--secondary-text-color);
}
`; `;
} }
} }