choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
303 lines (262 loc) • 10.4 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
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 React, { Component, isValidElement } from 'react';
import { findDOMNode } from 'react-dom';
import classNames from 'classnames';
import noop from 'lodash/noop';
import Checkbox from '../checkbox';
import Search from './search';
import Item from './item';
import triggerEvent from '../_util/triggerEvent';
import Animate from '../animate';
import PureRenderMixin from '../rc-components/util/PureRenderMixin';
function isIEorEDGE() {
return document.documentMode || /Edge/.test(navigator.userAgent);
}
function isRenderResultPlainObject(result) {
return result && ! /*#__PURE__*/isValidElement(result) && Object.prototype.toString.call(result) === '[object Object]';
}
var TransferList = /*#__PURE__*/function (_Component) {
_inherits(TransferList, _Component);
var _super = _createSuper(TransferList);
function TransferList() {
var _this;
_classCallCheck(this, TransferList);
_this = _super.apply(this, arguments);
_this.state = {
mounted: false
};
_this.handleSelect = function (selectedItem) {
var _this$props = _this.props,
checkedKeys = _this$props.checkedKeys,
handleSelect = _this$props.handleSelect;
var result = checkedKeys.some(function (key) {
return key === selectedItem.key;
});
handleSelect(selectedItem, !result);
};
_this.handleFilter = function (e) {
var _this$props2 = _this.props,
handleFilter = _this$props2.handleFilter,
prefixCls = _this$props2.prefixCls;
handleFilter(e);
if (!e.target.value) {
return;
} // Manually trigger scroll event for lazy search bug
_this.triggerScrollTimer = window.setTimeout(function () {
var transferNode = findDOMNode(_assertThisInitialized(_this));
var listNode = transferNode.querySelectorAll(".".concat(prefixCls, "-content"))[0];
if (listNode) {
triggerEvent(listNode, 'scroll');
}
}, 0);
_this.fixIERepaint();
};
_this.handleClear = function () {
var handleClear = _this.props.handleClear;
handleClear();
_this.fixIERepaint();
};
_this.matchFilter = function (text, item) {
var _this$props3 = _this.props,
filter = _this$props3.filter,
filterOption = _this$props3.filterOption;
if (filterOption) {
return filterOption(filter, item);
}
return text.indexOf(filter) >= 0;
};
_this.renderItem = function (item) {
var _this$props$render = _this.props.render,
render = _this$props$render === void 0 ? noop : _this$props$render;
var renderResult = render(item);
var isRenderResultPlain = isRenderResultPlainObject(renderResult);
return {
renderedText: isRenderResultPlain ? renderResult.value : renderResult,
renderedEl: isRenderResultPlain ? renderResult.label : renderResult
};
};
_this.saveNotFoundRef = function (node) {
_this.notFoundNode = node;
};
return _this;
}
_createClass(TransferList, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
this.timer = window.setTimeout(function () {
_this2.setState({
mounted: true
});
}, 0);
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
clearTimeout(this.timer);
clearTimeout(this.triggerScrollTimer);
clearTimeout(this.fixIERepaintTimer);
}
}, {
key: "shouldComponentUpdate",
value: function shouldComponentUpdate() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return PureRenderMixin.shouldComponentUpdate.apply(this, args);
}
}, {
key: "getCheckStatus",
value: function getCheckStatus(filteredDataSource) {
var checkedKeys = this.props.checkedKeys;
if (checkedKeys.length === 0) {
return 'none';
}
if (filteredDataSource.every(function (item) {
return checkedKeys.indexOf(item.key) >= 0;
})) {
return 'all';
}
return 'part';
} // Fix IE/Edge repaint
}, {
key: "fixIERepaint",
value: function fixIERepaint() {
var _this3 = this;
if (!isIEorEDGE()) {
return;
}
this.fixIERepaintTimer = window.setTimeout(function () {
if (_this3.notFoundNode) {
var className = _this3.notFoundNode.className;
_this3.notFoundNode.className = className;
}
}, 0);
}
}, {
key: "render",
value: function render() {
var _this4 = this;
var _this$props4 = this.props,
prefixCls = _this$props4.prefixCls,
checkboxPrefixCls = _this$props4.checkboxPrefixCls,
dataSource = _this$props4.dataSource,
titleText = _this$props4.titleText,
checkedKeys = _this$props4.checkedKeys,
highlightKey = _this$props4.highlightKey,
lazy = _this$props4.lazy,
_this$props4$body = _this$props4.body,
body = _this$props4$body === void 0 ? noop : _this$props4$body,
_this$props4$footer = _this$props4.footer,
footer = _this$props4$footer === void 0 ? noop : _this$props4$footer,
showSearch = _this$props4.showSearch,
style = _this$props4.style,
filter = _this$props4.filter,
searchPlaceholder = _this$props4.searchPlaceholder,
notFoundContent = _this$props4.notFoundContent,
itemUnit = _this$props4.itemUnit,
itemsUnit = _this$props4.itemsUnit,
onScroll = _this$props4.onScroll,
handleSelectAll = _this$props4.handleSelectAll,
inputProps = _this$props4.inputProps;
var mounted = this.state.mounted; // Custom Layout
var footerDom = footer(_objectSpread({}, this.props));
var bodyDom = body(_objectSpread({}, this.props));
var listCls = classNames(prefixCls, _defineProperty({}, "".concat(prefixCls, "-with-footer"), !!footerDom));
var filteredDataSource = [];
var totalDataSource = [];
var showItems = dataSource.map(function (item) {
var _this4$renderItem = _this4.renderItem(item),
renderedText = _this4$renderItem.renderedText,
renderedEl = _this4$renderItem.renderedEl;
if (filter && filter.trim() && !_this4.matchFilter(renderedText, item)) {
return null;
} // all show items
totalDataSource.push(item);
if (!item.disabled) {
// response to checkAll items
filteredDataSource.push(item);
}
var checked = checkedKeys.indexOf(item.key) >= 0;
var isHighlight = highlightKey === item.key;
return /*#__PURE__*/React.createElement(Item, {
key: item.key,
item: item,
lazy: lazy,
renderedText: renderedText,
renderedEl: renderedEl,
checked: checked,
isHighlight: isHighlight,
prefixCls: prefixCls,
onClick: _this4.handleSelect,
checkboxPrefixCls: checkboxPrefixCls
});
});
var unit = dataSource.length > 1 ? itemsUnit : itemUnit;
var search = showSearch ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-body-search-wrapper")
}, /*#__PURE__*/React.createElement(Search, {
prefixCls: "".concat(prefixCls, "-search"),
onChange: this.handleFilter,
handleClear: this.handleClear,
placeholder: searchPlaceholder,
value: filter,
inputProps: inputProps
})) : null;
var listBody = bodyDom || /*#__PURE__*/React.createElement("div", {
className: showSearch ? "".concat(prefixCls, "-body ").concat(prefixCls, "-body-with-search") : "".concat(prefixCls, "-body")
}, search, /*#__PURE__*/React.createElement(Animate, {
component: "ul",
componentProps: {
onScroll: onScroll
},
className: "".concat(prefixCls, "-content"),
transitionName: mounted ? "".concat(prefixCls, "-content-item-highlight") : '',
transitionLeave: false
}, showItems), /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-body-not-found"),
ref: this.saveNotFoundRef
}, notFoundContent));
var listFooter = footerDom ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-footer")
}, footerDom) : null;
var checkStatus = this.getCheckStatus(filteredDataSource);
var checkedAll = checkStatus === 'all';
var checkAllCheckbox = /*#__PURE__*/React.createElement(Checkbox, {
prefixCls: checkboxPrefixCls,
checked: checkedAll,
indeterminate: checkStatus === 'part',
onChange: function onChange() {
return handleSelectAll(filteredDataSource, checkedAll);
}
});
return /*#__PURE__*/React.createElement("div", {
className: listCls,
style: style
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-header")
}, checkAllCheckbox, /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-header-selected")
}, /*#__PURE__*/React.createElement("span", null, (checkedKeys.length > 0 ? "".concat(checkedKeys.length, "/") : '') + totalDataSource.length, ' ', unit), /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-header-title")
}, titleText))), listBody, listFooter);
}
}]);
return TransferList;
}(Component);
export { TransferList as default };
TransferList.displayName = 'TransferList';
TransferList.defaultProps = {
dataSource: [],
titleText: '',
showSearch: false,
render: noop,
lazy: {}
};
//# sourceMappingURL=list.js.map