UNPKG

@txdfe/at

Version:

一个设计体系组件库

160 lines (159 loc) 7.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _classnames = _interopRequireDefault(require("classnames")); var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _utils = require("../utils"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var Selected = exports["default"] = /*#__PURE__*/function (_React$Component) { function Selected() { _classCallCheck(this, Selected); return _callSuper(this, Selected, arguments); } _inherits(Selected, _React$Component); return _createClass(Selected, [{ key: "_getStyle", value: function _getStyle() { var _this$props = this.props, min = _this$props.min, max = _this$props.max, reverse = _this$props.reverse, rtl = _this$props.rtl; var value = this.props.value; if (!Array.isArray(value)) { value = [min, value]; } var width = (value[1] - value[0]) * 100 / (max - min); var style; if (!rtl && !reverse) { // normal select style = { width: "".concat(width, "%"), left: "".concat((0, _utils.getPercent)(min, max, value[0]), "%") }; } else if (rtl && !reverse) { // select in rtl mode style = { width: "".concat(width, "%"), left: "".concat((0, _utils.getPercent)(min, max, max + min - value[1]), "%") }; } else if (!rtl && reverse) { // select in reverse mode style = { width: "".concat(100 - width, "%"), left: "".concat((0, _utils.getPercent)(min, max, value[0]) + width, "%") }; } else { // select in rtl & reverse mode style = { width: "".concat(100 - width, "%"), left: "".concat((0, _utils.getPercent)(min, max, value[0]), "%") }; } return style; } }, { key: "_getStyleLeft", value: function _getStyleLeft() { var _this$props2 = this.props, min = _this$props2.min, max = _this$props2.max, rtl = _this$props2.rtl; var value = this.props.value; if (!Array.isArray(value)) { value = [min, value]; } var style = { width: "".concat((0, _utils.getPercent)(min, max, value[0]), "%"), left: 0 }; if (rtl) { style.width = "".concat(100 - (0, _utils.getPercent)(min, max, value[1]), "%"); } return style; } }, { key: "_getStyleRight", value: function _getStyleRight() { var _this$props3 = this.props, min = _this$props3.min, max = _this$props3.max, rtl = _this$props3.rtl; var value = this.props.value; if (!Array.isArray(value)) { value = [min, value]; } var width = (value[1] - value[0]) * 100 / (max - min); var style = { width: "".concat(100 - (0, _utils.getPercent)(min, max, value[0]) - width, "%"), left: "".concat((0, _utils.getPercent)(min, max, value[0]) + width, "%") }; if (rtl) { style = { width: "".concat((0, _utils.getPercent)(min, max, value[0]), "%"), left: "".concat(100 - (0, _utils.getPercent)(min, max, value[0]), "%") }; } return style; } }, { key: "render", value: function render() { var _this$props4 = this.props, prefix = _this$props4.prefix, slider = _this$props4.slider, reverse = _this$props4.reverse, rtl = _this$props4.rtl; var classes = (0, _classnames["default"])(_defineProperty({}, "".concat(prefix, "range-selected"), true)); var SeletedComps = /*#__PURE__*/_react["default"].createElement("div", { className: classes, style: this._getStyle() }); if (slider === 'double' && reverse) { SeletedComps = /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", { className: classes, style: this._getStyleLeft() }), /*#__PURE__*/_react["default"].createElement("div", { className: classes, style: this._getStyleRight() })); } return SeletedComps; } }]); }(_react["default"].Component); _defineProperty(Selected, "propTypes", { min: _propTypes["default"].number, max: _propTypes["default"].number, slider: _propTypes["default"].string, value: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].arrayOf(_propTypes["default"].number)]), prefix: _propTypes["default"].string, reverse: _propTypes["default"].bool, rtl: _propTypes["default"].bool }); _defineProperty(Selected, "defaultProps", { prefix: 'next-', slider: 'single', min: 0, max: 100, value: 0, reverse: false, rtl: false });