Change date picker (#8821)

This commit is contained in:
Bram Kragten 2021-04-06 14:36:12 +02:00 committed by GitHub
parent cc275f9877
commit b501b7f47c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 121 additions and 165 deletions

View File

@ -1,62 +1,61 @@
import "@vaadin/vaadin-date-picker/theme/material/vaadin-date-picker"; import "@vaadin/vaadin-date-picker/theme/material/vaadin-date-picker-light";
import {
css,
CSSResult,
customElement,
html,
LitElement,
property,
PropertyValues,
query,
} from "lit-element";
import "@polymer/paper-input/paper-input";
import { fireEvent } from "../common/dom/fire_event";
import { mdiCalendar } from "@mdi/js";
import "./ha-svg-icon";
const VaadinDatePicker = customElements.get("vaadin-date-picker"); const i18n = {
monthNames: [
const documentContainer = document.createElement("template"); "January",
documentContainer.setAttribute("style", "display: none;"); "February",
documentContainer.innerHTML = ` "March",
<dom-module id="ha-date-input-styles" theme-for="vaadin-text-field"> "April",
<template> "May",
<style> "June",
[part="input-field"] { "July",
top: 2px; "August",
height: 30px; "September",
color: var(--primary-text-color); "October",
} "November",
[part="value"] { "December",
text-align: center; ],
} weekdays: [
</style> "Sunday",
</template> "Monday",
</dom-module> "Tuesday",
`; "Wednesday",
document.head.appendChild(documentContainer.content); "Thursday",
"Friday",
export class HaDateInput extends VaadinDatePicker { "Saturday",
constructor() { ],
super(); weekdaysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
firstDayOfWeek: 0,
this.i18n.formatDate = this._formatISODate; week: "Week",
this.i18n.parseDate = this._parseISODate; calendar: "Calendar",
} clear: "Clear",
today: "Today",
ready() { cancel: "Cancel",
super.ready(); formatTitle: (monthName, fullYear) => {
const styleEl = document.createElement("style"); return monthName + " " + fullYear;
styleEl.innerHTML = ` },
:host { formatDate: (d: { day: number; month: number; year: number }) => {
width: 12ex;
margin-top: -6px;
--material-body-font-size: 16px;
--_material-text-field-input-line-background-color: var(--primary-text-color);
--_material-text-field-input-line-opacity: 1;
--material-primary-color: var(--primary-text-color);
}
`;
this.shadowRoot.appendChild(styleEl);
this._inputElement.querySelector("[part='toggle-button']").style.display =
"none";
}
private _formatISODate(d) {
return [ return [
("0000" + String(d.year)).slice(-4), ("0000" + String(d.year)).slice(-4),
("0" + String(d.month + 1)).slice(-2), ("0" + String(d.month + 1)).slice(-2),
("0" + String(d.day)).slice(-2), ("0" + String(d.day)).slice(-2),
].join("-"); ].join("-");
} },
parseDate: (text: string) => {
private _parseISODate(text) {
const parts = text.split("-"); const parts = text.split("-");
const today = new Date(); const today = new Date();
let date; let date;
@ -80,11 +79,73 @@ export class HaDateInput extends VaadinDatePicker {
return { day: date, month, year }; return { day: date, month, year };
} }
return undefined; return undefined;
},
};
@customElement("ha-date-input")
export class HaDateInput extends LitElement {
@property() public value?: string;
@property({ type: Boolean }) public disabled = false;
@property() public label?: string;
@query("vaadin-date-picker-light", true) private _datePicker;
private _inited = false;
updated(changedProps: PropertyValues) {
if (changedProps.has("value")) {
this._datePicker.value = this.value;
this._inited = true;
}
}
render() {
return html`<vaadin-date-picker-light
.disabled=${this.disabled}
@value-changed=${this._valueChanged}
attr-for-value="value"
.i18n=${i18n}
>
<paper-input .label=${this.label} no-label-float>
<ha-svg-icon slot="suffix" .path=${mdiCalendar}></ha-svg-icon>
</paper-input>
</vaadin-date-picker-light>`;
}
private _valueChanged(ev: CustomEvent) {
if (
!this.value ||
(this._inited && !this._compareStringDates(ev.detail.value, this.value))
) {
fireEvent(this, "value-changed", { value: ev.detail.value });
}
}
private _compareStringDates(a: string, b: string): boolean {
const aParts = a.split("-");
const bParts = b.split("-");
let i = 0;
for (const aPart of aParts) {
if (Number(aPart) !== Number(bParts[i])) {
return false;
}
i++;
}
return true;
}
static get styles(): CSSResult {
return css`
paper-input {
width: 110px;
}
ha-svg-icon {
color: var(--secondary-text-color);
}
`;
} }
} }
customElements.define("ha-date-input", HaDateInput as any);
declare global { declare global {
interface HTMLElementTagNameMap { interface HTMLElementTagNameMap {
"ha-date-input": HaDateInput; "ha-date-input": HaDateInput;

View File

@ -63,9 +63,9 @@ class HuiInputDatetimeEntityRow extends LitElement implements LovelaceRow {
<ha-date-input <ha-date-input
.disabled=${UNAVAILABLE_STATES.includes(stateObj.state)} .disabled=${UNAVAILABLE_STATES.includes(stateObj.state)}
.value=${`${stateObj.attributes.year}-${stateObj.attributes.month}-${stateObj.attributes.day}`} .value=${`${stateObj.attributes.year}-${stateObj.attributes.month}-${stateObj.attributes.day}`}
@change=${this._selectedValueChanged} @value-changed=${this._selectedValueChanged}
@click=${this._stopEventPropagation} >
></ha-date-input> </ha-date-input>
${stateObj.attributes.has_time ? "," : ""} ${stateObj.attributes.has_time ? "," : ""}
` `
: ``} : ``}
@ -103,9 +103,7 @@ class HuiInputDatetimeEntityRow extends LitElement implements LovelaceRow {
const date = this._dateInputEl ? this._dateInputEl.value : undefined; const date = this._dateInputEl ? this._dateInputEl.value : undefined;
if (time !== stateObj.state) { setInputDateTimeValue(this.hass!, stateObj.entity_id, time, date);
setInputDateTimeValue(this.hass!, stateObj.entity_id, time, date);
}
ev.target.blur(); ev.target.blur();
} }

View File

@ -1,103 +0,0 @@
const documentContainer = document.createElement("template");
documentContainer.setAttribute("style", "display: none;");
documentContainer.innerHTML = `
<dom-module id="ha-date-picker-text-field-styles" theme-for="vaadin-text-field">
<template>
<style>
:host {
padding: 8px 0 11px 0;
margin: 0;
}
[part~="label"] {
top: 6px;
font-size: var(--paper-font-subhead_-_font-size);
color: var(--paper-input-container-color, var(--secondary-text-color));
}
:host([focused]) [part~="label"] {
color: var(--paper-input-container-focus-color, var(--primary-color));
}
[part~="input-field"] {
color: var(--primary-text-color);
top: 3px;
}
[part~="input-field"]::before, [part~="input-field"]::after {
background-color: var(--paper-input-container-color, var(--secondary-text-color));
opacity: 1;
}
:host([focused]) [part~="input-field"]::before, :host([focused]) [part~="input-field"]::after {
background-color: var(--paper-input-container-focus-color, var(--primary-color));
}
[part~="value"] {
font-size: var(--paper-font-subhead_-_font-size);
height: 24px;
padding-top: 4px;
padding-bottom: 0;
}
</style>
</template>
</dom-module>
<dom-module id="ha-date-picker-button-styles" theme-for="vaadin-button">
<template>
<style>
:host([part~="today-button"]) [part~="button"]::before {
content: "⦿";
color: var(--primary-color);
}
[part~="button"] {
font-family: inherit;
font-size: var(--paper-font-subhead_-_font-size);
border: none;
background: transparent;
cursor: pointer;
min-height: var(--paper-item-min-height, 48px);
padding: 0px 16px;
color: inherit;
}
[part~="button"]:focus {
outline: none;
}
</style>
</template>
</dom-module>
<dom-module id="ha-date-picker-overlay-styles" theme-for="vaadin-date-picker-overlay">
<template>
<style include="vaadin-date-picker-overlay-default-theme">
[part~="toolbar"] {
padding: 0.3em;
background-color: var(--secondary-background-color);
}
[part="years"] {
background-color: var(--secondary-text-color);
--material-body-text-color: var(--primary-background-color);
}
[part="overlay"] {
background-color: var(--primary-background-color);
--material-body-text-color: var(--secondary-text-color);
}
</style>
</template>
</dom-module>
<dom-module id="ha-date-picker-month-styles" theme-for="vaadin-month-calendar">
<template>
<style include="vaadin-month-calendar-default-theme">
[part="date"][today] {
color: var(--primary-color);
}
</style>
</template>
</dom-module>
`;
document.head.appendChild(documentContainer.content);