ha-frontend/src/components/ha-timezone-picker.ts

63 lines
1.6 KiB
TypeScript

import timezones from "google-timezones-json";
import { css, CSSResultGroup, html, LitElement } from "lit";
import { customElement, property } from "lit/decorators";
import { fireEvent } from "../common/dom/fire_event";
import { stopPropagation } from "../common/dom/stop_propagation";
import "./ha-list-item";
import "./ha-select";
import type { HaSelect } from "./ha-select";
@customElement("ha-timezone-picker")
export class HaTimeZonePicker extends LitElement {
@property() public value?: string;
@property() public label?: string;
@property({ type: Boolean }) public required = false;
@property({ type: Boolean, reflect: true }) public disabled = false;
protected render() {
return html`
<ha-select
.label=${this.label}
.value=${this.value}
.required=${this.required}
.disabled=${this.disabled}
@selected=${this._changed}
@closed=${stopPropagation}
fixedMenuPosition
naturalMenuWidth
>
${Object.entries(timezones).map(
([key, value]) =>
html`<ha-list-item value=${key}>${value}</ha-list-item>`
)}
</ha-select>
`;
}
static get styles(): CSSResultGroup {
return css`
ha-select {
width: 100%;
}
`;
}
private _changed(ev): void {
const target = ev.target as HaSelect;
if (target.value === "" || target.value === this.value) {
return;
}
this.value = target.value;
fireEvent(this, "value-changed", { value: this.value });
}
}
declare global {
interface HTMLElementTagNameMap {
"ha-timezone-picker": HaTimeZonePicker;
}
}