UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

267 lines (234 loc) 7.71 kB
import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _createSuper(Derived) { function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } return function () { var Super = _getPrototypeOf(Derived), result; if (isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } /* eslint-disable react/prop-types */ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import warning from '../../_util/warning'; import Track from './common/Track'; import createSlider from './common/createSlider'; import * as utils from './utils'; var Slider = /*#__PURE__*/ function (_Component) { _inherits(Slider, _Component); var _super = _createSuper(Slider); function Slider(props) { var _this; _classCallCheck(this, Slider); _this = _super.call(this, props); _defineProperty(_assertThisInitialized(_this), "onEnd", function () { _this.setState({ dragging: false }); _this.removeDocumentEvents(); _this.props.onAfterChange(_this.getValue()); }); var defaultValue = props.defaultValue !== undefined ? props.defaultValue : props.min; var value = props.value !== undefined ? props.value : defaultValue; _this.state = { value: _this.trimAlignValue(value), dragging: false }; if (process.env.NODE_ENV !== 'production') { warning(!('minimumTrackStyle' in props), 'minimumTrackStyle will be deprecate, please use trackStyle instead.'); warning(!('maximumTrackStyle' in props), 'maximumTrackStyle will be deprecate, please use railStyle instead.'); } return _this; } _createClass(Slider, [{ key: "componentDidMount", value: function componentDidMount() { var _this$props = this.props, autoFocus = _this$props.autoFocus, disabled = _this$props.disabled; if (autoFocus && !disabled) { this.focus(); } } }, { key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { if (!('value' in nextProps || 'min' in nextProps || 'max' in nextProps)) return; var prevValue = this.state.value; var value = nextProps.value !== undefined ? nextProps.value : prevValue; var nextValue = this.trimAlignValue(value, nextProps); if (nextValue === prevValue) return; this.setState({ value: nextValue }); if (utils.isValueOutOfRange(value, nextProps)) { this.props.onChange(nextValue); } } }, { key: "onChange", value: function onChange(state) { var props = this.props; var isNotControlled = !('value' in props); if (isNotControlled) { this.setState(state); } var changedValue = state.value; props.onChange(changedValue); } }, { key: "onStart", value: function onStart(position) { this.setState({ dragging: true }); var props = this.props; var prevValue = this.getValue(); props.onBeforeChange(prevValue); var value = this.calcValueByPos(position); this.startValue = value; this.startPosition = position; if (value === prevValue) return; this.prevMovedHandleIndex = 0; this.onChange({ value: value }); } }, { key: "onMove", value: function onMove(e, position) { utils.pauseEvent(e); var oldValue = this.state.value; var value = this.calcValueByPos(position); if (value === oldValue) return; this.onChange({ value: value }); } }, { key: "onKeyboard", value: function onKeyboard(e) { var valueMutator = utils.getKeyboardValueMutator(e); if (valueMutator) { utils.pauseEvent(e); var state = this.state; var oldValue = state.value; var mutatedValue = valueMutator(oldValue, this.props); var value = this.trimAlignValue(mutatedValue); if (value === oldValue) return; this.onChange({ value: value }); } } }, { key: "getValue", value: function getValue() { return this.state.value; } }, { key: "getLowerBound", value: function getLowerBound() { return this.props.min; } }, { key: "getUpperBound", value: function getUpperBound() { return this.state.value; } }, { key: "trimAlignValue", value: function trimAlignValue(v) { var nextProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var mergedProps = _objectSpread({}, this.props, {}, nextProps); var val = utils.ensureValueInRange(v, mergedProps); return utils.ensureValuePrecision(val, mergedProps); } }, { key: "render", value: function render() { var _this2 = this; var _this$props2 = this.props, prefixCls = _this$props2.prefixCls, vertical = _this$props2.vertical, included = _this$props2.included, disabled = _this$props2.disabled, minimumTrackStyle = _this$props2.minimumTrackStyle, trackStyle = _this$props2.trackStyle, handleStyle = _this$props2.handleStyle, tabIndex = _this$props2.tabIndex, min = _this$props2.min, max = _this$props2.max, handleGenerator = _this$props2.handle; var _this$state = this.state, value = _this$state.value, dragging = _this$state.dragging; var offset = this.calcOffset(value); var handle = handleGenerator({ className: "".concat(prefixCls, "-handle"), prefixCls: prefixCls, vertical: vertical, offset: offset, value: value, dragging: dragging, disabled: disabled, min: min, max: max, index: 0, tabIndex: tabIndex, style: handleStyle[0] || handleStyle, ref: function ref(h) { return _this2.saveHandle(0, h); } }); var _trackStyle = trackStyle[0] || trackStyle; var track = React.createElement(Track, { className: "".concat(prefixCls, "-track"), vertical: vertical, included: included, offset: 0, length: offset, style: _objectSpread({}, minimumTrackStyle, {}, _trackStyle) }); return { tracks: track, handles: handle }; } }]); return Slider; }(Component); _defineProperty(Slider, "propTypes", { defaultValue: PropTypes.number, value: PropTypes.number, disabled: PropTypes.bool, autoFocus: PropTypes.bool, tabIndex: PropTypes.number }); export default createSlider(Slider); //# sourceMappingURL=Slider.js.map