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';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { PaginationType } 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 {
|
2020-11-08 15:28:03 +08:00
|
|
|
key?: string;
|
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
|
|
|
}
|
|
|
|
|
2020-11-08 15:28:03 +08:00
|
|
|
export type KeyWise<T> = T & { key: string };
|
|
|
|
|
|
|
|
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;
|
|
|
|
selectCurrent?: string;
|
|
|
|
selectInvert?: string;
|
|
|
|
removeAll?: string;
|
|
|
|
removeCurrent?: string;
|
2020-09-16 11:43:55 +08:00
|
|
|
}
|
2021-02-28 11:00:44 +08:00
|
|
|
|
2020-11-08 15:28:03 +08:00
|
|
|
export interface TransferProps<RecordType> {
|
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[];
|
2017-10-08 15:25:13 +08:00
|
|
|
targetKeys?: string[];
|
2016-11-04 17:46:01 +08:00
|
|
|
selectedKeys?: string[];
|
2020-11-08 15:28:03 +08:00
|
|
|
render?: TransferRender<RecordType>;
|
2020-11-04 14:12:45 +08:00
|
|
|
onChange?: (targetKeys: string[], direction: TransferDirection, moveKeys: string[]) => void;
|
2016-09-18 10:44:22 +08:00
|
|
|
onSelectChange?: (sourceSelectedKeys: string[], targetSelectedKeys: string[]) => 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;
|
2020-11-08 15:28:03 +08:00
|
|
|
rowKey?: (record: RecordType) => string;
|
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);
|
|
|
|
|
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
|
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,
|
|
|
|
] = useSelection(leftDataSource, rightDataSource, selectedKeys);
|
2020-05-13 19:15:40 +08:00
|
|
|
|
2023-10-26 11:46:22 +08:00
|
|
|
const [leftMultipleSelect, updateLeftPrevSelectedIndex] = useMultipleSelect<
|
|
|
|
KeyWise<RecordType>,
|
|
|
|
string
|
|
|
|
>((item) => item.key);
|
|
|
|
const [rightMultipleSelect, updateRightPrevSelectedIndex] = useMultipleSelect<
|
|
|
|
KeyWise<RecordType>,
|
|
|
|
string
|
|
|
|
>((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(
|
|
|
|
(direction: TransferDirection, keys: string[] | ((prevKeys: string[]) => string[])) => {
|
|
|
|
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(
|
|
|
|
(direction: TransferDirection, holder: string[]) => {
|
|
|
|
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) => {
|
2022-10-21 11:45:55 +08:00
|
|
|
let mergedCheckedKeys: string[] = [];
|
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
|
2023-01-04 15:09:03 +08:00
|
|
|
mergedCheckedKeys = Array.from(new Set<string>([...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,
|
|
|
|
holder: Set<string>,
|
|
|
|
selectedKey: string,
|
|
|
|
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>[],
|
|
|
|
holder: Set<string>,
|
|
|
|
currentSelectedIndex: number,
|
|
|
|
) => {
|
|
|
|
const isLeftDirection = direction === 'left';
|
|
|
|
const multipleSelect = isLeftDirection ? leftMultipleSelect : rightMultipleSelect;
|
|
|
|
multipleSelect(currentSelectedIndex, data, holder);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onItemSelect = (
|
|
|
|
direction: TransferDirection,
|
|
|
|
selectedKey: string,
|
|
|
|
checked: boolean,
|
|
|
|
multiple?: boolean,
|
|
|
|
) => {
|
|
|
|
const isLeftDirection = direction === 'left';
|
|
|
|
const holder = [...(isLeftDirection ? sourceSelectedKeys : targetSelectedKeys)];
|
|
|
|
const holderSet = new Set(holder);
|
|
|
|
const data = [...(isLeftDirection ? leftDataSource : rightDataSource)].filter(
|
|
|
|
(item) => !item.disabled,
|
|
|
|
);
|
|
|
|
const currentSelectedIndex = data.findIndex((item) => item.key === selectedKey);
|
|
|
|
// multiple select by hold down the shift key
|
|
|
|
if (multiple && holder.length > 0) {
|
|
|
|
handleMultipleSelect(direction, data, holderSet, currentSelectedIndex);
|
|
|
|
} 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
|
|
|
|
2023-10-26 11:46:22 +08:00
|
|
|
const onLeftItemSelect = (
|
|
|
|
selectedKey: string,
|
|
|
|
checked: boolean,
|
|
|
|
e?: React.MouseEvent<Element, MouseEvent>,
|
|
|
|
) => {
|
|
|
|
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 = (
|
|
|
|
selectedKey: string,
|
|
|
|
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
|
|
|
|
2023-01-04 15:09:03 +08:00
|
|
|
const onRightItemRemove = (keys: string[]) => {
|
|
|
|
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-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);
|
|
|
|
|
2023-01-20 11:03:50 +08:00
|
|
|
return wrapSSR(
|
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}
|
|
|
|
dataSource={leftDataSource}
|
|
|
|
filterOption={filterOption}
|
|
|
|
style={handleListStyle('left')}
|
|
|
|
checkedKeys={sourceSelectedKeys}
|
|
|
|
handleFilter={leftFilter}
|
|
|
|
handleClear={handleLeftClear}
|
|
|
|
onItemSelect={onLeftItemSelect}
|
|
|
|
onItemSelectAll={onLeftItemSelectAll}
|
|
|
|
render={render}
|
|
|
|
showSearch={showSearch}
|
|
|
|
renderList={children}
|
|
|
|
footer={footer}
|
|
|
|
onScroll={handleLeftScroll}
|
|
|
|
disabled={disabled}
|
|
|
|
direction={dir === 'rtl' ? 'right' : 'left'}
|
|
|
|
showSelectAll={showSelectAll}
|
|
|
|
selectAllLabel={selectAllLabels[0]}
|
|
|
|
pagination={mergedPagination}
|
2023-07-25 14:05:52 +08:00
|
|
|
selectionsIcon={selectionsIcon}
|
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}
|
|
|
|
dataSource={rightDataSource}
|
|
|
|
filterOption={filterOption}
|
|
|
|
style={handleListStyle('right')}
|
|
|
|
checkedKeys={targetSelectedKeys}
|
|
|
|
handleFilter={rightFilter}
|
|
|
|
handleClear={handleRightClear}
|
|
|
|
onItemSelect={onRightItemSelect}
|
|
|
|
onItemSelectAll={onRightItemSelectAll}
|
|
|
|
onItemRemove={onRightItemRemove}
|
|
|
|
render={render}
|
|
|
|
showSearch={showSearch}
|
|
|
|
renderList={children}
|
|
|
|
footer={footer}
|
|
|
|
onScroll={handleRightScroll}
|
|
|
|
disabled={disabled}
|
|
|
|
direction={dir === 'rtl' ? 'left' : 'right'}
|
|
|
|
showSelectAll={showSelectAll}
|
|
|
|
selectAllLabel={selectAllLabels[1]}
|
|
|
|
showRemove={oneWay}
|
|
|
|
pagination={mergedPagination}
|
2023-07-25 14:05:52 +08:00
|
|
|
selectionsIcon={selectionsIcon}
|
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;
|