Skip to content

Commit

Permalink
feat: introduce .defaultValue (#31)
Browse files Browse the repository at this point in the history
This re-implements the `defaultValue` property of the HTMLInputElement to keep track of the initial value.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement
  • Loading branch information
ItsMos authored Dec 21, 2020
1 parent d16a6c6 commit e29b00c
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions range-slider-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class RangeSliderElement extends HTMLElement {
super();
this._ignoreChange = false;
this._isRTL = this.getAttribute('dir') === 'rtl';
this._defaultValue = this.value;
}

static get observedAttributes() {
return REFLECTED_ATTRIBUTES;
}

get _defaultValue() {
get _computedValue() {
const min = Number(this.min);
const max = Number(this.max);
return String(max < min ? min : min + (max - min) / 2);
Expand All @@ -35,16 +36,18 @@ class RangeSliderElement extends HTMLElement {
get min() { return this.getAttribute('min') || '0'; }
get max() { return this.getAttribute('max') || '100'; }
get step() { return this.getAttribute('step') || '1'; }
get value() { return this.getAttribute('value') || this._defaultValue; }
get value() { return this.getAttribute('value') || this._computedValue(); }
get disabled() { return this.getAttribute('disabled') || false }
get valuePrecision() { return this.getAttribute('value-precision') || ''; }
get defaultValue() { return this._defaultValue; }

set min(min) { this.setAttribute('min', min); }
set max(max) { this.setAttribute('max', max); }
set step(step) { this.setAttribute('step', step); }
set value(value) { this.setAttribute('value', value); }
set disabled(disabled) { this.setAttribute('disabled', disabled); }
set valuePrecision(precision) { this.setAttribute('value-precision', precision); }
set defaultValue(value) { this._defaultValue = value; }

connectedCallback() {
if (!this.firstChild) {
Expand Down

0 comments on commit e29b00c

Please sign in to comment.