From 8ded42d4b6dc80be63cb73eb307da6a1b0a4d887 Mon Sep 17 00:00:00 2001 From: pk Date: Sun, 7 Dec 2025 11:21:48 +0100 Subject: [PATCH] Auto-detect if floating point mode is required --- package.json | 2 +- tp-number-input.js | 29 ++++++++++++++++------------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 28f1326..e909b50 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tp/tp-number-input", - "version": "1.2.0", + "version": "1.3.0", "description": "", "main": "tp-number-input.js", "scripts": { diff --git a/tp-number-input.js b/tp-number-input.js index d7e4a13..42c8724 100644 --- a/tp-number-input.js +++ b/tp-number-input.js @@ -98,10 +98,8 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) { // Most other settings are ignore if the control is in this mode. timeMode: { type: Boolean }, - // If true, enables floating point number support. When false (default), maintains strict integer behavior for backward compatibility. - floatingPoint: { type: Boolean }, - - _internValue: { type: String } + _internValue: { type: String }, + _floatingPoint: { type: Boolean, state: true } }; } @@ -122,7 +120,7 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) { this.max = Number.MAX_SAFE_INTEGER; this.fastInterval = 50; this.superFastInterval = 100; - this.floatingPoint = false; + this._floatingPoint = false; this._isConnected = false } @@ -145,6 +143,11 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) { updated(changes) { super.updated(); + if (changes.has('step')) { + // Automatically enable floating point mode if step is fractional + this._floatingPoint = this.step % 1 !== 0; + } + if (changes.has('timeMode')) { this._timeModeChanged(); } @@ -155,11 +158,11 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) { } /** - * Parse value based on floatingPoint mode. - * Strictly maintains integer behavior when floatingPoint=false for backward compatibility. + * Parse value based on floating point mode. + * Strictly maintains integer behavior when step is a whole number for backward compatibility. */ _parseValue(val) { - if (this.floatingPoint) { + if (this._floatingPoint) { return parseFloat(val); } // Maintain exact existing behavior for backward compatibility @@ -170,7 +173,7 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) { * Format floating point value with appropriate precision based on step. */ _formatFloatingValue(val) { - if (!this.floatingPoint) { + if (!this._floatingPoint) { return val; // No formatting for integer mode } @@ -207,7 +210,7 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) { } // Apply floating point formatting if enabled - if (this.floatingPoint) { + if (this._floatingPoint) { val = this._formatFloatingValue(val); } } @@ -342,7 +345,7 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) { e.preventDefault(); let val = this._parseValue(this.value) || 0; val = Math.min(this.max, val + this.step); - if (this.floatingPoint) { + if (this._floatingPoint) { val = this._formatFloatingValue(val); } this._updateValue(val); @@ -350,7 +353,7 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) { e.preventDefault(); let val = this._parseValue(this.value) || 0; val = Math.max(this.min, val - this.step); - if (this.floatingPoint) { + if (this._floatingPoint) { val = this._formatFloatingValue(val); } this._updateValue(val); @@ -402,7 +405,7 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) { val = Math.max(this.min, val); // Apply floating point formatting if enabled - if (this.floatingPoint) { + if (this._floatingPoint) { val = this._formatFloatingValue(val); }