Skip to content
This repository has been archived by the owner on Aug 20, 2022. It is now read-only.

Commit

Permalink
fix: emulate click in touch devices
Browse files Browse the repository at this point in the history
Fixes #197
  • Loading branch information
fengyuanchen committed Jan 12, 2019
1 parent 7304085 commit 85f7cc4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# next

- Emulate click in touch devices to support hiding the picker automatically (#197).

## 1.0.4 (Jan 6, 2019)

- Fix wrong future month selection when today is 31 (#195).
Expand Down
5 changes: 4 additions & 1 deletion src/js/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const WINDOW = typeof window !== 'undefined' ? window : {};
export const IS_BROWSER = typeof window !== 'undefined';
export const WINDOW = IS_BROWSER ? window : {};
export const IS_TOUCH_DEVICE = IS_BROWSER ? 'ontouchstart' in WINDOW.document.documentElement : false;
export const NAMESPACE = 'datepicker';
export const EVENT_CLICK = `click.${NAMESPACE}`;
export const EVENT_FOCUS = `focus.${NAMESPACE}`;
Expand All @@ -8,6 +10,7 @@ export const EVENT_PICK = `pick.${NAMESPACE}`;
export const EVENT_RESIZE = `resize.${NAMESPACE}`;
export const EVENT_SCROLL = `scroll.${NAMESPACE}`;
export const EVENT_SHOW = `show.${NAMESPACE}`;
export const EVENT_TOUCH_START = `touchstart.${NAMESPACE}`;
export const CLASS_HIDE = `${NAMESPACE}-hide`;
export const LANGUAGES = {};
export const VIEWS = {
Expand Down
10 changes: 10 additions & 0 deletions src/js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
EVENT_KEYUP,
EVENT_PICK,
EVENT_SHOW,
EVENT_TOUCH_START,
IS_TOUCH_DEVICE,
LANGUAGES,
NAMESPACE,
VIEWS,
Expand Down Expand Up @@ -191,6 +193,10 @@ class Datepicker {
} else {
$this.on(EVENT_CLICK, $.proxy(this.show, this));
}

if (IS_TOUCH_DEVICE) {
$(document).on(EVENT_TOUCH_START, $.proxy(this.touchstart, this));
}
}
}

Expand Down Expand Up @@ -221,6 +227,10 @@ class Datepicker {
} else {
$this.off(EVENT_CLICK, this.show);
}

if (IS_TOUCH_DEVICE) {
$(document).off(EVENT_TOUCH_START, this.touchstart);
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/js/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,12 @@ export default {
this.hide();
}
},

touchstart({ target }) {
// Emulate click in touch devices to support hiding the picker automatically (#197).
if (this.isInput && target !== this.element && !$.contains(this.$picker, target)) {
this.hide();
this.element.blur();
}
},
};

3 comments on commit 85f7cc4

@fengyuanchen
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change has not released yet.

@fengyuanchen
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just released it.

@fengyuanchen
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just fixed it.

Please sign in to comment.