Work with clamps

This commit is contained in:
Aidan Timson
2025-09-01 11:50:45 +01:00
parent 44cd425ce8
commit 807fbf8bb6
2 changed files with 39 additions and 29 deletions

View File

@@ -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`

View File

@@ -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"),