Skip to content

Commit

Permalink
Fix selected index on reset
Browse files Browse the repository at this point in the history
pryley committed May 29, 2021
1 parent ce5dc10 commit 6404f69
Showing 4 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dist/star-rating.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
@@ -81,10 +81,10 @@ <h2>Demo</h2>
<option value="">Select a rating</option>
<option value="10">10</option>
<option value="9">9</option>
<option value="8">8</option>
<option value="8" selected>8</option>
<option value="7">7</option>
<option value="6">6</option>
<option value="5" selected>5</option>
<option value="5">5</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
2 changes: 1 addition & 1 deletion index.js
9 changes: 7 additions & 2 deletions src/widget.js
Original file line number Diff line number Diff line change
@@ -167,11 +167,12 @@ export class Widget {
}

onReset () { // ():void
this.selectValue(this.el.querySelector('[selected]')?.index || -1, false); // do not trigger change event
const index = this.valueIndex(this.el.querySelector('[selected]')?.value)
this.selectValue(index || -1, false); // do not trigger change event
}

selected () { // ():int
return this.values.findIndex(val => val.value === +this.el.value); // get the selected span index
return this.valueIndex(this.el.value); // get the selected span index
}

selectValue (index, triggerChangeEvent) { // (int, bool):void
@@ -183,4 +184,8 @@ export class Widget {
this.el.dispatchEvent(new Event('change'));
}
}

valueIndex (value) {
return this.values.findIndex(val => val.value === +value);
}
}

0 comments on commit 6404f69

Please sign in to comment.