UNPKG

choerodon-ui

Version:

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

225 lines (199 loc) 6.64 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 _createSuper from "@babel/runtime/helpers/createSuper"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; /* eslint-disable react/prop-types */ import React, { Component } from 'react'; 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(_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 = /*#__PURE__*/React.createElement(Track, { className: "".concat(prefixCls, "-track"), vertical: vertical, included: included, offset: 0, length: offset, style: _objectSpread(_objectSpread({}, minimumTrackStyle), _trackStyle) }); return { tracks: track, handles: handle }; } }]); return Slider; }(Component); export default createSlider(Slider); //# sourceMappingURL=Slider.js.map