Skip to content

Commit c6fcaef

Browse files
committed
Sync attributes and inputs
1 parent 8cc2469 commit c6fcaef

File tree

1 file changed

+22
-39
lines changed

1 file changed

+22
-39
lines changed

src/slider/SliderWebcomponent.ts

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
126126

127127
private _numberInputs: HTMLInputElement[];
128128
private _rangeInputs: HTMLInputElement[];
129-
private _ready: Boolean = false;
130129
private _valuesGap: number = 1;
131130
private _suppressAttributeChange: boolean = false;
132131

@@ -137,6 +136,8 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
137136
this._suppressAttributeChange = true;
138137
this.setAttribute('value-min', value.toString());
139138
this._suppressAttributeChange = false;
139+
this._updateInputValues();
140+
this._updateRangeInputsValues();
140141
}
141142

142143
public get valueMax() {
@@ -146,6 +147,8 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
146147
this._suppressAttributeChange = true;
147148
this.setAttribute('value-max', value.toString());
148149
this._suppressAttributeChange = false;
150+
this._updateInputValues();
151+
this._updateRangeInputsValues();
149152
}
150153

151154
public get min() {
@@ -155,6 +158,7 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
155158
this._suppressAttributeChange = true;
156159
this.setAttribute('min', value.toString());
157160
this._suppressAttributeChange = false;
161+
this._updateRangeInputsMinMax();
158162
}
159163

160164
public get max() {
@@ -164,21 +168,19 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
164168
this._suppressAttributeChange = true;
165169
this.setAttribute('max', value.toString());
166170
this._suppressAttributeChange = false;
171+
this._updateRangeInputsMinMax();
167172
}
168173

169174
attributeChangedCallback(name: string, oldValue: string, newValue: string) {
170175
if (this._suppressAttributeChange) return;
171-
if (name == "value-min") {
172-
this._valueMinAttributeChanged();
173-
}
174-
if (name === "value-max") {
175-
this._valueMaxAttributeChanged();
176-
}
177-
if (name == "min") {
178-
this._minAttributeChanged();
176+
if (name === "value-min" || name === "value-max") {
177+
this._updateInputValues();
178+
this._updateRangeInputsValues();
179+
this._updateSliderPosition(parseInt(this.valueMin), parseInt(this._rangeInputs[0].max), true);
180+
this._updateSliderPosition(parseInt(this.valueMax), parseInt(this._rangeInputs[1].max), false);
179181
}
180-
if (name === "max") {
181-
this._maxAttributeChanged();
182+
if (name === "min" || name === "max") {
183+
this._updateRangeInputsMinMax();
182184
}
183185
}
184186

@@ -190,6 +192,7 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
190192
connectedCallback() {
191193
this._numberInputs = Array.from(this._getDomElements(".inputs-wrapper input"));
192194
this._rangeInputs = Array.from(this._getDomElements(".range-input input"));
195+
this.ready();
193196
}
194197

195198
disconnectedCallback() { }
@@ -224,10 +227,8 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
224227
}
225228
} else {
226229
// Update input values and range progress
227-
this._numberInputs[0].value = minVal.toString();
228-
this._numberInputs[1].value = maxVal.toString();
229-
this._updateSliderPosition(minVal, parseInt(this._rangeInputs[0].max), true);
230-
this._updateSliderPosition(maxVal, parseInt(this._rangeInputs[1].max), false);
230+
this.valueMin = minVal.toString();
231+
this.valueMax = maxVal.toString();
231232
}
232233
});
233234

@@ -240,10 +241,9 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
240241
});
241242
}
242243

243-
this._ready = true;
244-
245244
this._updateInputValues();
246-
this._updateRangeInputsMinMaxAndValue();
245+
this._updateRangeInputsMinMax();
246+
this._updateRangeInputsValues();
247247
this._updateSliderPosition(parseInt(this.valueMin), parseInt(this._rangeInputs[0].max), true);
248248
this._updateSliderPosition(parseInt(this.valueMax), parseInt(this._rangeInputs[1].max), false);
249249
}
@@ -253,11 +253,14 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
253253
this._numberInputs[1].value = this.valueMax.toString();
254254
}
255255

256-
private _updateRangeInputsMinMaxAndValue() {
256+
private _updateRangeInputsMinMax() {
257257
this._rangeInputs.forEach(rangeInput => {
258258
rangeInput.min = this.min.toString();
259259
rangeInput.max = this.max.toString();
260260
});
261+
}
262+
263+
private _updateRangeInputsValues() {
261264
this._rangeInputs[0].value = this.valueMin.toString();
262265
this._rangeInputs[1].value = this.valueMax.toString();
263266
}
@@ -319,26 +322,6 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
319322
rangevalue.style.right = `${100 - (relativeValue / range) * 100}%`;
320323
}
321324
}
322-
323-
private _valueMinAttributeChanged() {
324-
if (!this._ready) return;
325-
this._handleInputChange({ target: this._numberInputs[0] } as unknown as Event);
326-
}
327-
328-
private _valueMaxAttributeChanged() {
329-
if (!this._ready) return;
330-
this._handleInputChange({ target: this._numberInputs[1] } as unknown as Event);
331-
}
332-
333-
private _minAttributeChanged() {
334-
if (!this._ready) return;
335-
this._updateRangeInputsMinMaxAndValue();
336-
}
337-
338-
private _maxAttributeChanged() {
339-
if (!this._ready) return;
340-
this._updateRangeInputsMinMaxAndValue();
341-
}
342325
}
343326

344327
customElements.define(SliderWebcomponent.is, SliderWebcomponent);

0 commit comments

Comments
 (0)