UNPKG

acud

Version:

acg react 组件库

165 lines • 6.67 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; /* * transfer 穿梭框 * author zhousheng */ import * as React from 'react'; import classnames from 'classnames'; import OutlinedRight from "acud-icon/es/icons/OutlinedRight"; import OutlinedDown from "acud-icon/es/icons/OutlinedDown"; import List from '../virtual-list'; import Checkbox from '../checkbox'; import { getBodyHeight, ITEM_HEIGHT } from './util'; var SourceList = function SourceList(props) { var filterSource = props.filterSource, prefixCls = props.prefixCls, render = props.render, targetKeys = props.tKeys, setTargetKeys = props.setTKeys, _props$defaultExpandG = props.defaultExpandGroupKey, defaultExpandGroupKey = _props$defaultExpandG === void 0 ? false : _props$defaultExpandG, showSearch = props.showSearch, _props$leftStyle = props.leftStyle, leftStyle = _props$leftStyle === void 0 ? {} : _props$leftStyle, _props$virtual = props.virtual, virtual = _props$virtual === void 0 ? true : _props$virtual, onSourceExpandChange = props.onSourceExpandChange; var listHeight = React.useMemo(function () { return getBodyHeight(leftStyle, showSearch === true || showSearch === 'left'); }, [leftStyle, showSearch]); var itemToggle = React.useCallback(function (data) { if (data.disabled) { return; } var newTargetKeys = _toConsumableArray(targetKeys); if (data.group) { // const groupKeys = data.childrenKeys; var undisabledKeys = data.children.filter(function (child) { return !child.disabled; }).map(function (child) { return child.key; }); var checkedKeys = undisabledKeys.filter(function (key) { return targetKeys.includes(key); }); if (undisabledKeys.length === checkedKeys.length) { newTargetKeys = newTargetKeys.filter(function (tKey) { return !undisabledKeys.includes(tKey); }); } else { newTargetKeys = _toConsumableArray(new Set([].concat(_toConsumableArray(newTargetKeys), _toConsumableArray(undisabledKeys)))); } } else if (targetKeys.includes(data.key)) { newTargetKeys.splice(targetKeys.indexOf(data.key), 1); } else { newTargetKeys.push(data.key); } setTargetKeys(newTargetKeys); }, [targetKeys]); var groupKeys = filterSource.filter(function (source) { return source.group; }).map(function (source) { return source.key; }); var expandGroupKeys = []; if (defaultExpandGroupKey === true) { // 默认展开所有折叠项 expandGroupKeys = groupKeys; } else if (Array.isArray(defaultExpandGroupKey)) { expandGroupKeys = defaultExpandGroupKey; } else if (typeof defaultExpandGroupKey === 'string' || typeof defaultExpandGroupKey === 'number') { expandGroupKeys = [defaultExpandGroupKey]; } var _React$useState = React.useState(expandGroupKeys), _React$useState2 = _slicedToArray(_React$useState, 2), expandGroupKey = _React$useState2[0], setExpandGroupKey = _React$useState2[1]; var expandGroup = React.useCallback(function (e, groupKey) { var isOpen = false; if (expandGroupKey.includes(groupKey)) { setExpandGroupKey(function (keys) { keys.splice(keys.indexOf(groupKey), 1); return _toConsumableArray(keys); }); isOpen = false; } else { setExpandGroupKey(function (keys) { keys.push(groupKey); return _toConsumableArray(keys); }); isOpen = true; } onSourceExpandChange && onSourceExpandChange(groupKey, isOpen); e.stopPropagation(); }, [expandGroupKey]); var getSourceItem = React.useCallback(function (data) { var checked = targetKeys.includes(data.key); var indeterminate = false; if (data.group) { var undisabledKeys = data.children.filter(function (child) { return !child.disabled; }).map(function (child) { return child.key; }); var checkedKeys = undisabledKeys.filter(function (key) { return targetKeys.includes(key); }); if (undisabledKeys.length === checkedKeys.length) { checked = true; indeterminate = false; } else if (checkedKeys.length) { indeterminate = true; checked = false; } else { checked = false; indeterminate = false; } } var itemCls = classnames(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefixCls, "-datasource-item"), true), "".concat(prefixCls, "-datasource-item-disabeld"), data.disabled), "".concat(prefixCls, "-datasource-item-group"), data.group), "".concat(prefixCls, "-datasource-item-group-option"), data.groupOption)); return /*#__PURE__*/React.createElement("div", { key: data.key, "aria-disabled": data.disabled, className: itemCls, onClick: function onClick() { return itemToggle(data); }, style: { display: data.groupOption && !expandGroupKey.includes(data.groupKey) ? 'none' : '' } }, data.group && /*#__PURE__*/React.createElement("span", { className: "".concat(prefixCls, "-datasource-item-arrow"), onClick: function onClick(e) { return expandGroup(e, data.key); } }, expandGroupKey.includes(data.key) ? /*#__PURE__*/React.createElement(OutlinedDown, null) : /*#__PURE__*/React.createElement(OutlinedRight, null)), /*#__PURE__*/React.createElement(Checkbox, { className: "".concat(prefixCls, "-datasource-item-checkbox"), checked: checked, disabled: data.disabled, indeterminate: indeterminate }), render ? render(data) : /*#__PURE__*/React.createElement("span", { className: "".concat(prefixCls, "-datasource-item-title"), title: data.title }, data.title)); }, [targetKeys, expandGroupKey, render]); var virtualData = React.useMemo(function () { return filterSource.filter(function (data) { return !(data.groupOption && !expandGroupKey.includes(data.groupKey)); }); }, [filterSource, expandGroupKey]); return /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-datasource-list") }, virtual && /*#__PURE__*/React.createElement(List, { itemKey: "key", data: virtualData, height: listHeight, fullHeight: false, itemHeight: ITEM_HEIGHT }, function (data) { return getSourceItem(data); }), !virtual && filterSource.map(function (data) { return getSourceItem(data); })); }; export default SourceList;