UNPKG

@txdfe/at

Version:

一个设计体系组件库

333 lines 15.5 kB
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 _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } 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); } import React, { Component } from 'react'; import PropTypes from 'prop-types'; import cx from 'classnames'; import Checkbox from '../../checkbox'; import Search from '../../search'; import Menu from '../../menu'; import { func, htmlId } from '../../util'; import TransferItem from './transfer-item'; var bindCtx = func.bindCtx; var TransferPanel = /*#__PURE__*/function (_Component) { function TransferPanel(props, context) { var _this; _classCallCheck(this, TransferPanel); _this = _callSuper(this, TransferPanel, [props, context]); _this.state = { searchedValue: '', dragValue: null, dragOverValue: null }; _this.footerId = props.baseId ? htmlId.escapeForId("".concat(props.baseId, "-panel-footer-").concat(props.position)) : ''; _this.headerId = props.baseId ? htmlId.escapeForId("".concat(props.baseId, "-panel-header-").concat(props.position)) : ''; bindCtx(_this, ['handleCheck', 'handleAllCheck', 'handleSearch', 'handleItemDragStart', 'handleItemDragOver', 'handleItemDragEnd', 'handleItemDrop', 'getListDOM']); _this.firstRender = true; return _this; } _inherits(TransferPanel, _Component); return _createClass(TransferPanel, [{ key: "componentDidMount", value: function componentDidMount() { this.firstRender = false; } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { if (prevProps.dataSource.length !== this.props.dataSource.length && this.list) { if (this.list.scrollTop > 0) { this.list.scrollTop = 0; } } this.searched = false; } }, { key: "getListDOM", value: function getListDOM(ref) { this.list = ref; } }, { key: "handleAllCheck", value: function handleAllCheck(allChecked) { var _this$props = this.props, position = _this$props.position, onChange = _this$props.onChange; var newValue; if (allChecked) { newValue = this.enabledDatasource.map(function (item) { return item.value; }); } else { newValue = []; } onChange && onChange(position, newValue); } }, { key: "handleCheck", value: function handleCheck(itemValue, checked) { var _this$props2 = this.props, position = _this$props2.position, value = _this$props2.value, onChange = _this$props2.onChange; var newValue = _toConsumableArray(value); var index = value.indexOf(itemValue); if (checked && index === -1) { newValue.push(itemValue); } else if (!checked && index > -1) { newValue.splice(index, 1); } onChange && onChange(position, newValue); } }, { key: "handleSearch", value: function handleSearch(searchedValue) { this.setState({ searchedValue: searchedValue }); this.searched = true; var _this$props3 = this.props, onSearch = _this$props3.onSearch, position = _this$props3.position; onSearch(searchedValue, position); } }, { key: "handleItemDragStart", value: function handleItemDragStart(position, value) { this.setState({ dragPosition: position, dragValue: value }); } }, { key: "handleItemDragOver", value: function handleItemDragOver(value) { this.setState({ dragOverValue: value }); } }, { key: "handleItemDragEnd", value: function handleItemDragEnd() { this.setState({ dragOverValue: null }); } }, { key: "handleItemDrop", value: function handleItemDrop() { var _this$props4; this.setState({ dragOverValue: null }); (_this$props4 = this.props).onSort.apply(_this$props4, arguments); } }, { key: "renderHeader", value: function renderHeader() { var _this$props5 = this.props, title = _this$props5.title, prefix = _this$props5.prefix; return /*#__PURE__*/React.createElement("div", { id: this.headerId, className: "".concat(prefix, "transfer-panel-header") }, title); } }, { key: "renderSearch", value: function renderSearch() { var _this$props6 = this.props, prefix = _this$props6.prefix, searchPlaceholder = _this$props6.searchPlaceholder, locale = _this$props6.locale; return /*#__PURE__*/React.createElement(Search, { "aria-labelledby": this.headerId, shape: "simple", className: "".concat(prefix, "transfer-panel-search"), placeholder: searchPlaceholder || locale.searchPlaceholder, onChange: this.handleSearch }); } }, { key: "renderList", value: function renderList(dataSource) { var _this2 = this; var _this$props7 = this.props, prefix = _this$props7.prefix, position = _this$props7.position, mode = _this$props7.mode, value = _this$props7.value, onMove = _this$props7.onMove, disabled = _this$props7.disabled, listClassName = _this$props7.listClassName, listStyle = _this$props7.listStyle, itemRender = _this$props7.itemRender, sortable = _this$props7.sortable, customerList = _this$props7.customerList; var _this$state = this.state, dragPosition = _this$state.dragPosition, dragValue = _this$state.dragValue, dragOverValue = _this$state.dragOverValue; var newClassName = cx(_defineProperty(_defineProperty({}, "".concat(prefix, "transfer-panel-list"), true), listClassName, !!listClassName)); var customerPanel = customerList && customerList(this.props); if (customerPanel) { return /*#__PURE__*/React.createElement("div", { className: newClassName, style: listStyle, ref: this.getListDOM }, customerPanel); } return dataSource.length ? /*#__PURE__*/React.createElement(Menu, { className: newClassName, style: listStyle, ref: this.getListDOM }, dataSource.map(function (item) { return /*#__PURE__*/React.createElement(TransferItem, { key: item.value, prefix: prefix, mode: mode, checked: value.indexOf(item.value) > -1, disabled: disabled || item.disabled, item: item, onCheck: _this2.handleCheck, onClick: onMove, needHighlight: !_this2.firstRender && !_this2.searched, itemRender: itemRender, draggable: sortable, onDragStart: _this2.handleItemDragStart, onDragOver: _this2.handleItemDragOver, onDragEnd: _this2.handleItemDragEnd, onDrop: _this2.handleItemDrop, dragPosition: dragPosition, dragValue: dragValue, dragOverValue: dragOverValue, panelPosition: position }); })) : /*#__PURE__*/React.createElement("div", { className: newClassName, style: listStyle }, this.renderNotFoundContent()); } }, { key: "renderNotFoundContent", value: function renderNotFoundContent() { var _this$props8 = this.props, prefix = _this$props8.prefix, notFoundContent = _this$props8.notFoundContent; return /*#__PURE__*/React.createElement("div", { className: "".concat(prefix, "transfer-panel-not-found-container") }, /*#__PURE__*/React.createElement("div", { className: "".concat(prefix, "transfer-panel-not-found") }, notFoundContent)); } }, { key: "renderFooter", value: function renderFooter() { var _this$props9 = this.props, prefix = _this$props9.prefix, position = _this$props9.position, mode = _this$props9.mode, disabled = _this$props9.disabled, locale = _this$props9.locale, showCheckAll = _this$props9.showCheckAll; if (mode === 'simple') { var onMoveAll = this.props.onMoveAll; var classNames = cx(_defineProperty(_defineProperty({}, "".concat(prefix, "transfer-panel-move-all"), true), "".concat(prefix, "disabled"), disabled)); return /*#__PURE__*/React.createElement("div", { className: "".concat(prefix, "transfer-panel-footer") }, /*#__PURE__*/React.createElement("a", { className: classNames, onClick: onMoveAll.bind(this, position === 'left' ? 'right' : 'left') }, locale.moveAll)); } var _this$props10 = this.props, value = _this$props10.value, dataSource = _this$props10.dataSource; var checkedCount = value.length; var totalCount = dataSource.length; var totalEnabledCount = this.enabledDatasource.length; var checked = checkedCount > 0 && checkedCount >= totalEnabledCount; var indeterminate = checkedCount > 0 && checkedCount < totalEnabledCount; var items = totalCount > 1 ? locale.items : locale.item; var countLabel = checkedCount === 0 ? "".concat(totalCount, " ").concat(items) : "".concat(checkedCount, "/").concat(totalCount, " ").concat(items); return /*#__PURE__*/React.createElement("div", { className: "".concat(prefix, "transfer-panel-footer") }, showCheckAll && /*#__PURE__*/React.createElement(Checkbox, { disabled: disabled, checked: checked, indeterminate: indeterminate, onChange: this.handleAllCheck, "aria-labelledby": this.footerId }), /*#__PURE__*/React.createElement("span", { className: "".concat(prefix, "transfer-panel-count"), id: this.footerId }, countLabel)); } }, { key: "render", value: function render() { var _this$props11 = this.props, prefix = _this$props11.prefix, title = _this$props11.title, showSearch = _this$props11.showSearch, filter = _this$props11.filter; var searchedValue = this.state.searchedValue; var dataSource = this.props.dataSource; this.enabledDatasource = dataSource.filter(function (item) { return !item.disabled; }); if (showSearch && searchedValue) { dataSource = dataSource.filter(function (item) { return filter(searchedValue, item); }); } return /*#__PURE__*/React.createElement("div", { className: "".concat(prefix, "transfer-panel") }, title ? this.renderHeader() : null, showSearch ? this.renderSearch() : null, this.renderList(dataSource), this.renderFooter()); } }]); }(Component); _defineProperty(TransferPanel, "propTypes", { prefix: PropTypes.string, position: PropTypes.oneOf(['left', 'right']), mode: PropTypes.oneOf(['normal', 'simple']), dataSource: PropTypes.array, value: PropTypes.array, onChange: PropTypes.func, onMove: PropTypes.func, onMoveAll: PropTypes.func, disabled: PropTypes.bool, locale: PropTypes.object, title: PropTypes.node, showSearch: PropTypes.bool, filter: PropTypes.func, onSearch: PropTypes.func, searchPlaceholder: PropTypes.string, notFoundContent: PropTypes.node, listClassName: PropTypes.string, listStyle: PropTypes.object, itemRender: PropTypes.func, sortable: PropTypes.bool, onSort: PropTypes.func, baseId: PropTypes.string, customerList: PropTypes.func, showCheckAll: PropTypes.bool }); export { TransferPanel as default };