2023-01-04 15:09:03 +08:00
|
|
|
import type { ChangeEvent, CSSProperties } from 'react';
|
2023-06-07 22:54:56 +08:00
|
|
|
import React, { useCallback, useContext } from 'react';
|
2023-09-11 17:28:04 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2023-10-26 11:46:22 +08:00
|
|
|
import type { PrevSelectedIndex } from '../_util/hooks/useMultipleSelect';
|
|
|
|
import useMultipleSelect from '../_util/hooks/useMultipleSelect';
|
2023-03-19 16:13:04 +08:00
|
|
|
import type { InputStatus } from '../_util/statusUtils';
|
|
|
|
import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
|
|
|
|
import { groupDisabledKeysMap, groupKeysMap } from '../_util/transKeys';
|
2023-09-11 17:28:04 +08:00
|
|
|
import { devUseWarning } from '../_util/warning';
|
2023-01-09 10:04:35 +08:00
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
2023-01-04 15:09:03 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2023-01-09 10:04:35 +08:00
|
|
|
import DefaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
2023-01-04 15:09:03 +08:00
|
|
|
import type { FormItemStatusContextProps } from '../form/context';
|
2022-03-24 21:54:20 +08:00
|
|
|
import { FormItemInputContext } from '../form/context';
|
2023-03-21 13:08:43 +08:00
|
|
|
import { useLocale } from '../locale';
|
2023-04-27 11:48:57 +08:00
|
|
|
import defaultLocale from '../locale/en_US';
|
2023-06-07 22:54:56 +08:00
|
|
|
import useData from './hooks/useData';
|
|
|
|
import useSelection from './hooks/useSelection';
|
2024-03-14 21:14:21 +08:00
|
|
|
import type { PaginationType, TransferKey } from './interface';
|
2023-10-26 11:46:22 +08:00
|
|
|
import type { TransferCustomListBodyProps, TransferListProps } from './list';
|
2022-06-22 14:57:09 +08:00
|
|
|
import List from './list';
|
|
|
|
import Operation from './operation';
|
|
|
|
import Search from './search';
|
2022-05-16 12:21:14 +08:00
|
|
|
import useStyle from './style';
|
2016-07-01 20:52:17 +08:00
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
export type { TransferListProps } from './list';
|
|
|
|
export type { TransferOperationProps } from './operation';
|
|
|
|
export type { TransferSearchProps } from './search';
|
2017-09-25 22:14:49 +08:00
|
|
|
|
2017-11-20 17:41:32 +08:00
|
|
|
export type TransferDirection = 'left' | 'right';
|
2019-05-07 17:10:42 +08:00
|
|
|
|
|
|
|
export interface RenderResultObject {
|
|
|
|
label: React.ReactElement;
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type RenderResult = React.ReactElement | RenderResultObject | string | null;
|
|
|
|
|
2016-07-01 20:52:17 +08:00
|
|
|
export interface TransferItem {
|
2024-03-14 21:14:21 +08:00
|
|
|
key?: TransferKey;
|
2019-12-09 18:57:33 +08:00
|
|
|
title?: string;
|
2016-07-13 11:14:24 +08:00
|
|
|
description?: string;
|
2016-09-19 17:52:20 +08:00
|
|
|
disabled?: boolean;
|
2019-05-07 17:10:42 +08:00
|
|
|
[name: string]: any;
|
2016-07-01 20:52:17 +08:00
|
|
|
}
|
|
|
|
|
2024-03-14 21:14:21 +08:00
|
|
|
export type KeyWise<T> = T & { key: TransferKey };
|
2020-11-08 15:28:03 +08:00
|
|
|
|
|
|
|
export type KeyWiseTransferItem = KeyWise<TransferItem>;
|
|
|
|
|
|
|
|
type TransferRender<RecordType> = (item: RecordType) => RenderResult;
|
2020-09-16 11:43:55 +08:00
|
|
|
|
2019-10-22 11:05:39 +08:00
|
|
|
export interface ListStyle {
|
|
|
|
direction: TransferDirection;
|
|
|
|
}
|
|
|
|
|
2020-02-04 10:16:23 +08:00
|
|
|
export type SelectAllLabel =
|
|
|
|
| React.ReactNode
|
|
|
|
| ((info: { selectedCount: number; totalCount: number }) => React.ReactNode);
|
|
|
|
|
2020-09-16 11:43:55 +08:00
|
|
|
export interface TransferLocale {
|
2022-10-01 22:17:14 +08:00
|
|
|
titles?: React.ReactNode[];
|
2021-06-23 17:38:16 +08:00
|
|
|
notFoundContent?: React.ReactNode | React.ReactNode[];
|
2020-09-16 11:43:55 +08:00
|
|
|
searchPlaceholder: string;
|
|
|
|
itemUnit: string;
|
|
|
|
itemsUnit: string;
|
2022-10-01 22:17:14 +08:00
|
|
|
remove?: string;
|
|
|
|
selectAll?: string;
|
2024-04-22 12:03:12 +08:00
|
|
|
deselectAll?: string;
|
2022-10-01 22:17:14 +08:00
|
|
|
selectCurrent?: string;
|
|
|
|
selectInvert?: string;
|
|
|
|
removeAll?: string;
|
|
|
|
removeCurrent?: string;
|
2020-09-16 11:43:55 +08:00
|
|
|
}
|
2021-02-28 11:00:44 +08:00
|
|
|
|
2024-01-11 15:55:58 +08:00
|
|
|
export interface TransferProps<RecordType = any> {
|
2016-12-07 18:08:52 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2018-09-15 15:18:59 +08:00
|
|
|
disabled?: boolean;
|
2022-10-22 18:43:36 +08:00
|
|
|
dataSource?: RecordType[];
|
2024-03-14 21:14:21 +08:00
|
|
|
targetKeys?: TransferKey[];
|
|
|
|
selectedKeys?: TransferKey[];
|
2020-11-08 15:28:03 +08:00
|
|
|
render?: TransferRender<RecordType>;
|
2024-03-14 21:14:21 +08:00
|
|
|
onChange?: (
|
|
|
|
targetKeys: TransferKey[],
|
|
|
|
direction: TransferDirection,
|
|
|
|
moveKeys: TransferKey[],
|
|
|
|
) => void;
|
|
|
|
onSelectChange?: (sourceSelectedKeys: TransferKey[], targetSelectedKeys: TransferKey[]) => void;
|
2016-12-07 18:08:52 +08:00
|
|
|
style?: React.CSSProperties;
|
2023-01-04 15:09:03 +08:00
|
|
|
listStyle?: ((style: ListStyle) => CSSProperties) | CSSProperties;
|
|
|
|
operationStyle?: CSSProperties;
|
2020-12-11 23:23:49 +08:00
|
|
|
titles?: React.ReactNode[];
|
2016-09-19 17:52:20 +08:00
|
|
|
operations?: string[];
|
2016-07-13 11:14:24 +08:00
|
|
|
showSearch?: boolean;
|
2023-08-29 22:53:43 +08:00
|
|
|
filterOption?: (inputValue: string, item: RecordType, direction: TransferDirection) => boolean;
|
2019-07-09 11:46:21 +08:00
|
|
|
locale?: Partial<TransferLocale>;
|
2022-03-24 21:54:20 +08:00
|
|
|
footer?: (
|
|
|
|
props: TransferListProps<RecordType>,
|
2023-01-04 15:09:03 +08:00
|
|
|
info?: { direction: TransferDirection },
|
2022-03-24 21:54:20 +08:00
|
|
|
) => React.ReactNode;
|
2024-03-14 21:14:21 +08:00
|
|
|
rowKey?: (record: RecordType) => TransferKey;
|
2018-10-12 23:47:42 +08:00
|
|
|
onSearch?: (direction: TransferDirection, value: string) => void;
|
2019-12-23 18:33:08 +08:00
|
|
|
onScroll?: (direction: TransferDirection, e: React.SyntheticEvent<HTMLUListElement>) => void;
|
2023-10-26 11:46:22 +08:00
|
|
|
children?: (props: TransferCustomListBodyProps<RecordType>) => React.ReactNode;
|
2019-05-07 17:10:42 +08:00
|
|
|
showSelectAll?: boolean;
|
2020-02-04 10:16:23 +08:00
|
|
|
selectAllLabels?: SelectAllLabel[];
|
2020-05-13 19:15:40 +08:00
|
|
|
oneWay?: boolean;
|
|
|
|
pagination?: PaginationType;
|
2022-02-17 16:43:33 +08:00
|
|
|
status?: InputStatus;
|
2023-07-25 14:05:52 +08:00
|
|
|
selectionsIcon?: React.ReactNode;
|
2017-11-20 17:41:32 +08:00
|
|
|
}
|
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const Transfer = <RecordType extends TransferItem = TransferItem>(
|
|
|
|
props: TransferProps<RecordType>,
|
|
|
|
) => {
|
|
|
|
const {
|
2023-06-07 22:54:56 +08:00
|
|
|
dataSource,
|
2023-01-04 15:09:03 +08:00
|
|
|
targetKeys = [],
|
2023-06-07 22:54:56 +08:00
|
|
|
selectedKeys,
|
2023-01-04 15:09:03 +08:00
|
|
|
selectAllLabels = [],
|
|
|
|
operations = [],
|
|
|
|
style = {},
|
|
|
|
listStyle = {},
|
|
|
|
locale = {},
|
|
|
|
titles,
|
|
|
|
disabled,
|
|
|
|
showSearch = false,
|
|
|
|
operationStyle,
|
|
|
|
showSelectAll,
|
|
|
|
oneWay,
|
2020-05-13 19:15:40 +08:00
|
|
|
pagination,
|
2023-01-04 15:09:03 +08:00
|
|
|
status: customStatus,
|
|
|
|
prefixCls: customizePrefixCls,
|
2023-01-20 11:03:50 +08:00
|
|
|
className,
|
|
|
|
rootClassName,
|
2023-07-25 14:05:52 +08:00
|
|
|
selectionsIcon,
|
2023-01-04 15:09:03 +08:00
|
|
|
filterOption,
|
|
|
|
render,
|
|
|
|
footer,
|
2020-05-13 19:15:40 +08:00
|
|
|
children,
|
2023-01-04 15:09:03 +08:00
|
|
|
rowKey,
|
|
|
|
onScroll,
|
|
|
|
onChange,
|
|
|
|
onSearch,
|
|
|
|
onSelectChange,
|
|
|
|
} = props;
|
|
|
|
|
2023-01-20 11:03:50 +08:00
|
|
|
const {
|
|
|
|
getPrefixCls,
|
|
|
|
renderEmpty,
|
|
|
|
direction: dir,
|
2023-07-06 09:36:45 +08:00
|
|
|
transfer,
|
2023-01-20 11:03:50 +08:00
|
|
|
} = useContext<ConfigConsumerProps>(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('transfer', customizePrefixCls);
|
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
|
2023-01-20 11:03:50 +08:00
|
|
|
|
2023-06-07 22:54:56 +08:00
|
|
|
// Fill record with `key`
|
|
|
|
const [mergedDataSource, leftDataSource, rightDataSource] = useData(
|
|
|
|
dataSource,
|
|
|
|
rowKey,
|
|
|
|
targetKeys,
|
2023-01-04 15:09:03 +08:00
|
|
|
);
|
|
|
|
|
2023-06-07 22:54:56 +08:00
|
|
|
// Get direction selected keys
|
|
|
|
const [
|
|
|
|
// Keys
|
|
|
|
sourceSelectedKeys,
|
|
|
|
targetSelectedKeys,
|
|
|
|
// Setters
|
|
|
|
setSourceSelectedKeys,
|
|
|
|
setTargetSelectedKeys,
|
2024-04-01 15:49:45 +08:00
|
|
|
] = useSelection(leftDataSource as any, rightDataSource as any, selectedKeys);
|
2020-05-13 19:15:40 +08:00
|
|
|
|
2023-10-26 11:46:22 +08:00
|
|
|
const [leftMultipleSelect, updateLeftPrevSelectedIndex] = useMultipleSelect<
|
|
|
|
KeyWise<RecordType>,
|
2024-03-14 21:14:21 +08:00
|
|
|
TransferKey
|
2023-10-26 11:46:22 +08:00
|
|
|
>((item) => item.key);
|
|
|
|
const [rightMultipleSelect, updateRightPrevSelectedIndex] = useMultipleSelect<
|
|
|
|
KeyWise<RecordType>,
|
2024-03-14 21:14:21 +08:00
|
|
|
TransferKey
|
2023-10-26 11:46:22 +08:00
|
|
|
>((item) => item.key);
|
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Transfer');
|
|
|
|
|
|
|
|
warning(!pagination || !children, 'usage', '`pagination` not support customize render list.');
|
2019-03-07 15:18:57 +08:00
|
|
|
}
|
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const setStateKeys = useCallback(
|
2024-03-14 21:14:21 +08:00
|
|
|
(
|
|
|
|
direction: TransferDirection,
|
|
|
|
keys: TransferKey[] | ((prevKeys: TransferKey[]) => TransferKey[]),
|
|
|
|
) => {
|
2023-01-04 15:09:03 +08:00
|
|
|
if (direction === 'left') {
|
2023-04-27 11:48:57 +08:00
|
|
|
const nextKeys = typeof keys === 'function' ? keys(sourceSelectedKeys || []) : keys;
|
|
|
|
setSourceSelectedKeys(nextKeys);
|
2023-01-04 15:09:03 +08:00
|
|
|
} else {
|
2023-04-27 11:48:57 +08:00
|
|
|
const nextKeys = typeof keys === 'function' ? keys(targetSelectedKeys || []) : keys;
|
|
|
|
setTargetSelectedKeys(nextKeys);
|
2023-01-04 15:09:03 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[sourceSelectedKeys, targetSelectedKeys],
|
|
|
|
);
|
|
|
|
|
2023-10-26 11:46:22 +08:00
|
|
|
const setPrevSelectedIndex = (direction: TransferDirection, value: PrevSelectedIndex) => {
|
|
|
|
const isLeftDirection = direction === 'left';
|
|
|
|
const updatePrevSelectedIndex = isLeftDirection
|
|
|
|
? updateLeftPrevSelectedIndex
|
|
|
|
: updateRightPrevSelectedIndex;
|
|
|
|
updatePrevSelectedIndex(value);
|
|
|
|
};
|
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const handleSelectChange = useCallback(
|
2024-03-14 21:14:21 +08:00
|
|
|
(direction: TransferDirection, holder: TransferKey[]) => {
|
2023-01-04 15:09:03 +08:00
|
|
|
if (direction === 'left') {
|
|
|
|
onSelectChange?.(holder, targetSelectedKeys);
|
|
|
|
} else {
|
|
|
|
onSelectChange?.(sourceSelectedKeys, holder);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[sourceSelectedKeys, targetSelectedKeys],
|
|
|
|
);
|
2016-08-08 14:42:29 +08:00
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const getTitles = (transferLocale: TransferLocale): React.ReactNode[] =>
|
|
|
|
titles ?? transferLocale.titles ?? [];
|
2016-11-04 17:46:01 +08:00
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const handleLeftScroll = (e: React.SyntheticEvent<HTMLUListElement>) => {
|
|
|
|
onScroll?.('left', e);
|
2020-05-13 19:15:40 +08:00
|
|
|
};
|
2016-09-19 17:52:20 +08:00
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const handleRightScroll = (e: React.SyntheticEvent<HTMLUListElement>) => {
|
|
|
|
onScroll?.('right', e);
|
2022-10-22 18:43:36 +08:00
|
|
|
};
|
2015-12-16 23:02:49 +08:00
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const moveTo = (direction: TransferDirection) => {
|
2016-11-04 17:06:57 +08:00
|
|
|
const moveKeys = direction === 'right' ? sourceSelectedKeys : targetSelectedKeys;
|
2023-06-07 22:54:56 +08:00
|
|
|
const dataSourceDisabledKeysMap = groupDisabledKeysMap(mergedDataSource);
|
2017-02-24 19:08:02 +08:00
|
|
|
// filter the disabled options
|
2022-12-25 15:21:21 +08:00
|
|
|
const newMoveKeys = moveKeys.filter((key) => !dataSourceDisabledKeysMap.has(key));
|
|
|
|
const newMoveKeysMap = groupKeysMap(newMoveKeys);
|
2015-12-21 15:29:02 +08:00
|
|
|
// move items to target box
|
2018-12-07 20:02:01 +08:00
|
|
|
const newTargetKeys =
|
|
|
|
direction === 'right'
|
|
|
|
? newMoveKeys.concat(targetKeys)
|
2022-12-25 15:21:21 +08:00
|
|
|
: targetKeys.filter((targetKey) => !newMoveKeysMap.has(targetKey));
|
2015-12-21 15:29:02 +08:00
|
|
|
|
|
|
|
// empty checked keys
|
2016-09-18 10:44:22 +08:00
|
|
|
const oppositeDirection = direction === 'right' ? 'left' : 'right';
|
2023-01-04 15:09:03 +08:00
|
|
|
setStateKeys(oppositeDirection, []);
|
|
|
|
handleSelectChange(oppositeDirection, []);
|
2021-02-19 18:26:53 +08:00
|
|
|
onChange?.(newTargetKeys, direction, newMoveKeys);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2015-11-25 23:17:06 +08:00
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const moveToLeft = () => {
|
|
|
|
moveTo('left');
|
2023-10-26 11:46:22 +08:00
|
|
|
setPrevSelectedIndex('left', null);
|
2023-01-04 15:09:03 +08:00
|
|
|
};
|
2016-10-24 16:30:38 +08:00
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const moveToRight = () => {
|
|
|
|
moveTo('right');
|
2023-10-26 11:46:22 +08:00
|
|
|
setPrevSelectedIndex('right', null);
|
2023-01-04 15:09:03 +08:00
|
|
|
};
|
2016-09-18 10:44:22 +08:00
|
|
|
|
2023-04-27 11:48:57 +08:00
|
|
|
const onItemSelectAll = (
|
|
|
|
direction: TransferDirection,
|
|
|
|
keys: string[],
|
|
|
|
checkAll: boolean | 'replace',
|
|
|
|
) => {
|
2023-01-04 15:09:03 +08:00
|
|
|
setStateKeys(direction, (prevKeys) => {
|
2024-03-14 21:14:21 +08:00
|
|
|
let mergedCheckedKeys: TransferKey[] = [];
|
2023-04-27 11:48:57 +08:00
|
|
|
if (checkAll === 'replace') {
|
|
|
|
mergedCheckedKeys = keys;
|
|
|
|
} else if (checkAll) {
|
2020-05-13 19:15:40 +08:00
|
|
|
// Merge current keys with origin key
|
2024-03-14 21:14:21 +08:00
|
|
|
mergedCheckedKeys = Array.from(new Set<TransferKey>([...prevKeys, ...keys]));
|
2020-05-13 19:15:40 +08:00
|
|
|
} else {
|
2023-01-04 15:09:03 +08:00
|
|
|
const selectedKeysMap = groupKeysMap(keys);
|
2020-05-13 19:15:40 +08:00
|
|
|
// Remove current keys from origin keys
|
2022-12-25 15:21:21 +08:00
|
|
|
mergedCheckedKeys = prevKeys.filter((key) => !selectedKeysMap.has(key));
|
2020-05-13 19:15:40 +08:00
|
|
|
}
|
2023-01-04 15:09:03 +08:00
|
|
|
handleSelectChange(direction, mergedCheckedKeys);
|
2020-05-13 19:15:40 +08:00
|
|
|
return mergedCheckedKeys;
|
|
|
|
});
|
2023-10-26 11:46:22 +08:00
|
|
|
setPrevSelectedIndex(direction, null);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2015-11-25 23:17:06 +08:00
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const onLeftItemSelectAll = (keys: string[], checkAll: boolean) => {
|
|
|
|
onItemSelectAll('left', keys, checkAll);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2015-11-26 16:07:11 +08:00
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const onRightItemSelectAll = (keys: string[], checkAll: boolean) => {
|
|
|
|
onItemSelectAll('right', keys, checkAll);
|
|
|
|
};
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const leftFilter = (e: ChangeEvent<HTMLInputElement>) => onSearch?.('left', e.target.value);
|
2016-03-30 17:06:19 +08:00
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const rightFilter = (e: ChangeEvent<HTMLInputElement>) => onSearch?.('right', e.target.value);
|
2015-11-25 23:17:06 +08:00
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const handleLeftClear = () => onSearch?.('left', '');
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const handleRightClear = () => onSearch?.('right', '');
|
2016-03-30 17:06:19 +08:00
|
|
|
|
2023-10-26 11:46:22 +08:00
|
|
|
const handleSingleSelect = (
|
|
|
|
direction: TransferDirection,
|
2024-03-14 21:14:21 +08:00
|
|
|
holder: Set<TransferKey>,
|
|
|
|
selectedKey: TransferKey,
|
2023-10-26 11:46:22 +08:00
|
|
|
checked: boolean,
|
|
|
|
currentSelectedIndex: number,
|
|
|
|
) => {
|
|
|
|
const isSelected = holder.has(selectedKey);
|
|
|
|
if (isSelected) {
|
|
|
|
holder.delete(selectedKey);
|
|
|
|
setPrevSelectedIndex(direction, null);
|
2015-12-21 15:29:02 +08:00
|
|
|
}
|
2016-01-07 17:46:46 +08:00
|
|
|
if (checked) {
|
2023-10-26 11:46:22 +08:00
|
|
|
holder.add(selectedKey);
|
|
|
|
setPrevSelectedIndex(direction, currentSelectedIndex);
|
2015-12-21 15:29:02 +08:00
|
|
|
}
|
2023-10-26 11:46:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleMultipleSelect = (
|
|
|
|
direction: TransferDirection,
|
|
|
|
data: KeyWise<RecordType>[],
|
2024-03-14 21:14:21 +08:00
|
|
|
holder: Set<TransferKey>,
|
2023-10-26 11:46:22 +08:00
|
|
|
currentSelectedIndex: number,
|
|
|
|
) => {
|
|
|
|
const isLeftDirection = direction === 'left';
|
|
|
|
const multipleSelect = isLeftDirection ? leftMultipleSelect : rightMultipleSelect;
|
|
|
|
multipleSelect(currentSelectedIndex, data, holder);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onItemSelect = (
|
|
|
|
direction: TransferDirection,
|
2024-03-14 21:14:21 +08:00
|
|
|
selectedKey: TransferKey,
|
2023-10-26 11:46:22 +08:00
|
|
|
checked: boolean,
|
|
|
|
multiple?: boolean,
|
|
|
|
) => {
|
|
|
|
const isLeftDirection = direction === 'left';
|
|
|
|
const holder = [...(isLeftDirection ? sourceSelectedKeys : targetSelectedKeys)];
|
|
|
|
const holderSet = new Set(holder);
|
|
|
|
const data = [...(isLeftDirection ? leftDataSource : rightDataSource)].filter(
|
2024-04-26 21:22:24 +08:00
|
|
|
(item) => !item?.disabled,
|
2023-10-26 11:46:22 +08:00
|
|
|
);
|
|
|
|
const currentSelectedIndex = data.findIndex((item) => item.key === selectedKey);
|
|
|
|
// multiple select by hold down the shift key
|
|
|
|
if (multiple && holder.length > 0) {
|
2024-04-01 15:49:45 +08:00
|
|
|
handleMultipleSelect(direction, data as any, holderSet, currentSelectedIndex);
|
2023-10-26 11:46:22 +08:00
|
|
|
} else {
|
|
|
|
handleSingleSelect(direction, holderSet, selectedKey, checked, currentSelectedIndex);
|
|
|
|
}
|
|
|
|
const holderArr = Array.from(holderSet);
|
|
|
|
handleSelectChange(direction, holderArr);
|
2023-01-04 15:09:03 +08:00
|
|
|
if (!props.selectedKeys) {
|
2023-10-26 11:46:22 +08:00
|
|
|
setStateKeys(direction, holderArr);
|
2016-11-04 17:46:01 +08:00
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2015-11-25 23:17:06 +08:00
|
|
|
|
2024-04-01 15:49:45 +08:00
|
|
|
const onLeftItemSelect: TransferListProps<KeyWise<RecordType>>['onItemSelect'] = (
|
|
|
|
selectedKey,
|
|
|
|
checked,
|
|
|
|
e,
|
2023-10-26 11:46:22 +08:00
|
|
|
) => {
|
|
|
|
onItemSelect('left', selectedKey, checked, e?.shiftKey);
|
2023-01-04 15:09:03 +08:00
|
|
|
};
|
2020-05-13 19:15:40 +08:00
|
|
|
|
2023-10-26 11:46:22 +08:00
|
|
|
const onRightItemSelect = (
|
2024-03-14 21:14:21 +08:00
|
|
|
selectedKey: TransferKey,
|
2023-10-26 11:46:22 +08:00
|
|
|
checked: boolean,
|
|
|
|
e?: React.MouseEvent<Element, MouseEvent>,
|
|
|
|
) => {
|
|
|
|
onItemSelect('right', selectedKey, checked, e?.shiftKey);
|
2023-01-04 15:09:03 +08:00
|
|
|
};
|
2020-05-13 19:15:40 +08:00
|
|
|
|
2024-03-14 21:14:21 +08:00
|
|
|
const onRightItemRemove = (keys: TransferKey[]) => {
|
2023-01-04 15:09:03 +08:00
|
|
|
setStateKeys('right', []);
|
2021-02-19 18:26:53 +08:00
|
|
|
onChange?.(
|
2023-01-04 15:09:03 +08:00
|
|
|
targetKeys.filter((key) => !keys.includes(key)),
|
2021-02-19 18:26:53 +08:00
|
|
|
'left',
|
2023-01-04 15:09:03 +08:00
|
|
|
[...keys],
|
2021-02-19 18:26:53 +08:00
|
|
|
);
|
2020-05-13 19:15:40 +08:00
|
|
|
};
|
|
|
|
|
2023-01-08 15:36:50 +08:00
|
|
|
const handleListStyle = (direction: TransferDirection): CSSProperties => {
|
|
|
|
if (typeof listStyle === 'function') {
|
|
|
|
return listStyle({ direction });
|
2019-10-22 11:05:39 +08:00
|
|
|
}
|
2023-01-08 15:36:50 +08:00
|
|
|
return listStyle || {};
|
2019-10-22 11:05:39 +08:00
|
|
|
};
|
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const formItemContext = useContext<FormItemStatusContextProps>(FormItemInputContext);
|
2016-11-04 17:06:57 +08:00
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const { hasFeedback, status } = formItemContext;
|
|
|
|
|
2023-01-09 10:04:35 +08:00
|
|
|
const getLocale = (transferLocale: TransferLocale) => ({
|
|
|
|
...transferLocale,
|
|
|
|
notFoundContent: renderEmpty?.('Transfer') || <DefaultRenderEmpty componentName="Transfer" />,
|
|
|
|
...locale,
|
|
|
|
});
|
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const mergedStatus = getMergedStatus(status, customStatus);
|
|
|
|
const mergedPagination = !children && pagination;
|
|
|
|
|
|
|
|
const leftActive = targetSelectedKeys.length > 0;
|
|
|
|
const rightActive = sourceSelectedKeys.length > 0;
|
|
|
|
|
|
|
|
const cls = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
[`${prefixCls}-customize-list`]: !!children,
|
2023-01-20 11:03:50 +08:00
|
|
|
[`${prefixCls}-rtl`]: dir === 'rtl',
|
2023-01-04 15:09:03 +08:00
|
|
|
},
|
|
|
|
getStatusClassNames(prefixCls, mergedStatus, hasFeedback),
|
2023-07-06 09:36:45 +08:00
|
|
|
transfer?.className,
|
2023-01-04 15:09:03 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2023-01-04 15:09:03 +08:00
|
|
|
);
|
|
|
|
|
2023-02-24 10:51:59 +08:00
|
|
|
const [contextLocale] = useLocale('Transfer', defaultLocale.Transfer);
|
2023-02-22 18:18:26 +08:00
|
|
|
|
|
|
|
const listLocale = getLocale(contextLocale!);
|
|
|
|
|
|
|
|
const [leftTitle, rightTitle] = getTitles(listLocale);
|
|
|
|
|
2024-02-02 10:48:05 +08:00
|
|
|
const mergedSelectionsIcon = selectionsIcon ?? transfer?.selectionsIcon;
|
|
|
|
|
2023-11-13 15:35:39 +08:00
|
|
|
return wrapCSSVar(
|
2023-07-06 09:36:45 +08:00
|
|
|
<div className={cls} style={{ ...transfer?.style, ...style }}>
|
2023-02-22 18:18:26 +08:00
|
|
|
<List<KeyWise<RecordType>>
|
|
|
|
prefixCls={`${prefixCls}-list`}
|
|
|
|
titleText={leftTitle}
|
2024-04-01 15:49:45 +08:00
|
|
|
dataSource={leftDataSource as any}
|
2023-02-22 18:18:26 +08:00
|
|
|
filterOption={filterOption}
|
|
|
|
style={handleListStyle('left')}
|
|
|
|
checkedKeys={sourceSelectedKeys}
|
|
|
|
handleFilter={leftFilter}
|
|
|
|
handleClear={handleLeftClear}
|
|
|
|
onItemSelect={onLeftItemSelect}
|
2024-04-01 15:49:45 +08:00
|
|
|
onItemSelectAll={onLeftItemSelectAll as any}
|
2023-02-22 18:18:26 +08:00
|
|
|
render={render}
|
|
|
|
showSearch={showSearch}
|
2024-04-01 15:49:45 +08:00
|
|
|
renderList={children as any}
|
|
|
|
footer={footer as any}
|
2023-02-22 18:18:26 +08:00
|
|
|
onScroll={handleLeftScroll}
|
|
|
|
disabled={disabled}
|
|
|
|
direction={dir === 'rtl' ? 'right' : 'left'}
|
|
|
|
showSelectAll={showSelectAll}
|
|
|
|
selectAllLabel={selectAllLabels[0]}
|
|
|
|
pagination={mergedPagination}
|
2024-02-02 10:48:05 +08:00
|
|
|
selectionsIcon={mergedSelectionsIcon}
|
2023-02-22 18:18:26 +08:00
|
|
|
{...listLocale}
|
|
|
|
/>
|
|
|
|
<Operation
|
|
|
|
className={`${prefixCls}-operation`}
|
|
|
|
rightActive={rightActive}
|
|
|
|
rightArrowText={operations[0]}
|
|
|
|
moveToRight={moveToRight}
|
|
|
|
leftActive={leftActive}
|
|
|
|
leftArrowText={operations[1]}
|
|
|
|
moveToLeft={moveToLeft}
|
|
|
|
style={operationStyle}
|
|
|
|
disabled={disabled}
|
|
|
|
direction={dir}
|
|
|
|
oneWay={oneWay}
|
|
|
|
/>
|
|
|
|
<List<KeyWise<RecordType>>
|
|
|
|
prefixCls={`${prefixCls}-list`}
|
|
|
|
titleText={rightTitle}
|
2024-04-01 15:49:45 +08:00
|
|
|
dataSource={rightDataSource as any}
|
2023-02-22 18:18:26 +08:00
|
|
|
filterOption={filterOption}
|
|
|
|
style={handleListStyle('right')}
|
|
|
|
checkedKeys={targetSelectedKeys}
|
|
|
|
handleFilter={rightFilter}
|
|
|
|
handleClear={handleRightClear}
|
|
|
|
onItemSelect={onRightItemSelect}
|
2024-04-01 15:49:45 +08:00
|
|
|
onItemSelectAll={onRightItemSelectAll as any}
|
2023-02-22 18:18:26 +08:00
|
|
|
onItemRemove={onRightItemRemove}
|
|
|
|
render={render}
|
|
|
|
showSearch={showSearch}
|
2024-04-01 15:49:45 +08:00
|
|
|
renderList={children as any}
|
|
|
|
footer={footer as any}
|
2023-02-22 18:18:26 +08:00
|
|
|
onScroll={handleRightScroll}
|
|
|
|
disabled={disabled}
|
|
|
|
direction={dir === 'rtl' ? 'left' : 'right'}
|
|
|
|
showSelectAll={showSelectAll}
|
|
|
|
selectAllLabel={selectAllLabels[1]}
|
|
|
|
showRemove={oneWay}
|
|
|
|
pagination={mergedPagination}
|
2024-02-02 10:48:05 +08:00
|
|
|
selectionsIcon={mergedSelectionsIcon}
|
2023-02-22 18:18:26 +08:00
|
|
|
{...listLocale}
|
|
|
|
/>
|
|
|
|
</div>,
|
2023-01-04 15:09:03 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Transfer.displayName = 'Transfer';
|
2017-10-20 14:54:38 +08:00
|
|
|
}
|
2019-03-07 15:39:34 +08:00
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
Transfer.List = List;
|
|
|
|
Transfer.Search = Search;
|
|
|
|
Transfer.Operation = Operation;
|
|
|
|
|
2019-03-07 15:39:34 +08:00
|
|
|
export default Transfer;
|