Auto-detect if floating point mode is required

This commit is contained in:
2025-12-07 11:21:48 +01:00
parent 92a366b4aa
commit 8ded42d4b6
2 changed files with 17 additions and 14 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@tp/tp-number-input", "name": "@tp/tp-number-input",
"version": "1.2.0", "version": "1.3.0",
"description": "", "description": "",
"main": "tp-number-input.js", "main": "tp-number-input.js",
"scripts": { "scripts": {

View File

@@ -98,10 +98,8 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) {
// Most other settings are ignore if the control is in this mode. // Most other settings are ignore if the control is in this mode.
timeMode: { type: Boolean }, timeMode: { type: Boolean },
// If true, enables floating point number support. When false (default), maintains strict integer behavior for backward compatibility. _internValue: { type: String },
floatingPoint: { type: Boolean }, _floatingPoint: { type: Boolean, state: true }
_internValue: { type: String }
}; };
} }
@@ -122,7 +120,7 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) {
this.max = Number.MAX_SAFE_INTEGER; this.max = Number.MAX_SAFE_INTEGER;
this.fastInterval = 50; this.fastInterval = 50;
this.superFastInterval = 100; this.superFastInterval = 100;
this.floatingPoint = false; this._floatingPoint = false;
this._isConnected = false this._isConnected = false
} }
@@ -145,6 +143,11 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) {
updated(changes) { updated(changes) {
super.updated(); 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')) { if (changes.has('timeMode')) {
this._timeModeChanged(); this._timeModeChanged();
} }
@@ -155,11 +158,11 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) {
} }
/** /**
* Parse value based on floatingPoint mode. * Parse value based on floating point mode.
* Strictly maintains integer behavior when floatingPoint=false for backward compatibility. * Strictly maintains integer behavior when step is a whole number for backward compatibility.
*/ */
_parseValue(val) { _parseValue(val) {
if (this.floatingPoint) { if (this._floatingPoint) {
return parseFloat(val); return parseFloat(val);
} }
// Maintain exact existing behavior for backward compatibility // 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. * Format floating point value with appropriate precision based on step.
*/ */
_formatFloatingValue(val) { _formatFloatingValue(val) {
if (!this.floatingPoint) { if (!this._floatingPoint) {
return val; // No formatting for integer mode return val; // No formatting for integer mode
} }
@@ -207,7 +210,7 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) {
} }
// Apply floating point formatting if enabled // Apply floating point formatting if enabled
if (this.floatingPoint) { if (this._floatingPoint) {
val = this._formatFloatingValue(val); val = this._formatFloatingValue(val);
} }
} }
@@ -342,7 +345,7 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) {
e.preventDefault(); e.preventDefault();
let val = this._parseValue(this.value) || 0; let val = this._parseValue(this.value) || 0;
val = Math.min(this.max, val + this.step); val = Math.min(this.max, val + this.step);
if (this.floatingPoint) { if (this._floatingPoint) {
val = this._formatFloatingValue(val); val = this._formatFloatingValue(val);
} }
this._updateValue(val); this._updateValue(val);
@@ -350,7 +353,7 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) {
e.preventDefault(); e.preventDefault();
let val = this._parseValue(this.value) || 0; let val = this._parseValue(this.value) || 0;
val = Math.max(this.min, val - this.step); val = Math.max(this.min, val - this.step);
if (this.floatingPoint) { if (this._floatingPoint) {
val = this._formatFloatingValue(val); val = this._formatFloatingValue(val);
} }
this._updateValue(val); this._updateValue(val);
@@ -402,7 +405,7 @@ class TpNumberInput extends FormElement(EventHelpers(DomQuery(LitElement))) {
val = Math.max(this.min, val); val = Math.max(this.min, val);
// Apply floating point formatting if enabled // Apply floating point formatting if enabled
if (this.floatingPoint) { if (this._floatingPoint) {
val = this._formatFloatingValue(val); val = this._formatFloatingValue(val);
} }