wix-style-react
Version:
wix-style-react
175 lines (140 loc) • 5.73 kB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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; }; }();
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; }
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Range } from 'rc-slider';
import uniqueId from 'lodash/uniqueId';
import SliderHandle from './SliderHandle';
import classNames from 'classnames';
import './Slider.scss';
/**
* A slider component with multi-range support
*/
var Slider = function (_Component) {
_inherits(Slider, _Component);
function Slider() {
_classCallCheck(this, Slider);
return _possibleConstructorReturn(this, (Slider.__proto__ || Object.getPrototypeOf(Slider)).apply(this, arguments));
}
_createClass(Slider, [{
key: 'getRange',
value: function getRange() {
var _props = this.props,
min = _props.min,
max = _props.max,
step = _props.step;
var range = [];
for (var i = min; i <= max; i += step) {
range.push(i);
}
return range;
}
}, {
key: 'renderLabel',
value: function renderLabel(value) {
var _props2 = this.props,
min = _props2.min,
max = _props2.max;
return React.createElement(
'div',
{ className: 'mark' },
React.createElement('div', { className: 'mark-line' }),
React.createElement(
'div',
{ className: 'mark-value' },
(value === min || value === max) && React.createElement(
'div',
null,
value
)
)
);
}
}, {
key: 'getMarks',
value: function getMarks() {
var _this2 = this;
return this.getRange().reduce(function (acc, cur) {
acc[cur] = {
label: _this2.renderLabel(cur)
};
return acc;
}, {});
}
}, {
key: 'render',
value: function render() {
var _this3 = this;
var marks = this.props.displayMarks ? this.getMarks() : {};
var dataHook = this.props.dataHook;
return React.createElement(
'div',
{
className: classNames('wix-slider', { rtl: this.props.rtl }),
id: this.props.id,
'data-hook': dataHook
},
React.createElement(Range, {
handle: function handle(props) {
return React.createElement(SliderHandle, _extends({
key: props.index,
displayTooltip: _this3.props.displayTooltip
}, props));
},
min: this.props.min,
max: this.props.max,
value: this.props.value,
marks: marks,
step: this.props.step,
pushable: this.props.pushable,
onChange: this.props.onChange,
onAfterChange: this.props.onAfterChange,
allowCross: this.props.allowCross
})
);
}
}]);
return Slider;
}(Component);
export { Slider as default };
Slider.displayName = 'Slider';
Slider.propTypes = {
/** Allows the slider's handles to cross. */
allowCross: PropTypes.bool,
dataHook: PropTypes.string,
/** Controls the visibility of the marks. */
displayMarks: PropTypes.bool,
/** Controls visibility of slide handle tooltip */
displayTooltip: PropTypes.bool,
id: PropTypes.string,
/** The absolute maximum of the slider's range */
max: PropTypes.number,
/** The absolute minimum of the slider's range */
min: PropTypes.number,
/** Called after every value change */
onAfterChange: PropTypes.func,
/** Called upon every value change */
onChange: PropTypes.func.isRequired,
/** Adjust for RTL dir. */
rtl: PropTypes.bool,
/** The slider's step */
step: PropTypes.number,
/** Allow pushing of surrounding handles when moving a handle. Number means a minimum distance between handles */
pushable: PropTypes.oneOfType([PropTypes.bool, PropTypes.number]),
/** The slider's selected range */
value: PropTypes.arrayOf(PropTypes.number)
};
Slider.defaultProps = {
min: 1,
max: 20,
step: 1,
value: [2, 7],
allowCross: true,
id: uniqueId(),
displayTooltip: true,
displayMarks: true,
rtl: false
};