Skip to content

Commit e29aa4b

Browse files
committed
Update slider bar on dynamically on input but update attribute with change only
1 parent d6049ac commit e29aa4b

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

src/slider/SliderWebcomponent.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,18 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
158158
}
159159

160160
attributeChangedCallback(name: string, oldValue: string, newValue: string) {
161-
if (name == "value-min"){
162-
this._valueMinAttributeChanged();
163-
}
164-
if (name === "value-max"){
165-
this._valueMaxAttributeChanged();
166-
}
167-
if (name == "min"){
168-
this._minAttributeChanged();
169-
}
170-
if (name === "max"){
171-
this._maxAttributeChanged();
172-
}
161+
if (name == "value-min") {
162+
this._valueMinAttributeChanged();
163+
}
164+
if (name === "value-max") {
165+
this._valueMaxAttributeChanged();
166+
}
167+
if (name == "min") {
168+
this._minAttributeChanged();
169+
}
170+
if (name === "max") {
171+
this._maxAttributeChanged();
172+
}
173173
}
174174

175175
constructor() {
@@ -182,7 +182,7 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
182182
this._rangeInputs = Array.from(this._getDomElements(".range-input input"));
183183
}
184184

185-
disconnectedCallback() {}
185+
disconnectedCallback() { }
186186

187187
ready() {
188188
this._parseAttributesToProperties();
@@ -196,36 +196,41 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
196196
});
197197
}
198198

199-
// Add event listeners to range input elements
199+
// Add event listeners to range input elements
200200
for (let i = 0; i < this._rangeInputs.length; i++) {
201-
this._rangeInputs[i].addEventListener("change", e => {
201+
this._rangeInputs[i].addEventListener("input", e => {
202202
let minVal = parseInt(this._rangeInputs[0].value);
203203
let maxVal = parseInt(this._rangeInputs[1].value);
204204

205205
let diff = maxVal - minVal;
206206

207-
// Check if the values gap is exceeded
207+
// Check if the values gap is exceeded
208208
if (diff < this._valuesGap) {
209-
210-
// Check if the input is the min range input
209+
// Check if the input is the min range input
211210
if ((e.target as HTMLInputElement).className === "min-range") {
212211
this._rangeInputs[0].value = (maxVal - this._valuesGap).toString();
213212
} else {
214213
this._rangeInputs[1].value = (minVal + this._valuesGap).toString();
215214
}
216215
} else {
217-
218-
// Update input values and range progress
216+
// Update input values and range progress
219217
this._numberInputs[0].value = minVal.toString();
220218
this._numberInputs[1].value = maxVal.toString();
221-
this.valueMin = minVal.toString();
222-
this.valueMax = maxVal.toString();
223-
this._updateSliderPosition(parseInt(this.valueMin), parseInt(this._rangeInputs[0].max), true);
224-
this._updateSliderPosition(parseInt(this.valueMax), parseInt(this._rangeInputs[1].max), false);
219+
this._updateSliderPosition(minVal, parseInt(this._rangeInputs[0].max), true);
220+
this._updateSliderPosition(maxVal, parseInt(this._rangeInputs[1].max), false);
225221
}
226222
});
223+
224+
this._rangeInputs[i].addEventListener("change", e => {
225+
let minVal = parseInt(this._rangeInputs[0].value);
226+
let maxVal = parseInt(this._rangeInputs[1].value);
227+
228+
this.valueMin = minVal.toString();
229+
this.valueMax = maxVal.toString();
230+
});
227231
}
228232

233+
229234
this._ready = true;
230235

231236
this._updateInputValues();
@@ -294,15 +299,15 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
294299
private _updateSliderPosition(value: number, max: number, isMin: boolean) {
295300
const rangevalue: HTMLDivElement = this._getDomElement("slider");
296301
const range = parseInt(this.max) - parseInt(this.min); // Calculate the total range
297-
302+
298303
if (isMin) {
299304
const relativeValue = value - parseInt(this.min); // Calculate the relative value within the range
300305
rangevalue.style.left = `${(relativeValue / range) * 100}%`;
301306
} else {
302307
const relativeValue = value - parseInt(this.min); // Calculate the relative value within the range
303308
rangevalue.style.right = `${100 - (relativeValue / range) * 100}%`;
304309
}
305-
}
310+
}
306311

307312
private _valueMinAttributeChanged() {
308313
if (!this._ready) return;

0 commit comments

Comments
 (0)