Skip to content

Commit 2680baa

Browse files
committed
Fix that number inputs are changed if attributes are changed
1 parent 966acc3 commit 2680baa

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

sample/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</script>
1919
</head>
2020
<body>
21-
<node-projects-slider style="position: absolute; width: 500px; height: 300px;" value-min="10000"; value-max="90000" min="0"; max="100000">
21+
<node-projects-slider style="position: absolute; width: 500px; height: 300px;" value-min="20000"; value-max="90000" min="10000"; max="100000">
2222

2323
</node-projects-slider>
2424
</body>

src/slider/SliderWebcomponent.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,32 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
124124

125125
attributeChangedCallback(name: string, oldValue: string, newValue: string) {
126126
if (this._suppressAttributeChange) return;
127+
127128
if (name === "value-min" || name === "value-max") {
129+
let min = parseInt(this.getAttribute('min'));
130+
let max = parseInt(this.getAttribute('max'));
131+
let valueMin = parseInt(this.getAttribute('value-min'));
132+
let valueMax = parseInt(this.getAttribute('value-max'));
133+
134+
if (valueMin < min) {
135+
valueMin = min;
136+
this._suppressAttributeChange = true;
137+
this.setAttribute('value-min', valueMin.toString());
138+
this._suppressAttributeChange = false;
139+
}
140+
if (valueMax > max) {
141+
valueMax = max;
142+
this._suppressAttributeChange = true;
143+
this.setAttribute('value-max', valueMax.toString());
144+
this._suppressAttributeChange = false;
145+
}
146+
128147
this._updateInputValues();
129148
this._updateRangeInputsValues();
130-
this._updateSliderPosition(parseInt(this.getAttribute('value-min')), parseInt(this.getAttribute('max')), true);
131-
this._updateSliderPosition(parseInt(this.getAttribute('value-max')), parseInt(this.getAttribute('max')), false);
149+
this._updateSliderPosition(valueMin, max, true);
150+
this._updateSliderPosition(valueMax, max, false);
132151
}
152+
133153
if (name === "min" || name === "max") {
134154
this._updateRangeInputsMinMax();
135155
}
@@ -178,17 +198,25 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
178198
}
179199
} else {
180200
// Update input values and range progress
201+
this._suppressAttributeChange = true;
181202
this.setAttribute('value-min', minVal.toString());
182203
this.setAttribute('value-max', maxVal.toString());
204+
this._suppressAttributeChange = false;
205+
this._updateSliderPosition(minVal, parseInt(this.getAttribute('max')), true);
206+
this._updateSliderPosition(maxVal, parseInt(this.getAttribute('max')), false);
207+
this._updateInputValues(); // Ensure number inputs are updated
183208
}
184209
});
185210

186211
this._rangeInputs[i].addEventListener("change", e => {
187212
let minVal = parseInt(this._rangeInputs[0].value);
188213
let maxVal = parseInt(this._rangeInputs[1].value);
189214

215+
this._suppressAttributeChange = true;
190216
this.setAttribute('value-min', minVal.toString());
191217
this.setAttribute('value-max', maxVal.toString());
218+
this._suppressAttributeChange = false;
219+
this._updateInputValues(); // Ensure number inputs are updated
192220
});
193221
}
194222

@@ -257,8 +285,11 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
257285
this._updateSliderPosition(maxp, value2, false);
258286
}
259287
}
288+
this._suppressAttributeChange = true;
260289
this.setAttribute('value-min', this._rangeInputs[0].value);
261290
this.setAttribute('value-max', this._rangeInputs[1].value);
291+
this._suppressAttributeChange = false;
292+
this._updateInputValues(); // Ensure number inputs are updated
262293
}
263294

264295
private _updateSliderPosition(value: number, max: number, isMin: boolean) {

0 commit comments

Comments
 (0)