Skip to content

Commit eb09dfc

Browse files
committed
make selection on iOS more intuitive
allow touchend to cause selection of touched item. Detect touchmove events so that selection of touched item can be canceled
1 parent 85de8cc commit eb09dfc

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

select2.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,9 @@ the specific language governing permissions and limitations under the Apache Lic
715715

716716
installFilteredMouseMove(this.results);
717717
this.dropdown.on("mousemove-filtered touchstart touchmove touchend", resultsSelector, this.bind(this.highlightUnderEvent));
718+
this.dropdown.on("touchend", resultsSelector, this.bind(this.selectHighlighted));
719+
this.dropdown.on("touchmove", resultsSelector, this.bind(this.touchMoved));
720+
this.dropdown.on("touchstart touchend", resultsSelector, this.bind(this.clearTouchMoved));
718721

719722
installDebouncedScroll(80, this.results);
720723
this.dropdown.on("scroll-debounced", resultsSelector, this.bind(this.loadMoreIfNeeded));
@@ -1483,6 +1486,14 @@ the specific language governing permissions and limitations under the Apache Lic
14831486
this.results.find(".select2-highlighted").removeClass("select2-highlighted");
14841487
},
14851488

1489+
touchMoved: function() {
1490+
this._touchMoved = true;
1491+
},
1492+
1493+
clearTouchMoved: function() {
1494+
this._touchMoved = false;
1495+
},
1496+
14861497
// abstract
14871498
countSelectableResults: function() {
14881499
return this.findHighlightableChoices().length;
@@ -1715,6 +1726,10 @@ the specific language governing permissions and limitations under the Apache Lic
17151726

17161727
// abstract
17171728
selectHighlighted: function (options) {
1729+
if (this._touchMoved) {
1730+
this.clearTouchMoved();
1731+
return;
1732+
}
17181733
var index=this.highlight(),
17191734
highlighted=this.results.find(".select2-highlighted"),
17201735
data = highlighted.closest('.select2-result').data("select2-data");

0 commit comments

Comments
 (0)