UNPKG

frc-ui

Version:

React Web UI

257 lines (200 loc) 10.7 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _radio = _interopRequireDefault(require("antd/es/radio")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var __rest = void 0 && (void 0).__rest || function (s, e) { var t = {}; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; } if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var Group = _radio["default"].Group, Button = _radio["default"].Button; var Tabs = /*#__PURE__*/function (_React$PureComponent) { _inherits(Tabs, _React$PureComponent); var _super = _createSuper(Tabs); function Tabs(props) { var _this; _classCallCheck(this, Tabs); _this = _super.call(this, props); _this.h_change = function (ev) { var _this$props = _this.props, multi = _this$props.multi, onChange = _this$props.onChange, options = _this$props.options, showAll = _this$props.showAll; var stateValue = _this.state.value; var value = ev.target.value; if (value === undefined) return; var disabled = options.some(function (o) { return o.value === value && o.disabled === true; }); if (disabled) return; if (multi) { if (!!ev.nativeEvent.ctrlKey) { if (value === 'all') { value = ['all']; } else { var sv = stateValue || []; if (sv instanceof Array) { if (!sv.some(function (va) { return va === value; })) { value = [].concat(_toConsumableArray(sv.filter(function (v) { return v !== 'all'; })), [value]); } else { value = sv.filter(function (v) { return v !== value; }); } } } if (showAll) { var sa = options.some(function (o) { return !o.disabled && value.indexOf(o.value) < 0; }); if (!sa || value.length === 0) { value = ['all']; } } } else { value = [value]; } } _this.setState({ value: value }, function () { if (typeof onChange === 'function') { onChange(value, ev.target.value); } }); }; _this.f_checked = function (v) { var value = _this.state.value; var vs = value || []; return vs instanceof Array && vs.some(function (va) { return va === v; }) || value === v; }; _this.r_button = function () { var showAll = _this.props.showAll; var options = _this.props.options || []; if (showAll) { var all = { label: '全部', value: 'all' }; options = [all].concat(_toConsumableArray(options)); } return (options || []).map(function (o) { var _classNames; var style = { minWidth: _this.props.itemWidth }; var checked = _this.f_checked(o.value); checked && (style.background = '#FF9300'); checked && (style.color = '#1E1E1E'); o.width && (style.width = o.width); checked && o.background && (style.background = o.background); checked && o.color && (style.color = o.color); var cls = (0, _classnames["default"])((_classNames = {}, _defineProperty(_classNames, 'swc-tabs-checked', checked), _defineProperty(_classNames, 'swc-tabs-disabled', o.disabled), _classNames)); if (typeof o.render === 'function') { var _props = { style: style, className: cls, children: o.label }; return o.render(Button, _props); } return /*#__PURE__*/_react["default"].createElement(Button, { key: o.value, style: style, value: o.value, className: cls, checked: false }, o.label); }); }; var showAll = props.showAll, multi = props.multi; var value = props.value; if (showAll && !value) { value = value || (multi ? ['all'] : 'all'); } _this.state = { value: value }; return _this; } _createClass(Tabs, [{ key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { if (nextProps.value !== this.props.value) { this.setState({ value: nextProps.value }); } } }, { key: "render", value: function render() { var _a = this.props, className = _a.className, children = _a.children, label = _a.label, other = __rest(_a, ["className", "children", "label"]); var value = this.state.value; delete other.onChange; delete other.value; delete other.options; var cls = (0, _classnames["default"])('swc-tabs', className); var child = /*#__PURE__*/_react["default"].createElement(Group, Object.assign({}, other, { className: cls, onChange: this.h_change, value: value }), children || this.r_button()); if (label) { return /*#__PURE__*/_react["default"].createElement("div", { className: 'swc-tabs-wrapper' }, /*#__PURE__*/_react["default"].createElement("label", null, label, ":"), child); } return child; } }]); return Tabs; }(_react["default"].PureComponent); Tabs.defaultProps = { showAll: false, multi: false, itemWidth: 60 }; var _default = Tabs; exports["default"] = _default;