Skip to content

Commit 8562d49

Browse files
committed
fix(onClick): do not replace the browser's behavior on special clicks
It seems to be a common practice to not do anything if one of the special (shift, ctrl, alt or meta) keys are down: https://github.com/search?q=react+metakey+ctrlkey+preventdefault&type=Code&utf8=%E2%9C%93 Fix #278
1 parent 7f983d0 commit 8562d49

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

components/Pagination/Pagination.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var React = require('react');
22
var forEach = require('lodash/collection/forEach');
33
var defaultsDeep = require('lodash/object/defaultsDeep');
4+
var {isSpecialClick} = require('../../lib/utils.js');
45

56
var Paginator = require('./Paginator');
67
var PaginationLink = require('./PaginationLink');
@@ -14,6 +15,11 @@ class Pagination extends React.Component {
1415
}
1516

1617
handleClick(pageNumber, event) {
18+
if (isSpecialClick(event)) {
19+
// do not alter the default browser behavior
20+
// if one special key is down
21+
return;
22+
}
1723
event.preventDefault();
1824
this.props.setCurrentPage(pageNumber);
1925
}

components/Pagination/PaginationLink.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
var React = require('react');
22

33
class PaginationLink extends React.Component {
4-
handleClick(page, e) {
5-
e.preventDefault();
6-
this.props.setCurrentPage(page);
7-
}
8-
94
render() {
105
var {className, label, ariaLabel, handleClick, url} = this.props;
116

components/RefinementList.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ var cx = require('classnames');
44

55
var Template = require('./Template');
66

7+
var {isSpecialClick} = require('../lib/utils.js');
8+
9+
710
class RefinementList extends React.Component {
811
refine(value) {
912
this.props.toggleRefinement(value);
@@ -53,6 +56,12 @@ class RefinementList extends React.Component {
5356
// Finally, we always stop propagation of the event to avoid multiple levels RefinementLists to fail: click
5457
// on child would click on parent also
5558
handleClick(value, e) {
59+
if (isSpecialClick(e)) {
60+
// do not alter the default browser behavior
61+
// if one special key is down
62+
return;
63+
}
64+
5665
if (e.target.tagName === 'INPUT') {
5766
this.refine(value);
5867
return;

lib/utils.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var utils = {
22
getContainerNode,
33
bemHelper,
4-
prepareTemplateProps
4+
prepareTemplateProps,
5+
isSpecialClick
56
};
67

78
function getContainerNode(selectorOrHTMLElement) {
@@ -28,6 +29,10 @@ function isDomElement(o) {
2829
return o instanceof HTMLElement || o && o.nodeType > 0;
2930
}
3031

32+
function isSpecialClick(event) {
33+
return event.altKey || event.ctrlKey || event.metaKey || event.shiftKey;
34+
}
35+
3136
function bemHelper(block) {
3237
return function(element, modifier) {
3338
if (!element) {

0 commit comments

Comments
 (0)