forked from JedWatson/react-select
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOption.js
117 lines (96 loc) · 3.62 KB
/
Option.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _createReactClass = require('create-react-class');
var _createReactClass2 = _interopRequireDefault(_createReactClass);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var Option = (0, _createReactClass2['default'])({
propTypes: {
children: _propTypes2['default'].node,
className: _propTypes2['default'].string, // className (based on mouse position)
instancePrefix: _propTypes2['default'].string.isRequired, // unique prefix for the ids (used for aria)
isDisabled: _propTypes2['default'].bool, // the option is disabled
isFocused: _propTypes2['default'].bool, // the option is focused
isSelected: _propTypes2['default'].bool, // the option is selected
onFocus: _propTypes2['default'].func, // method to handle mouseEnter on option element
onSelect: _propTypes2['default'].func, // method to handle click on option element
onUnfocus: _propTypes2['default'].func, // method to handle mouseLeave on option element
option: _propTypes2['default'].object.isRequired, // object that is base for that option
optionIndex: _propTypes2['default'].number },
// index of the option, used to generate unique ids for aria
blockEvent: function blockEvent(event) {
event.preventDefault();
event.stopPropagation();
if (event.target.tagName !== 'A' || !('href' in event.target)) {
return;
}
if (event.target.target) {
window.open(event.target.href, event.target.target);
} else {
window.location.href = event.target.href;
}
},
handleMouseDown: function handleMouseDown(event) {
event.preventDefault();
event.stopPropagation();
this.props.onSelect(this.props.option, event);
},
handleMouseEnter: function handleMouseEnter(event) {
this.onFocus(event);
},
handleMouseMove: function handleMouseMove(event) {
this.onFocus(event);
},
handleTouchEnd: function handleTouchEnd(event) {
// Check if the view is being dragged, In this case
// we don't want to fire the click event (because the user only wants to scroll)
if (this.dragging) return;
this.handleMouseDown(event);
},
handleTouchMove: function handleTouchMove(event) {
// Set a flag that the view is being dragged
this.dragging = true;
},
handleTouchStart: function handleTouchStart(event) {
// Set a flag that the view is not being dragged
this.dragging = false;
},
onFocus: function onFocus(event) {
if (!this.props.isFocused) {
this.props.onFocus(this.props.option, event);
}
},
render: function render() {
var _props = this.props;
var option = _props.option;
var instancePrefix = _props.instancePrefix;
var optionIndex = _props.optionIndex;
var className = (0, _classnames2['default'])(this.props.className, option.className);
return option.disabled ? _react2['default'].createElement(
'div',
{ className: className,
onMouseDown: this.blockEvent,
onClick: this.blockEvent },
this.props.children
) : _react2['default'].createElement(
'div',
{ className: className,
style: option.style,
role: 'option',
onMouseDown: this.handleMouseDown,
onMouseEnter: this.handleMouseEnter,
onMouseMove: this.handleMouseMove,
onTouchStart: this.handleTouchStart,
onTouchMove: this.handleTouchMove,
onTouchEnd: this.handleTouchEnd,
id: instancePrefix + '-option-' + optionIndex,
title: option.title },
this.props.children
);
}
});
module.exports = Option;