From 981dd5df633a8d11390fde2420bc43580f4db062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Fri, 26 Apr 2019 06:47:46 +0200 Subject: [PATCH] Basic input-datetime entity row (#3121) * Basic input-datetime entity row * Address review comments * Fix imports --- src/components/ha-date-input.ts | 126 +++++++++++++++++ src/components/paper-time-input.js | 2 +- src/data/input_datetime.ts | 11 ++ .../lovelace/common/create-row-element.ts | 2 + .../hui-input-datetime-entity-row.ts | 128 ++++++++++++++++++ 5 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 src/components/ha-date-input.ts create mode 100644 src/data/input_datetime.ts create mode 100644 src/panels/lovelace/entity-rows/hui-input-datetime-entity-row.ts diff --git a/src/components/ha-date-input.ts b/src/components/ha-date-input.ts new file mode 100644 index 0000000000..4157d70af9 --- /dev/null +++ b/src/components/ha-date-input.ts @@ -0,0 +1,126 @@ +import { + html, + css, + LitElement, + TemplateResult, + property, + customElement, +} from "lit-element"; + +import "@polymer/paper-input/paper-input"; +// tslint:disable-next-line:no-duplicate-imports +import { PaperInputElement } from "@polymer/paper-input/paper-input"; + +@customElement("ha-date-input") +export class HaDateInput extends LitElement { + @property() public year?: string; + @property() public month?: string; + @property() public day?: string; + @property({ type: Boolean }) public disabled = false; + + static get styles() { + return css` + :host { + display: block; + font-family: var(--paper-font-common-base_-_font-family); + -webkit-font-smoothing: var( + --paper-font-common-base_-_-webkit-font-smoothing + ); + } + + paper-input { + width: 30px; + text-align: center; + --paper-input-container-input_-_-moz-appearance: textfield; + --paper-input-container-input-webkit-spinner_-_-webkit-appearance: none; + --paper-input-container-input-webkit-spinner_-_margin: 0; + --paper-input-container-input-webkit-spinner_-_display: none; + } + + paper-input#year { + width: 50px; + } + + .date-input-wrap { + display: flex; + flex-direction: row; + } + `; + } + + protected render(): TemplateResult { + return html` +
+ + - + + + - + + + +
+ `; + } + + private _formatYear() { + const yearElement = this.shadowRoot!.getElementById( + "year" + ) as PaperInputElement; + this.year = yearElement.value!; + } + + private _formatMonth() { + const monthElement = this.shadowRoot!.getElementById( + "month" + ) as PaperInputElement; + this.month = ("0" + monthElement.value!).slice(-2); + } + + private _formatDay() { + const dayElement = this.shadowRoot!.getElementById( + "day" + ) as PaperInputElement; + this.day = ("0" + dayElement.value!).slice(-2); + } + + get value() { + return `${this.year}-${this.month}-${this.day}`; + } +} + +declare global { + interface HTMLElementTagNameMap { + "ha-date-input": HaDateInput; + } +} diff --git a/src/components/paper-time-input.js b/src/components/paper-time-input.js index 59c819858a..3fec74281a 100644 --- a/src/components/paper-time-input.js +++ b/src/components/paper-time-input.js @@ -23,7 +23,7 @@ import "@polymer/paper-dropdown-menu/paper-dropdown-menu"; import { html } from "@polymer/polymer/lib/utils/html-tag"; import { PolymerElement } from "@polymer/polymer/polymer-element"; -class PaperTimeInput extends PolymerElement { +export class PaperTimeInput extends PolymerElement { static get template() { return html`