react-select-plus
Version:
A fork of react-select with support for option groups
106 lines (87 loc) • 3.13 kB
JavaScript
'use strict';
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var Value = _react2['default'].createClass({
displayName: 'Value',
propTypes: {
children: _react2['default'].PropTypes.node,
disabled: _react2['default'].PropTypes.bool, // disabled prop passed to ReactSelect
id: _react2['default'].PropTypes.string, // Unique id for the value - used for aria
onClick: _react2['default'].PropTypes.func, // method to handle click on value label
onRemove: _react2['default'].PropTypes.func, // method to handle removal of the value
value: _react2['default'].PropTypes.object.isRequired },
// the option object for this value
handleMouseDown: function handleMouseDown(event) {
if (event.type === 'mousedown' && event.button !== 0) {
return;
}
if (this.props.onClick) {
event.stopPropagation();
this.props.onClick(this.props.value, event);
return;
}
if (this.props.value.href) {
event.stopPropagation();
}
},
onRemove: function onRemove(event) {
event.preventDefault();
event.stopPropagation();
this.props.onRemove(this.props.value);
},
handleTouchEndRemove: function handleTouchEndRemove(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;
// Fire the mouse events
this.onRemove(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;
},
renderRemoveIcon: function renderRemoveIcon() {
if (this.props.disabled || !this.props.onRemove) return;
return _react2['default'].createElement(
'span',
{ className: 'Select-value-icon',
'aria-hidden': 'true',
onMouseDown: this.onRemove,
onTouchEnd: this.handleTouchEndRemove,
onTouchStart: this.handleTouchStart,
onTouchMove: this.handleTouchMove },
'×'
);
},
renderLabel: function renderLabel() {
var className = 'Select-value-label';
return this.props.onClick || this.props.value.href ? _react2['default'].createElement(
'a',
{ className: className, href: this.props.value.href, target: this.props.value.target, onMouseDown: this.handleMouseDown, onTouchEnd: this.handleMouseDown },
this.props.children
) : _react2['default'].createElement(
'span',
{ className: className, role: 'option', 'aria-selected': 'true', id: this.props.id },
this.props.children
);
},
render: function render() {
return _react2['default'].createElement(
'div',
{ className: (0, _classnames2['default'])('Select-value', this.props.value.className),
style: this.props.value.style,
title: this.props.value.title
},
this.renderRemoveIcon(),
this.renderLabel()
);
}
});
module.exports = Value;