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",
"version": "1.2.0",
"version": "1.3.0",
"description": "",
"main": "tp-number-input.js",
"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.
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);
}