Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

number | remove obsolete numDecimalPlaces #71

Merged
merged 6 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions packages/v3/src/esp-entity-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ interface entityConfig {
target_temperature_high?: number;
min_temp?: number;
max_temp?: number;
min_value?: number;
max_value?: number;
min_value?: string;
max_value?: string;
step?: number;
min_length?: number;
max_length?: number;
Expand Down Expand Up @@ -337,28 +337,22 @@ class ActionRenderer {
entity: entityConfig,
action: string,
opt: string,
value: number | string,
min?: number,
max?: number,
value: string | number,
min?: string | undefined,
max?: string | undefined,
step = 1
) {
if(entity.mode == 1) {
const val = Number(value);
let stepString = step.toString();
let numDecimalPlaces = 0
if (stepString.indexOf('.') !== -1) {
numDecimalPlaces = stepString.split('.')[1].length;
}
return html`<div class="range">
<label>${min || 0}</label>
<input
type="${entity.mode == 1 ? "number" : "range"}"
name="${entity.unique_id}"
id="${entity.unique_id}"
step="${step}"
min="${min || Math.min(0, val)}"
max="${max || Math.max(10, val)}"
.value="${(val.toFixed(numDecimalPlaces))}"
min="${min || Math.min(0, value as number)}"
max="${max || Math.max(10, value as number)}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not terribly important, but by keeping const val = Number(value); (or const val = parseFloat(value);), the type casting as number can be removed.

.value="${value}"
@change="${(e: Event) => {
const val = (<HTMLTextAreaElement>e.target)?.value;
this.actioner?.restAction(entity, `${action}?${opt}=${val}`);
Expand Down
19 changes: 6 additions & 13 deletions packages/v3/src/esp-range-slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ export class EspRangeSlider extends LitElement {
private inputRange: HTMLInputElement | null = null;
private currentValue: HTMLInputElement | null = null;

private numDecimalPlaces: number = 0;
private longPressTimer: ReturnType<typeof setTimeout> | null = null;
private isPopupInputVisible: boolean = false;

@property({ type: Number }) value = 0;
@property({ type: Number }) min = 0;
@property({ type: Number }) max = 0;
@property({ type: Number }) step = 0;
@property({ type: String }) value = 0;
@property({ type: String }) min = 0;
@property({ type: String }) max = 0;
@property({ type: String }) step = 0;
@property({ type: String }) name = "";

protected firstUpdated(
Expand All @@ -31,12 +30,6 @@ export class EspRangeSlider extends LitElement {
this.currentValue = this.shadowRoot?.getElementById(
currentValueID
) as HTMLInputElement;

let stepString = this.step.toString();
if (stepString.indexOf('.') !== -1) {
this.numDecimalPlaces = stepString.split('.')[1].length;
}

document.addEventListener('mousedown', (event) => {
if(!document.querySelector('.popup-number-input')) {
return;
Expand Down Expand Up @@ -133,7 +126,7 @@ export class EspRangeSlider extends LitElement {
updateCurrentValueOverlay(): void {
const newValueAsPercent = Number( (this.inputRange.value - this.inputRange.min) * 100 / (this.inputRange.max - this.inputRange.min) ),
newPosition = 10 - (newValueAsPercent * 0.2);
this.currentValue.innerHTML = `<span>${Number(this.inputRange?.value).toFixed(this.numDecimalPlaces)}</span>`;
this.currentValue.innerHTML = `<span>${this.inputRange?.value}</span>`;
this.currentValue.style.left = `calc(${newValueAsPercent}% + (${newPosition}px))`;

const spanTooltip = this.currentValue?.querySelector('span');
Expand Down Expand Up @@ -178,7 +171,7 @@ export class EspRangeSlider extends LitElement {
step="${this.step}"
min="${this.min || Math.min(0, this.value)}"
max="${this.max || Math.max(10, this.value)}"
.value="${(this.value.toFixed(this.numDecimalPlaces))}"
.value="${this.value}"
@input="${this.onInputEvent}"
@change="${this.onInputChangeEvent}"
/>
Expand Down