From 807fbf8bb6db24c1da51b5a4014443b8503dbaf9 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Mon, 1 Sep 2025 11:50:45 +0100 Subject: [PATCH] Work with clamps --- src/components/ha-numeric-arrow-input.ts | 18 +++++---- src/components/ha-time-picker.ts | 50 ++++++++++++++---------- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/components/ha-numeric-arrow-input.ts b/src/components/ha-numeric-arrow-input.ts index 0c32a06dde..1468e6a392 100644 --- a/src/components/ha-numeric-arrow-input.ts +++ b/src/components/ha-numeric-arrow-input.ts @@ -64,22 +64,24 @@ export class HaNumericArrowInput extends LitElement { private _up() { const newValue = this.value + (this.step ?? 1); - fireEvent(this, "value-changed", { value: this._clampValue(newValue) }); + fireEvent(this, "value-changed", this._clampValue(newValue)); } private _down() { const newValue = this.value - (this.step ?? 1); - fireEvent(this, "value-changed", { value: this._clampValue(newValue) }); + fireEvent(this, "value-changed", this._clampValue(newValue)); } - private _clampValue(value: number) { - if (this.max && value >= this.max) { - return this.max; + private _clampValue(value: number): { clamped: boolean; value: number } { + if (this.max !== undefined && value >= this.max) { + return { clamped: true, value: this.max }; } - if (this.min && value <= this.min) { - return this.min; + + if (this.min !== undefined && value < this.min) { + return { clamped: true, value: this.min }; } - return value; + + return { clamped: false, value }; } static styles = css` diff --git a/src/components/ha-time-picker.ts b/src/components/ha-time-picker.ts index 148c0446e1..9f43eaf53a 100644 --- a/src/components/ha-time-picker.ts +++ b/src/components/ha-time-picker.ts @@ -152,9 +152,8 @@ export class HaTimePicker extends LitElement { } } - private _hoursChanged(ev: CustomEvent<{ value: number }>) { + private _hoursChanged(ev: CustomEvent<{ clamped: boolean; value: number }>) { const value = ev.detail.value; - console.log({ originalValue: this.value }, "hoursChanged", value); if (this._useAmPm) { if (value > 12) { this._hours = value - 12; @@ -168,22 +167,38 @@ export class HaTimePicker extends LitElement { } } - private _minutesChanged(ev: CustomEvent<{ value: number }>) { - console.log( - { originalValue: this.value }, - "minutesChanged", - ev.detail.value - ); + private _minutesChanged( + ev: CustomEvent<{ clamped: boolean; value: number }> + ) { this._minutes = ev.detail.value; + if (ev.detail.clamped) { + if (ev.detail.value <= 0) { + this._hours -= 1; + this._minutes = 59; + } + + if (ev.detail.value >= 59) { + this._hours += 1; + this._minutes = 0; + } + } } - private _secondsChanged(ev: CustomEvent<{ value: number }>) { - console.log( - { originalValue: this.value }, - "secondsChanged", - ev.detail.value - ); + private _secondsChanged( + ev: CustomEvent<{ clamped: boolean; value: number }> + ) { this._seconds = ev.detail.value; + if (ev.detail.clamped) { + if (ev.detail.value <= 0) { + this._minutes -= 1; + this._seconds = 59; + } + + if (ev.detail.value >= 59) { + this._minutes += 1; + this._seconds = 0; + } + } } private _toggleAmPm() { @@ -191,13 +206,6 @@ export class HaTimePicker extends LitElement { } private _timeUpdated() { - console.log( - { originalValue: this.value }, - "timeUpdated", - this._hours, - this._minutes, - this._seconds - ); const timeParts = [ this._hours.toString().padStart(2, "0"), this._minutes.toString().padStart(2, "0"),