Skip to content

Commit 807fbf8

Browse files
committed
Work with clamps
1 parent 44cd425 commit 807fbf8

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

src/components/ha-numeric-arrow-input.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,24 @@ export class HaNumericArrowInput extends LitElement {
6464

6565
private _up() {
6666
const newValue = this.value + (this.step ?? 1);
67-
fireEvent(this, "value-changed", { value: this._clampValue(newValue) });
67+
fireEvent(this, "value-changed", this._clampValue(newValue));
6868
}
6969

7070
private _down() {
7171
const newValue = this.value - (this.step ?? 1);
72-
fireEvent(this, "value-changed", { value: this._clampValue(newValue) });
72+
fireEvent(this, "value-changed", this._clampValue(newValue));
7373
}
7474

75-
private _clampValue(value: number) {
76-
if (this.max && value >= this.max) {
77-
return this.max;
75+
private _clampValue(value: number): { clamped: boolean; value: number } {
76+
if (this.max !== undefined && value >= this.max) {
77+
return { clamped: true, value: this.max };
7878
}
79-
if (this.min && value <= this.min) {
80-
return this.min;
79+
80+
if (this.min !== undefined && value < this.min) {
81+
return { clamped: true, value: this.min };
8182
}
82-
return value;
83+
84+
return { clamped: false, value };
8385
}
8486

8587
static styles = css`

src/components/ha-time-picker.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,8 @@ export class HaTimePicker extends LitElement {
152152
}
153153
}
154154

155-
private _hoursChanged(ev: CustomEvent<{ value: number }>) {
155+
private _hoursChanged(ev: CustomEvent<{ clamped: boolean; value: number }>) {
156156
const value = ev.detail.value;
157-
console.log({ originalValue: this.value }, "hoursChanged", value);
158157
if (this._useAmPm) {
159158
if (value > 12) {
160159
this._hours = value - 12;
@@ -168,36 +167,45 @@ export class HaTimePicker extends LitElement {
168167
}
169168
}
170169

171-
private _minutesChanged(ev: CustomEvent<{ value: number }>) {
172-
console.log(
173-
{ originalValue: this.value },
174-
"minutesChanged",
175-
ev.detail.value
176-
);
170+
private _minutesChanged(
171+
ev: CustomEvent<{ clamped: boolean; value: number }>
172+
) {
177173
this._minutes = ev.detail.value;
174+
if (ev.detail.clamped) {
175+
if (ev.detail.value <= 0) {
176+
this._hours -= 1;
177+
this._minutes = 59;
178+
}
179+
180+
if (ev.detail.value >= 59) {
181+
this._hours += 1;
182+
this._minutes = 0;
183+
}
184+
}
178185
}
179186

180-
private _secondsChanged(ev: CustomEvent<{ value: number }>) {
181-
console.log(
182-
{ originalValue: this.value },
183-
"secondsChanged",
184-
ev.detail.value
185-
);
187+
private _secondsChanged(
188+
ev: CustomEvent<{ clamped: boolean; value: number }>
189+
) {
186190
this._seconds = ev.detail.value;
191+
if (ev.detail.clamped) {
192+
if (ev.detail.value <= 0) {
193+
this._minutes -= 1;
194+
this._seconds = 59;
195+
}
196+
197+
if (ev.detail.value >= 59) {
198+
this._minutes += 1;
199+
this._seconds = 0;
200+
}
201+
}
187202
}
188203

189204
private _toggleAmPm() {
190205
this._hours = this._hours > 12 ? this._hours - 12 : this._hours + 12;
191206
}
192207

193208
private _timeUpdated() {
194-
console.log(
195-
{ originalValue: this.value },
196-
"timeUpdated",
197-
this._hours,
198-
this._minutes,
199-
this._seconds
200-
);
201209
const timeParts = [
202210
this._hours.toString().padStart(2, "0"),
203211
this._minutes.toString().padStart(2, "0"),

0 commit comments

Comments
 (0)