Skip to content

Commit c5dbe47

Browse files
caroqliuaghassemi
authored andcommitted
🐛 Fix selectUp off-by-one bug in amp-selector. (ampproject#20889)
1 parent 6bebd80 commit c5dbe47

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

extensions/amp-selector/0.1/amp-selector.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,12 @@ export class AmpSelector extends AMP.BaseElement {
434434
// The selection should loop around if the user attempts to go one
435435
// past the beginning or end.
436436
const previousIndex = this.elements_.indexOf(this.selectedElements_[0]);
437-
const index = previousIndex + delta;
437+
438+
// If previousIndex === -1 is true, then a negative delta will be offset
439+
// one more than is wanted when looping back around in the options.
440+
// This occurs when no options are selected and "selectUp" is called.
441+
const selectUpWhenNoneSelected = previousIndex === -1 && delta < 0;
442+
const index = selectUpWhenNoneSelected ? delta : previousIndex + delta;
438443
const normalizedIndex = mod(index, this.elements_.length);
439444

440445
this.setSelection_(this.elements_[normalizedIndex]);

0 commit comments

Comments
 (0)