UNPKG

@yncoder/element-react

Version:
476 lines (401 loc) 14.8 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends2 = require('babel-runtime/helpers/extends'); var _extends3 = _interopRequireDefault(_extends2); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _react = require('react'); var React = _interopRequireWildcard(_react); var _libs = require('../../libs'); var _inputNumber = require('../input-number'); var _inputNumber2 = _interopRequireDefault(_inputNumber); var _Button = require('./Button'); var _Button2 = _interopRequireDefault(_Button); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } (function () { var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined; enterModule && enterModule(module); })(); var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) { return a; }; var Slider = function (_Component) { (0, _inherits3.default)(Slider, _Component); function Slider() { var _ref; var _temp, _this, _ret; (0, _classCallCheck3.default)(this, Slider); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Slider.__proto__ || Object.getPrototypeOf(Slider)).call.apply(_ref, [this].concat(args))), _this), _this.state = { firstValue: 0, secondValue: 0, draggingValue: _this.props.value, precision: 0, inputValue: 0 }, _this.slider = React.createRef(), _this.button1 = React.createRef(), _this.button2 = React.createRef(), _temp), (0, _possibleConstructorReturn3.default)(_this, _ret); } (0, _createClass3.default)(Slider, [{ key: 'getChildContext', value: function getChildContext() { return { component: this }; } }, { key: 'setValues', value: function setValues(state) { var _props = this.props, range = _props.range, min = _props.min, max = _props.max, _props$triggerOnDragg = _props.triggerOnDragging, triggerOnDragging = _props$triggerOnDragg === undefined ? false : _props$triggerOnDragg, onChange = _props.onChange; var states = (0, _extends3.default)({}, this.state, state); var firstValue = states.firstValue, secondValue = states.secondValue; var inputValue = states.inputValue; var changes = void 0; if (range) { if (secondValue < min) { changes = [min, min]; } else if (firstValue > max) { changes = [max, max]; } else if (firstValue < min) { changes = [min, secondValue]; } else if (secondValue > max) { changes = [firstValue, max]; } else { changes = [Math.min(firstValue, secondValue), Math.max(firstValue, secondValue)]; } } else if (typeof firstValue === 'number' && !isNaN(firstValue)) { var initValue = Slider.getInitValue(firstValue, this.props); if (initValue < min) { changes = min; } else if (initValue > max) { changes = max; } else { changes = firstValue; inputValue = changes; } } this.setState({ firstValue: firstValue, secondValue: secondValue, inputValue: inputValue, draggingValue: changes }); if (typeof changes !== 'undefined' && onChange && !(Number(triggerOnDragging) ^ Number(this.dragging))) { onChange(changes); } } }, { key: 'setPosition', value: function setPosition(percent) { var _props2 = this.props, range = _props2.range, min = _props2.min, max = _props2.max; var _state = this.state, firstValue = _state.firstValue, secondValue = _state.secondValue; var targetValue = min + percent * (max - min) / 100; if (!range) { this.button1 && this.button1.current.setPosition(percent); return; } var button = void 0; if (Math.abs(Math.min(firstValue, secondValue) - targetValue) < Math.abs(Math.max(firstValue, secondValue) - targetValue)) { button = firstValue < secondValue ? 'button1' : 'button2'; } else { button = firstValue > secondValue ? 'button1' : 'button2'; } this[button] && this[button].current.setPosition(percent); } }, { key: 'onSliderClick', value: function onSliderClick(event) { var _props3 = this.props, disabled = _props3.disabled, vertical = _props3.vertical; if (disabled || this.dragging || !this.slider.current) return; if (vertical) { var sliderOffsetBottom = this.slider.current.getBoundingClientRect().bottom; this.setPosition((sliderOffsetBottom - event.clientY) / this.sliderSize() * 100); } else { var sliderOffsetLeft = this.slider.current.getBoundingClientRect().left; this.setPosition((event.clientX - sliderOffsetLeft) / this.sliderSize() * 100); } this.setValues(this.state); } }, { key: 'onInputValueChanged', value: function onInputValueChanged(value) { this.setValues({ inputValue: value || 0, firstValue: value || 0 }); } /* Computed Methods */ }, { key: 'sliderSize', value: function sliderSize() { var vertical = this.props.vertical; if (!this.slider.current) return 0; return parseInt(vertical ? this.slider.current.offsetHeight : this.slider.current.offsetWidth, 10); } }, { key: 'stops', value: function stops() { var _props4 = this.props, range = _props4.range, min = _props4.min, max = _props4.max, step = _props4.step; var _state2 = this.state, firstValue = _state2.firstValue, secondValue = _state2.secondValue; var stopCount = (max - min) / step; var stepWidth = 100 * step / (max - min); var result = []; for (var i = 1; i < stopCount; i++) { result.push(i * stepWidth); } if (range) { return result.filter(function (step) { return step < 100 * (Math.min(firstValue, secondValue) - min) / (max - min) || step > 100 * (Math.max(firstValue, secondValue) - min) / (max - min); }); } else { return result.filter(function (step) { return step > 100 * (firstValue - min) / (max - min); }); } } }, { key: 'runwayStyle', value: function runwayStyle() { var _props5 = this.props, vertical = _props5.vertical, height = _props5.height; return vertical ? { height: height } : {}; } }, { key: 'barStyle', value: function barStyle() { var vertical = this.props.vertical; return vertical ? { height: this.barSize(), bottom: this.barStart() } : { width: this.barSize(), left: this.barStart() }; } }, { key: 'barSize', value: function barSize() { var _state3 = this.state, firstValue = _state3.firstValue, secondValue = _state3.secondValue; var _props6 = this.props, range = _props6.range, max = _props6.max, min = _props6.min; return range ? 100 * (Math.max(firstValue, secondValue) - Math.min(firstValue, secondValue)) / (max - min) + '%' : 100 * (firstValue - min) / (max - min) + '%'; } }, { key: 'barStart', value: function barStart() { var _state4 = this.state, firstValue = _state4.firstValue, secondValue = _state4.secondValue; var _props7 = this.props, range = _props7.range, max = _props7.max, min = _props7.min; return range ? 100 * (Math.min(firstValue, secondValue) - min) / (max - min) + '%' : '0%'; } }, { key: 'render', value: function render() { var _this2 = this; var _props8 = this.props, vertical = _props8.vertical, showInput = _props8.showInput, showStops = _props8.showStops, showInputControls = _props8.showInputControls, range = _props8.range, step = _props8.step, disabled = _props8.disabled, min = _props8.min, max = _props8.max; var _state5 = this.state, inputValue = _state5.inputValue, firstValue = _state5.firstValue, secondValue = _state5.secondValue; return React.createElement( 'div', { className: this.className('el-slider', { 'is-vertical': vertical, 'el-slider--with-input': showInput }) }, showInput && !range && React.createElement(_inputNumber2.default, { ref: 'input', className: 'el-slider__input', defaultValue: inputValue, value: firstValue, step: step, disabled: disabled, controls: showInputControls, min: min, max: max, size: 'small', onChange: this.onInputValueChanged.bind(this) }), React.createElement( 'div', { ref: this.slider, style: this.runwayStyle(), className: this.classNames('el-slider__runway', { 'show-input': showInput, 'disabled': disabled }), onClick: this.onSliderClick.bind(this) }, React.createElement('div', { className: 'el-slider__bar', style: this.barStyle() }), React.createElement(_Button2.default, { ref: this.button1, vertical: vertical, value: firstValue, onChange: function onChange(v) { return _this2.setValues({ firstValue: v }); } }), range && React.createElement(_Button2.default, { ref: this.button2, vertical: vertical, value: secondValue, onChange: function onChange(v) { return _this2.setValues({ secondValue: v }); } }), showStops && this.stops().map(function (item, index) { return React.createElement('div', { key: index, className: 'el-slider__stop', style: vertical ? { 'bottom': item + '%' } : { 'left': item + '%' } }); }) ) ); } }, { key: '__reactstandin__regenerateByEval', // @ts-ignore value: function __reactstandin__regenerateByEval(key, code) { // @ts-ignore this[key] = eval(code); } }, { key: 'dragging', get: function get() { return this.button1.current && this.button1.current.state.dragging || this.button2.current && this.button2.current.state.dragging; } }], [{ key: 'getDerivedStateFromProps', value: function getDerivedStateFromProps(props, state) { var range = props.range, value = props.value, min = props.min, max = props.max, step = props.step; var nextValue = state.draggingValue; if (range) { if (Array.isArray(nextValue)) { state.firstValue = Math.max(min, nextValue[0]); state.secondValue = Math.min(max, nextValue[1]); } else { state.firstValue = min; state.secondValue = max; } state.draggingValue = [state.firstValue, state.secondValue]; } else { state.firstValue = Slider.getInitValue(nextValue, props); state.draggingValue = state.firstValue; } var precisions = [min, max, step].map(function (item) { var decimal = ('' + item).split('.')[1]; return decimal ? decimal.length : 0; }); state.precision = Math.max.apply(null, precisions); state.inputValue = state.inputValue || state.firstValue; return state; } }, { key: 'getInitValue', value: function getInitValue(value, _ref2) { var min = _ref2.min, max = _ref2.max; var initValue = value; if (typeof value !== 'number' || isNaN(value)) { initValue = min; } else { initValue = Math.min(max, Math.max(min, value)); } return initValue; } }]); return Slider; }(_libs.Component); var _default = Slider; exports.default = _default; Slider.childContextTypes = { component: _libs.PropTypes.any }; Slider.propTypes = { min: _libs.PropTypes.oneOfType([_libs.PropTypes.number, _libs.PropTypes.string]), max: _libs.PropTypes.oneOfType([_libs.PropTypes.number, _libs.PropTypes.string]), step: _libs.PropTypes.oneOfType([_libs.PropTypes.number, _libs.PropTypes.string]), value: _libs.PropTypes.oneOfType([_libs.PropTypes.number, _libs.PropTypes.arrayOf(_libs.PropTypes.number)]), showInput: _libs.PropTypes.bool, showInputControls: _libs.PropTypes.bool, showTooltip: _libs.PropTypes.bool, showStops: _libs.PropTypes.bool, disabled: _libs.PropTypes.bool, range: _libs.PropTypes.bool, vertical: _libs.PropTypes.bool, height: _libs.PropTypes.string, formatTooltip: _libs.PropTypes.func, onChange: _libs.PropTypes.func, triggerOnDragging: _libs.PropTypes.bool }; Slider.defaultProps = { showTooltip: true, showInputControls: true, min: 0, max: 100, step: 1, value: 0, triggerOnDragging: false }; ; (function () { var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined; if (!reactHotLoader) { return; } reactHotLoader.register(Slider, 'Slider', 'src/slider/Slider.jsx'); reactHotLoader.register(_default, 'default', 'src/slider/Slider.jsx'); })(); ; (function () { var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined; leaveModule && leaveModule(module); })();