react-input-range
Version:
React component for inputting numeric values within a range
398 lines (328 loc) • 12.2 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _desc, _value, _class;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _autobindDecorator = require('autobind-decorator');
var _autobindDecorator2 = _interopRequireDefault(_autobindDecorator);
var _label = require('./label');
var _label2 = _interopRequireDefault(_label);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
var desc = {};
Object['ke' + 'ys'](descriptor).forEach(function (key) {
desc[key] = descriptor[key];
});
desc.enumerable = !!desc.enumerable;
desc.configurable = !!desc.configurable;
if ('value' in desc || desc.initializer) {
desc.writable = true;
}
desc = decorators.slice().reverse().reduce(function (desc, decorator) {
return decorator(target, property, desc) || desc;
}, desc);
if (context && desc.initializer !== void 0) {
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
if (desc.initializer === void 0) {
Object['define' + 'Property'](target, property, desc);
desc = null;
}
return desc;
}
/**
* @ignore
*/
var Slider = (_class = function (_React$Component) {
_inherits(Slider, _React$Component);
_createClass(Slider, null, [{
key: 'propTypes',
/**
* Accepted propTypes of Slider
* @override
* @return {Object}
* @property {Function} ariaLabelledby
* @property {Function} ariaControls
* @property {Function} className
* @property {Function} formatLabel
* @property {Function} maxValue
* @property {Function} minValue
* @property {Function} onSliderDrag
* @property {Function} onSliderKeyDown
* @property {Function} percentage
* @property {Function} type
* @property {Function} value
*/
get: function get() {
return {
ariaLabelledby: _propTypes2.default.string,
ariaControls: _propTypes2.default.string,
classNames: _propTypes2.default.objectOf(_propTypes2.default.string).isRequired,
formatLabel: _propTypes2.default.func,
maxValue: _propTypes2.default.number,
minValue: _propTypes2.default.number,
onSliderDrag: _propTypes2.default.func.isRequired,
onSliderKeyDown: _propTypes2.default.func.isRequired,
percentage: _propTypes2.default.number.isRequired,
type: _propTypes2.default.string.isRequired,
value: _propTypes2.default.number.isRequired
};
}
/**
* @param {Object} props
* @param {string} [props.ariaLabelledby]
* @param {string} [props.ariaControls]
* @param {InputRangeClassNames} props.classNames
* @param {Function} [props.formatLabel]
* @param {number} [props.maxValue]
* @param {number} [props.minValue]
* @param {Function} props.onSliderKeyDown
* @param {Function} props.onSliderDrag
* @param {number} props.percentage
* @param {number} props.type
* @param {number} props.value
*/
}]);
function Slider(props) {
_classCallCheck(this, Slider);
/**
* @private
* @type {?Component}
*/
var _this = _possibleConstructorReturn(this, (Slider.__proto__ || Object.getPrototypeOf(Slider)).call(this, props));
_this.node = null;
return _this;
}
/**
* @ignore
* @override
* @return {void}
*/
_createClass(Slider, [{
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this.removeDocumentMouseMoveListener();
this.removeDocumentMouseUpListener();
this.removeDocumentTouchEndListener();
this.removeDocumentTouchMoveListener();
}
/**
* @private
* @return {Object}
*/
}, {
key: 'getStyle',
value: function getStyle() {
var perc = (this.props.percentage || 0) * 100;
var style = {
position: 'absolute',
left: perc + '%'
};
return style;
}
/**
* Listen to mousemove event
* @private
* @return {void}
*/
}, {
key: 'addDocumentMouseMoveListener',
value: function addDocumentMouseMoveListener() {
this.removeDocumentMouseMoveListener();
this.node.ownerDocument.addEventListener('mousemove', this.handleMouseMove);
}
/**
* Listen to mouseup event
* @private
* @return {void}
*/
}, {
key: 'addDocumentMouseUpListener',
value: function addDocumentMouseUpListener() {
this.removeDocumentMouseUpListener();
this.node.ownerDocument.addEventListener('mouseup', this.handleMouseUp);
}
/**
* Listen to touchmove event
* @private
* @return {void}
*/
}, {
key: 'addDocumentTouchMoveListener',
value: function addDocumentTouchMoveListener() {
this.removeDocumentTouchMoveListener();
this.node.ownerDocument.addEventListener('touchmove', this.handleTouchMove);
}
/**
* Listen to touchend event
* @private
* @return {void}
*/
}, {
key: 'addDocumentTouchEndListener',
value: function addDocumentTouchEndListener() {
this.removeDocumentTouchEndListener();
this.node.ownerDocument.addEventListener('touchend', this.handleTouchEnd);
}
/**
* @private
* @return {void}
*/
}, {
key: 'removeDocumentMouseMoveListener',
value: function removeDocumentMouseMoveListener() {
this.node.ownerDocument.removeEventListener('mousemove', this.handleMouseMove);
}
/**
* @private
* @return {void}
*/
}, {
key: 'removeDocumentMouseUpListener',
value: function removeDocumentMouseUpListener() {
this.node.ownerDocument.removeEventListener('mouseup', this.handleMouseUp);
}
/**
* @private
* @return {void}
*/
}, {
key: 'removeDocumentTouchMoveListener',
value: function removeDocumentTouchMoveListener() {
this.node.ownerDocument.removeEventListener('touchmove', this.handleTouchMove);
}
/**
* @private
* @return {void}
*/
}, {
key: 'removeDocumentTouchEndListener',
value: function removeDocumentTouchEndListener() {
this.node.ownerDocument.removeEventListener('touchend', this.handleTouchEnd);
}
/**
* @private
* @return {void}
*/
}, {
key: 'handleMouseDown',
value: function handleMouseDown() {
this.addDocumentMouseMoveListener();
this.addDocumentMouseUpListener();
}
/**
* @private
* @return {void}
*/
}, {
key: 'handleMouseUp',
value: function handleMouseUp() {
this.removeDocumentMouseMoveListener();
this.removeDocumentMouseUpListener();
}
/**
* @private
* @param {SyntheticEvent} event
* @return {void}
*/
}, {
key: 'handleMouseMove',
value: function handleMouseMove(event) {
this.props.onSliderDrag(event, this.props.type);
}
/**
* @private
* @return {void}
*/
}, {
key: 'handleTouchStart',
value: function handleTouchStart() {
this.addDocumentTouchEndListener();
this.addDocumentTouchMoveListener();
}
/**
* @private
* @param {SyntheticEvent} event
* @return {void}
*/
}, {
key: 'handleTouchMove',
value: function handleTouchMove(event) {
this.props.onSliderDrag(event, this.props.type);
}
/**
* @private
* @return {void}
*/
}, {
key: 'handleTouchEnd',
value: function handleTouchEnd() {
this.removeDocumentTouchMoveListener();
this.removeDocumentTouchEndListener();
}
/**
* @private
* @param {SyntheticEvent} event
* @return {void}
*/
}, {
key: 'handleKeyDown',
value: function handleKeyDown(event) {
this.props.onSliderKeyDown(event, this.props.type);
}
/**
* @override
* @return {JSX.Element}
*/
}, {
key: 'render',
value: function render() {
var _this2 = this;
var style = this.getStyle();
return _react2.default.createElement(
'span',
{
className: this.props.classNames.sliderContainer,
ref: function ref(node) {
_this2.node = node;
},
style: style },
_react2.default.createElement(
_label2.default,
{
classNames: this.props.classNames,
formatLabel: this.props.formatLabel,
type: 'value' },
this.props.value
),
_react2.default.createElement('div', {
'aria-labelledby': this.props.ariaLabelledby,
'aria-controls': this.props.ariaControls,
'aria-valuemax': this.props.maxValue,
'aria-valuemin': this.props.minValue,
'aria-valuenow': this.props.value,
className: this.props.classNames.slider,
draggable: 'false',
onKeyDown: this.handleKeyDown,
onMouseDown: this.handleMouseDown,
onTouchStart: this.handleTouchStart,
role: 'slider',
tabIndex: '0' })
);
}
}]);
return Slider;
}(_react2.default.Component), (_applyDecoratedDescriptor(_class.prototype, 'handleMouseDown', [_autobindDecorator2.default], Object.getOwnPropertyDescriptor(_class.prototype, 'handleMouseDown'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'handleMouseUp', [_autobindDecorator2.default], Object.getOwnPropertyDescriptor(_class.prototype, 'handleMouseUp'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'handleMouseMove', [_autobindDecorator2.default], Object.getOwnPropertyDescriptor(_class.prototype, 'handleMouseMove'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'handleTouchStart', [_autobindDecorator2.default], Object.getOwnPropertyDescriptor(_class.prototype, 'handleTouchStart'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'handleTouchMove', [_autobindDecorator2.default], Object.getOwnPropertyDescriptor(_class.prototype, 'handleTouchMove'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'handleTouchEnd', [_autobindDecorator2.default], Object.getOwnPropertyDescriptor(_class.prototype, 'handleTouchEnd'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'handleKeyDown', [_autobindDecorator2.default], Object.getOwnPropertyDescriptor(_class.prototype, 'handleKeyDown'), _class.prototype)), _class);
exports.default = Slider;
module.exports = exports['default'];
//# sourceMappingURL=slider.js.map