Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(antd/transfer): transfer performance optimization while dataSourc… #39465

Merged
merged 3 commits into from Dec 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions components/_util/transKeys.ts
@@ -0,0 +1,16 @@
export const groupKeysMap = (keys: string[]) => {
const map = new Map<string, number>();
keys.forEach((key, index) => {
map.set(key, index);
});
return map;
};
export const groupDisabledKeysMap = <RecordType extends any[]>(dataSource: RecordType) => {
const map = new Map<string, number>();
dataSource.forEach(({ disabled, key }, index) => {
if (disabled) {
map.set(key, index);
}
});
return map;
};
20 changes: 11 additions & 9 deletions components/transfer/index.tsx
Expand Up @@ -8,6 +8,7 @@ import LocaleReceiver from '../locale/LocaleReceiver';
import defaultLocale from '../locale/en_US';
import type { InputStatus } from '../_util/statusUtils';
import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
import { groupKeysMap, groupDisabledKeysMap } from '../_util/transKeys';
import warning from '../_util/warning';
import type { PaginationType } from './interface';
import type { TransferListProps } from './list';
Expand Down Expand Up @@ -203,15 +204,16 @@ class Transfer<RecordType extends TransferItem = TransferItem> extends React.Com
const { targetKeys = [], dataSource = [], onChange } = this.props;
const { sourceSelectedKeys, targetSelectedKeys } = this.state;
const moveKeys = direction === 'right' ? sourceSelectedKeys : targetSelectedKeys;
const dataSourceDisabledKeysMap = groupDisabledKeysMap(dataSource);

// filter the disabled options
const newMoveKeys = moveKeys.filter(
(key) => !dataSource.some((data) => !!(key === data.key && data.disabled)),
);
const newMoveKeys = moveKeys.filter((key) => !dataSourceDisabledKeysMap.has(key));
const newMoveKeysMap = groupKeysMap(newMoveKeys);
// move items to target box
const newTargetKeys =
direction === 'right'
? newMoveKeys.concat(targetKeys)
: targetKeys.filter((targetKey) => !newMoveKeys.includes(targetKey));
: targetKeys.filter((targetKey) => !newMoveKeysMap.has(targetKey));

// empty checked keys
const oppositeDirection = direction === 'right' ? 'left' : 'right';
Expand All @@ -232,8 +234,9 @@ class Transfer<RecordType extends TransferItem = TransferItem> extends React.Com
// Merge current keys with origin key
mergedCheckedKeys = Array.from(new Set<string>([...prevKeys, ...selectedKeys]));
} else {
const selectedKeysMap = groupKeysMap(selectedKeys);
// Remove current keys from origin keys
mergedCheckedKeys = prevKeys.filter((key) => !selectedKeys.includes(key));
mergedCheckedKeys = prevKeys.filter((key) => !selectedKeysMap.has(key));
}

this.handleSelectChange(direction, mergedCheckedKeys);
Expand Down Expand Up @@ -341,19 +344,18 @@ class Transfer<RecordType extends TransferItem = TransferItem> extends React.Com

const leftDataSource: KeyWise<RecordType>[] = [];
const rightDataSource: KeyWise<RecordType>[] = new Array(targetKeys.length);
const targetKeysMap = groupKeysMap(targetKeys);
dataSource.forEach((record: KeyWise<RecordType>) => {
if (rowKey) {
record = {
...record,
key: rowKey(record),
};
}

// rightDataSource should be ordered by targetKeys
// leftDataSource should be ordered by dataSource
const indexOfKey = targetKeys.indexOf(record.key);
if (indexOfKey !== -1) {
rightDataSource[indexOfKey] = record;
if (targetKeysMap.has(record.key)) {
rightDataSource[targetKeysMap.get(record.key)!] = record;
} else {
leftDataSource.push(record);
}
Expand Down
4 changes: 3 additions & 1 deletion components/transfer/list.tsx
Expand Up @@ -6,6 +6,7 @@ import Checkbox from '../checkbox';
import Dropdown from '../dropdown';
import type { MenuProps } from '../menu';
import { isValidElement } from '../_util/reactNode';
import { groupKeysMap } from '../_util/transKeys';
import type {
KeyWiseTransferItem,
RenderResult,
Expand Down Expand Up @@ -103,7 +104,8 @@ export default class TransferList<
if (checkedKeys.length === 0) {
return 'none';
}
if (filteredItems.every((item) => checkedKeys.includes(item.key) || !!item.disabled)) {
const checkedKeysMap = groupKeysMap(checkedKeys);
if (filteredItems.every((item) => checkedKeysMap.has(item.key) || !!item.disabled)) {
return 'all';
}
return 'part';
Expand Down