mirror of
https://github.com/home-assistant/frontend.git
synced 2025-11-09 19:09:48 +00:00
Work with clamps
This commit is contained in:
@@ -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`
|
||||
|
||||
@@ -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"),
|
||||
|
||||
Reference in New Issue
Block a user