Fix labels in duration input (#25510)

This commit is contained in:
Paul Bottein 2025-05-19 11:26:25 +02:00 committed by GitHub
parent 15fd4134d0
commit 5cbadaa5f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 22 deletions

View File

@ -81,27 +81,27 @@ export class HaBaseTimeInput extends LitElement {
/** /**
* Label for the day input * Label for the day input
*/ */
@property({ attribute: false }) dayLabel = ""; @property({ type: String, attribute: "day-label" }) dayLabel = "";
/** /**
* Label for the hour input * Label for the hour input
*/ */
@property({ attribute: false }) hourLabel = ""; @property({ type: String, attribute: "hour-label" }) hourLabel = "";
/** /**
* Label for the min input * Label for the min input
*/ */
@property({ attribute: false }) minLabel = ""; @property({ type: String, attribute: "min-label" }) minLabel = "";
/** /**
* Label for the sec input * Label for the sec input
*/ */
@property({ attribute: false }) secLabel = ""; @property({ type: String, attribute: "sec-label" }) secLabel = "";
/** /**
* Label for the milli sec input * Label for the milli sec input
*/ */
@property({ attribute: false }) millisecLabel = ""; @property({ type: String, attribute: "ms-label" }) millisecLabel = "";
/** /**
* show the sec field * show the sec field
@ -342,7 +342,7 @@ export class HaBaseTimeInput extends LitElement {
padding-right: 3px; padding-right: 3px;
} }
ha-textfield { ha-textfield {
width: 55px; width: 60px;
flex-grow: 1; flex-grow: 1;
text-align: center; text-align: center;
--mdc-shape-small: 0; --mdc-shape-small: 0;

View File

@ -52,11 +52,11 @@ class HaDurationInput extends LitElement {
.milliseconds=${this._milliseconds} .milliseconds=${this._milliseconds}
@value-changed=${this._durationChanged} @value-changed=${this._durationChanged}
no-hours-limit no-hours-limit
dayLabel="dd" day-label="dd"
hourLabel="hh" hour-label="hh"
minLabel="mm" min-label="mm"
secLabel="ss" sec-label="ss"
millisecLabel="ms" ms-label="ms"
></ha-base-time-input> ></ha-base-time-input>
`; `;
} }

View File

@ -28,22 +28,30 @@ export class HaTimeInput extends LitElement {
protected render() { protected render() {
const useAMPM = useAmPm(this.locale); const useAMPM = useAmPm(this.locale);
const parts = this.value?.split(":") || []; let hours = NaN;
let hours = parts[0]; let minutes = NaN;
const numberHours = Number(parts[0]); let seconds = NaN;
if (numberHours && useAMPM && numberHours > 12 && numberHours < 24) { let numberHours = 0;
hours = String(numberHours - 12).padStart(2, "0"); if (this.value) {
} const parts = this.value?.split(":") || [];
if (useAMPM && numberHours === 0) { minutes = parts[1] ? Number(parts[1]) : 0;
hours = "12"; seconds = parts[2] ? Number(parts[2]) : 0;
hours = parts[0] ? Number(parts[0]) : 0;
numberHours = hours;
if (numberHours && useAMPM && numberHours > 12 && numberHours < 24) {
hours = numberHours - 12;
}
if (useAMPM && numberHours === 0) {
hours = 12;
}
} }
return html` return html`
<ha-base-time-input <ha-base-time-input
.label=${this.label} .label=${this.label}
.hours=${Number(hours)} .hours=${hours}
.minutes=${Number(parts[1])} .minutes=${minutes}
.seconds=${Number(parts[2])} .seconds=${seconds}
.format=${useAMPM ? 12 : 24} .format=${useAMPM ? 12 : 24}
.amPm=${useAMPM && numberHours >= 12 ? "PM" : "AM"} .amPm=${useAMPM && numberHours >= 12 ? "PM" : "AM"}
.disabled=${this.disabled} .disabled=${this.disabled}
@ -52,6 +60,11 @@ export class HaTimeInput extends LitElement {
.required=${this.required} .required=${this.required}
.clearable=${this.clearable && this.value !== undefined} .clearable=${this.clearable && this.value !== undefined}
.helper=${this.helper} .helper=${this.helper}
day-label="dd"
hour-label="hh"
min-label="mm"
sec-label="ss"
ms-label="ms"
></ha-base-time-input> ></ha-base-time-input>
`; `;
} }