mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 03:06:41 +00:00
Fix time selector + base am/pm on user language (#8908)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
aaa50b4d1d
commit
193016a46a
@ -1,12 +1,9 @@
|
|||||||
import { customElement, html, LitElement, property } from "lit-element";
|
import { customElement, html, LitElement, property } from "lit-element";
|
||||||
|
import memoizeOne from "memoize-one";
|
||||||
import { fireEvent } from "../../common/dom/fire_event";
|
import { fireEvent } from "../../common/dom/fire_event";
|
||||||
import { TimeSelector } from "../../data/selector";
|
import { TimeSelector } from "../../data/selector";
|
||||||
import { HomeAssistant } from "../../types";
|
import { HomeAssistant } from "../../types";
|
||||||
import "../paper-time-input";
|
import "../paper-time-input";
|
||||||
|
|
||||||
const test = new Date().toLocaleString();
|
|
||||||
const useAMPM = test.includes("AM") || test.includes("PM");
|
|
||||||
|
|
||||||
@customElement("ha-selector-time")
|
@customElement("ha-selector-time")
|
||||||
export class HaTimeSelector extends LitElement {
|
export class HaTimeSelector extends LitElement {
|
||||||
@property() public hass!: HomeAssistant;
|
@property() public hass!: HomeAssistant;
|
||||||
@ -19,16 +16,24 @@ export class HaTimeSelector extends LitElement {
|
|||||||
|
|
||||||
@property({ type: Boolean }) public disabled = false;
|
@property({ type: Boolean }) public disabled = false;
|
||||||
|
|
||||||
|
private _useAmPm = memoizeOne((language: string) => {
|
||||||
|
const test = new Date().toLocaleString(language);
|
||||||
|
return test.includes("AM") || test.includes("PM");
|
||||||
|
});
|
||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
|
const useAMPM = this._useAmPm(this.hass.locale.language);
|
||||||
|
|
||||||
const parts = this.value?.split(":") || [];
|
const parts = this.value?.split(":") || [];
|
||||||
const hours = useAMPM ? parts[0] ?? "12" : parts[0] ?? "0";
|
const hours = parts[0];
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<paper-time-input
|
<paper-time-input
|
||||||
.label=${this.label}
|
.label=${this.label}
|
||||||
.hour=${useAMPM && Number(hours) > 12 ? Number(hours) - 12 : hours}
|
.hour=${hours &&
|
||||||
.min=${parts[1] ?? "00"}
|
(useAMPM && Number(hours) > 12 ? Number(hours) - 12 : hours)}
|
||||||
.sec=${parts[2] ?? "00"}
|
.min=${parts[1]}
|
||||||
|
.sec=${parts[2]}
|
||||||
.format=${useAMPM ? 12 : 24}
|
.format=${useAMPM ? 12 : 24}
|
||||||
.amPm=${useAMPM && (Number(hours) > 12 ? "PM" : "AM")}
|
.amPm=${useAMPM && (Number(hours) > 12 ? "PM" : "AM")}
|
||||||
.disabled=${this.disabled}
|
.disabled=${this.disabled}
|
||||||
@ -42,12 +47,16 @@ export class HaTimeSelector extends LitElement {
|
|||||||
|
|
||||||
private _timeChanged(ev) {
|
private _timeChanged(ev) {
|
||||||
let value = ev.target.value;
|
let value = ev.target.value;
|
||||||
if (useAMPM) {
|
const useAMPM = this._useAmPm(this.hass.locale.language);
|
||||||
let hours = Number(ev.target.hour);
|
let hours = Number(ev.target.hour || 0);
|
||||||
|
if (value && useAMPM) {
|
||||||
if (ev.target.amPm === "PM") {
|
if (ev.target.amPm === "PM") {
|
||||||
hours += 12;
|
hours += 12;
|
||||||
}
|
}
|
||||||
value = `${hours}:${ev.target.min}:${ev.target.sec}`;
|
value = `${hours}:${ev.target.min || "00"}:${ev.target.sec || "00"}`;
|
||||||
|
}
|
||||||
|
if (value === this.value) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
fireEvent(this, "value-changed", {
|
fireEvent(this, "value-changed", {
|
||||||
value,
|
value,
|
||||||
|
@ -133,7 +133,7 @@ export class PaperTimeInput extends PolymerElement {
|
|||||||
always-float-label$="[[alwaysFloatInputLabels]]"
|
always-float-label$="[[alwaysFloatInputLabels]]"
|
||||||
disabled="[[disabled]]"
|
disabled="[[disabled]]"
|
||||||
>
|
>
|
||||||
<span suffix="" slot="suffix">:</span>
|
<span suffix slot="suffix">:</span>
|
||||||
</paper-input>
|
</paper-input>
|
||||||
|
|
||||||
<!-- Min Input -->
|
<!-- Min Input -->
|
||||||
@ -303,28 +303,28 @@ export class PaperTimeInput extends PolymerElement {
|
|||||||
notify: true,
|
notify: true,
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* Suffix for the hour input
|
* Label for the hour input
|
||||||
*/
|
*/
|
||||||
hourLabel: {
|
hourLabel: {
|
||||||
type: String,
|
type: String,
|
||||||
value: "",
|
value: "",
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* Suffix for the min input
|
* Label for the min input
|
||||||
*/
|
*/
|
||||||
minLabel: {
|
minLabel: {
|
||||||
type: String,
|
type: String,
|
||||||
value: ":",
|
value: "",
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* Suffix for the sec input
|
* Label for the sec input
|
||||||
*/
|
*/
|
||||||
secLabel: {
|
secLabel: {
|
||||||
type: String,
|
type: String,
|
||||||
value: "",
|
value: "",
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* Suffix for the milli sec input
|
* Label for the milli sec input
|
||||||
*/
|
*/
|
||||||
millisecLabel: {
|
millisecLabel: {
|
||||||
type: String,
|
type: String,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user