UNPKG

tdesign-react

Version:
466 lines (462 loc) 18.3 kB
/** * tdesign v1.15.1 * (c) 2025 tdesign * @license MIT */ import { _ as _defineProperty } from '../_chunks/dep-cb0a3966.js'; import { _ as _toConsumableArray } from '../_chunks/dep-87d110df.js'; import { _ as _slicedToArray } from '../_chunks/dep-48805ab8.js'; import React, { useState, useMemo, useEffect } from 'react'; import { isArray, difference } from 'lodash-es'; import classNames from 'classnames'; import { ChevronLeftIcon, ChevronRightIcon } from 'tdesign-icons-react'; import useConfig from '../hooks/useConfig.js'; import useGlobalIcon from '../hooks/useGlobalIcon.js'; import { Button } from '../button/index.js'; import TransferList from './TransferList.js'; import { getDefaultValue, getJSX, filterCheckedTreeNodes as _filterCheckedTreeNodes, getTargetNodes, getLeafNodes } from './utils.js'; import { useLocaleReceiver } from '../locale/LocalReceiver.js'; import { transferDefaultProps } from './defaultProps.js'; import useDefaultProps from '../hooks/useDefaultProps.js'; import '../_chunks/dep-eca3a3de.js'; import '../_chunks/dep-026a4c6b.js'; import '../config-provider/ConfigContext.js'; import '../_chunks/dep-f97636ce.js'; import '../_chunks/dep-9dbbf468.js'; import 'dayjs'; import '../button/Button.js'; import '../_chunks/dep-6b660ef0.js'; import '../hooks/useDomRefCallback.js'; import '../hooks/useRipple.js'; import '../_chunks/dep-c48e2ca1.js'; import '../hooks/useAnimation.js'; import '../_chunks/dep-3a09424a.js'; import '../loading/index.js'; import '../loading/Loading.js'; import '../common/Portal.js'; import 'react-dom'; import '../hooks/useLayoutEffect.js'; import '../loading/gradient.js'; import '../_chunks/dep-6af6bc60.js'; import '../loading/defaultProps.js'; import '../loading/plugin.js'; import '../_util/react-render.js'; import '../_chunks/dep-52ff3837.js'; import '../common/PluginContainer.js'; import '../config-provider/ConfigProvider.js'; import '../button/defaultProps.js'; import '../_util/parseTNode.js'; import '../_chunks/dep-b908e1fe.js'; import '../checkbox/index.js'; import '../_chunks/dep-872df51f.js'; import '../_util/forwardRefWithStatics.js'; import 'hoist-non-react-statics'; import '../common/Check.js'; import '../_util/helper.js'; import '../hooks/useControlled.js'; import '../_util/noop.js'; import '../checkbox/defaultProps.js'; import '../input/index.js'; import '../input/Input.js'; import '../input/InputGroup.js'; import '../input/defaultProps.js'; import '../input/useLengthLimit.js'; import '../pagination/index.js'; import '../pagination/Pagination.js'; import '../select/index.js'; import '../select/base/Select.js'; import '../_util/composeRefs.js'; import '../common/FakeArrow.js'; import '../select-input/index.js'; import '../select-input/SelectInput.js'; import '../popup/index.js'; import '../popup/Popup.js'; import 'react-transition-group'; import '../_util/ref.js'; import 'react-is'; import '../_util/isFragment.js'; import '../hooks/useAttach.js'; import '../hooks/useMutationObserver.js'; import '../hooks/useLatest.js'; import '../hooks/usePopper.js'; import '@popperjs/core'; import 'react-fast-compare'; import '../hooks/useWindowSize.js'; import '../popup/defaultProps.js'; import '../popup/hooks/useTrigger.js'; import '../_util/listener.js'; import '../popup/utils/transition.js'; import '../popup/PopupPlugin.js'; import '../select-input/useSingle.js'; import '../select-input/useMultiple.js'; import '../tag-input/index.js'; import '../tag-input/TagInput.js'; import '../hooks/useDragSorter.js'; import '../hooks/useEventCallback.js'; import '../tag-input/defaultProps.js'; import '../tag-input/useHover.js'; import '../tag-input/useTagList.js'; import '../tag/index.js'; import '../tag/Tag.js'; import '../_chunks/dep-3c3d29db.js'; import '../tag/defaultProps.js'; import '../tag/CheckTag.js'; import '../_chunks/dep-dbcf7b24.js'; import '../tag/CheckTagGroup.js'; import '../tag-input/useTagScroll.js'; import '../select-input/useOverlayInnerStyle.js'; import '../select-input/defaultProps.js'; import '../select/defaultProps.js'; import '../_chunks/dep-72472aa7.js'; import '../select/base/OptionGroup.js'; import '../select/base/Option.js'; import '../select/base/PopupContent.js'; import '../select/hooks/usePanelVirtualScroll.js'; import '../hooks/useVirtualScroll.js'; import '../input-number/index.js'; import '../input-number/InputNumber.js'; import '../input-number/useInputNumber.js'; import '../_chunks/dep-76b39920.js'; import '../hooks/useCommonClassName.js'; import '../input-number/defaultProps.js'; import '../input-adornment/InputAdornment.js'; import '../pagination/hooks/useBoundaryJumper.js'; import '../pagination/hooks/usePrevNextJumper.js'; import '../pagination/hooks/usePageNumber.js'; import '../pagination/hooks/useTotal.js'; import '../pagination/validators.js'; import '../pagination/defaultProps.js'; import '../pagination/PaginationMini.js'; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } var Transfer = function Transfer(originalProps) { var props = useDefaultProps(originalProps, transferDefaultProps); var data = props.data, search = props.search, checked = props.checked, defaultChecked = props.defaultChecked, onCheckedChange = props.onCheckedChange, value = props.value, defaultValue = props.defaultValue, onChange = props.onChange, empty = props.empty, disabled = props.disabled, pagination = props.pagination, title = props.title, operation = props.operation, footer = props.footer, transferItem = props.transferItem, content = props.content, tree = props.tree, showCheckAll = props.showCheckAll, _props$direction = props.direction, direction = _props$direction === void 0 ? "both" : _props$direction; var _useState = useState(function () { return { source: data.filter(function (item) { return !defaultValue.includes(item.value); }), target: data.filter(function (item) { return defaultValue.includes(item.value); }) }; }), _useState2 = _slicedToArray(_useState, 2), state = _useState2[0], setState = _useState2[1]; var _useState3 = useState(function () { return { source: state.source.filter(function (item) { return !item.disabled && defaultChecked.includes(item.value); }).map(function (item) { return item.value; }), target: state.target.filter(function (item) { return !item.disabled && defaultChecked.includes(item.value); }).map(function (item) { return item.value; }) }; }), _useState4 = _slicedToArray(_useState3, 2), checkeds = _useState4[0], setCheckeds = _useState4[1]; var _useState5 = useState({ source: "", target: "" }), _useState6 = _slicedToArray(_useState5, 2), searchState = _useState6[0], setSearchState = _useState6[1]; var isTargetControlled = "value" in props; var isCheckedControlled = "checked" in props; var _useConfig = useConfig(), classPrefix = _useConfig.classPrefix; var _useGlobalIcon = useGlobalIcon({ ChevronLeftIcon: ChevronLeftIcon, ChevronRightIcon: ChevronRightIcon }), ChevronRightIcon$1 = _useGlobalIcon.ChevronRightIcon, ChevronLeftIcon$1 = _useGlobalIcon.ChevronLeftIcon; var transferClassName = "".concat(classPrefix, "-transfer"); var _useLocaleReceiver = useLocaleReceiver("transfer"), _useLocaleReceiver2 = _slicedToArray(_useLocaleReceiver, 2), local = _useLocaleReceiver2[0], t = _useLocaleReceiver2[1]; var _useMemo = useMemo(function () { var source = state.source, target = state.target; var searchSource = searchState.source, searchTarget = searchState.target; return [source.filter(function (item) { return !searchSource || item.label.indexOf(searchState.source) > -1; }), target.filter(function (item) { return !searchTarget || item.label.indexOf(searchState.target) > -1; })]; }, [searchState, state]), _useMemo2 = _slicedToArray(_useMemo, 2), sourceData = _useMemo2[0], targetData = _useMemo2[1]; var _getDefaultValue$map = getDefaultValue(empty, t(local.empty)).map(function (item) { return getJSX(item); }), _getDefaultValue$map2 = _slicedToArray(_getDefaultValue$map, 2), SourceEmptyCmp = _getDefaultValue$map2[0], TargetEmptyCmp = _getDefaultValue$map2[1]; var sourceDisabled = isArray(disabled) ? disabled[0] : disabled; var targetDisabled = isArray(disabled) ? disabled[1] : disabled; var _getDefaultValue = getDefaultValue(pagination), _getDefaultValue2 = _slicedToArray(_getDefaultValue, 2), sourcePagination = _getDefaultValue2[0], targetPagination = _getDefaultValue2[1]; var _getDefaultValue$map3 = getDefaultValue(title).map(function (item) { return getJSX(item); }), _getDefaultValue$map4 = _slicedToArray(_getDefaultValue$map3, 2), sourceTitle = _getDefaultValue$map4[0], targetTitle = _getDefaultValue$map4[1]; var _getDefaultValue$map5 = getDefaultValue(operation, [function () { return /* @__PURE__ */React.createElement(ChevronRightIcon$1, null); }, function () { return /* @__PURE__ */React.createElement(ChevronLeftIcon$1, null); }]).map(function (item) { return getJSX(item); }), _getDefaultValue$map6 = _slicedToArray(_getDefaultValue$map5, 2), leftOperation = _getDefaultValue$map6[0], rightOperation = _getDefaultValue$map6[1]; var _getDefaultValue$map7 = getDefaultValue(footer).map(function (item) { return getJSX(item); }), _getDefaultValue$map8 = _slicedToArray(_getDefaultValue$map7, 2), sourceFooter = _getDefaultValue$map8[0], targetFooter = _getDefaultValue$map8[1]; var _getDefaultValue3 = getDefaultValue(content), _getDefaultValue4 = _slicedToArray(_getDefaultValue3, 2), sourceContent = _getDefaultValue4[0], targetContent = _getDefaultValue4[1]; var _useMemo3 = useMemo(function () { var _showCheckAll$, _showCheckAll$2; return Array.isArray(showCheckAll) ? [(_showCheckAll$ = showCheckAll[0]) !== null && _showCheckAll$ !== void 0 ? _showCheckAll$ : true, (_showCheckAll$2 = showCheckAll[1]) !== null && _showCheckAll$2 !== void 0 ? _showCheckAll$2 : true] : [showCheckAll, showCheckAll]; }, [showCheckAll]), _useMemo4 = _slicedToArray(_useMemo3, 2), showCheckAllSource = _useMemo4[0], showCheckAllTarget = _useMemo4[1]; var transformSource = function transformSource() { var source = state.source, target = state.target; var sourceCheckeds = source.filter(function (item) { return checkeds.source.includes(item.value); }); var newSource = difference(source, sourceCheckeds); var newTarget = [].concat(_toConsumableArray(target), _toConsumableArray(sourceCheckeds)); var newTargetValue = newTarget.map(function (t2) { return t2.value; }); if (tree) { newSource = _filterCheckedTreeNodes(source, checkeds.source); newTarget = getTargetNodes(newSource, data); newTargetValue = getLeafNodes(newTarget).map(function (t2) { return t2.value; }); } if (isTargetControlled) { onChange === null || onChange === void 0 || onChange(newTargetValue, { type: "source", movedValue: sourceCheckeds.map(function (s) { return s.value; }) }); } else { setState(_objectSpread(_objectSpread({}, state), {}, { source: newSource, target: _toConsumableArray(newTarget) })); setCheckeds(_objectSpread(_objectSpread({}, checkeds), {}, { source: [] })); } }; var transformTarget = function transformTarget() { var source = state.source, target = state.target; var targetCheckeds = target.filter(function (item) { return checkeds.target.includes(item.value); }); var newSource = [].concat(_toConsumableArray(source), _toConsumableArray(targetCheckeds)); var newTarget = difference(target, targetCheckeds); var newTargetValue = newTarget.map(function (t2) { return t2.value; }); if (tree) { newTarget = _filterCheckedTreeNodes(target, checkeds.target); newSource = getTargetNodes(newTarget, data); newTargetValue = getLeafNodes(newTarget).map(function (t2) { return t2.value; }); } if (isTargetControlled) { onChange === null || onChange === void 0 || onChange(newTargetValue, { type: "target", movedValue: targetCheckeds.map(function (s) { return s.value; }) }); } else { setState(_objectSpread(_objectSpread({}, state), {}, { source: newSource, target: newTarget })); setCheckeds(_objectSpread(_objectSpread({}, checkeds), {}, { target: [] })); } }; var OperationsCmp = function OperationsCmp() { var isSourceDisabled = sourceDisabled || !checkeds.source.length; var isTargetDisabled = targetDisabled || !checkeds.target.length; var isToRightShow = direction !== "left"; var isToLeftShow = direction !== "right"; return /* @__PURE__ */React.createElement("div", { className: "".concat(transferClassName, "__operations") }, isToRightShow && /* @__PURE__ */React.createElement(Button, { size: "small", key: isSourceDisabled ? "right-outline" : "right-base", variant: "outline", disabled: isSourceDisabled, onClick: transformSource }, leftOperation), isToLeftShow && /* @__PURE__ */React.createElement(Button, { size: "small", key: isSourceDisabled ? "left-outline" : "left-base", variant: "outline", disabled: isTargetDisabled, onClick: transformTarget }, rightOperation)); }; var handleCheckChange = function handleCheckChange(value2, type) { var sourceChecked = checkeds.source, targetChecked = checkeds.target; var inverseMap = { source: "target", target: "source" }; isCheckedControlled ? onCheckedChange === null || onCheckedChange === void 0 ? void 0 : onCheckedChange({ type: type, checked: value2.concat(checkeds[inverseMap[type]]), sourceChecked: sourceChecked, targetChecked: targetChecked }) : setCheckeds(_objectSpread(_objectSpread({}, checkeds), {}, _defineProperty({}, type, value2))); }; useEffect(function () { if (isTargetControlled && Array.isArray(value)) { var newTarget = data.filter(function (item) { return value.includes(item.value); }); var newSource = difference(data, newTarget); if (tree) { newSource = _filterCheckedTreeNodes(data, value); newTarget = getTargetNodes(newSource, data); } setState({ source: _toConsumableArray(newSource), target: _toConsumableArray(newTarget) }); setCheckeds({ source: [], target: [] }); } }, [value, data, isTargetControlled, tree]); useEffect(function () { var source = state.source, target = state.target; if (isCheckedControlled && Array.isArray(checked)) { var newSourceChecked = source.filter(function (item) { return !item.disabled && checked.includes(item.value); }).map(function (item) { return item.value; }); var newTargetChecked = target.filter(function (item) { return !item.disabled && checked.includes(item.value); }).map(function (item) { return item.value; }); setCheckeds({ source: newSourceChecked, target: newTargetChecked }); } }, [checked, state, isCheckedControlled]); return /* @__PURE__ */React.createElement("div", { className: classNames(transferClassName, _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(transferClassName, "__search"), search), "".concat(transferClassName, "__pagination"), pagination), "".concat(transferClassName, "__footer"), footer), "".concat(transferClassName, "--with-tree"), tree)) }, /* @__PURE__ */React.createElement(TransferList, { className: "".concat(transferClassName, "__list-source"), listType: "source", data: sourceData, search: search, checked: checkeds.source, empty: SourceEmptyCmp, disabled: sourceDisabled, pagination: sourcePagination, title: sourceTitle, footer: sourceFooter, transferItem: transferItem, content: sourceContent, onCheckbox: function onCheckbox(value2) { return handleCheckChange(value2, "source"); }, onSearch: function onSearch(val) { return setSearchState(_objectSpread(_objectSpread({}, searchState), {}, { source: val })); }, tree: tree, showCheckAll: showCheckAllSource }), OperationsCmp(), /* @__PURE__ */React.createElement(TransferList, { className: "".concat(transferClassName, "__list-target"), listType: "target", data: targetData, search: search, checked: checkeds.target, empty: TargetEmptyCmp, disabled: targetDisabled, pagination: targetPagination, title: targetTitle, footer: targetFooter, transferItem: transferItem, content: targetContent, onCheckbox: function onCheckbox(value2) { return handleCheckChange(value2, "target"); }, onSearch: function onSearch(val) { return setSearchState(_objectSpread(_objectSpread({}, searchState), {}, { target: val })); }, tree: tree, showCheckAll: showCheckAllTarget })); }; Transfer.displayName = "Transfer"; export { Transfer as default }; //# sourceMappingURL=Transfer.js.map