Skip to content

Commit

Permalink
fix(antd/transfer): transKeys Map Structure Adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
wqs576222103 committed Dec 23, 2022
1 parent 10aebbc commit 4faf47a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
23 changes: 7 additions & 16 deletions components/_util/transKeys.ts
@@ -1,24 +1,15 @@
export type mapItem = {
k: string;
i: number;
};
export type MapType = Record<string, mapItem>;

export const groupKeysMap = (keys: string[]) => {
const map: MapType = {};
keys.forEach((k, i) => {
map[k] = { k, i };
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: MapType = {};
dataSource.forEach((d, i) => {
if (d.disabled) {
map[d.key] = {
k: d.key,
i,
};
const map = new Map<string, number>();
dataSource.forEach(({ disabled, key }, index) => {
if (disabled) {
map.set(key, index);
}
});
return map;
Expand Down
10 changes: 5 additions & 5 deletions components/transfer/index.tsx
Expand Up @@ -207,13 +207,13 @@ class Transfer<RecordType extends TransferItem = TransferItem> extends React.Com
const dataSourceDisabledKeysMap = groupDisabledKeysMap(dataSource);

// filter the disabled options
const newMoveKeys = moveKeys.filter((key) => !dataSourceDisabledKeysMap[key]);
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) => !newMoveKeysMap[targetKey]);
: targetKeys.filter((targetKey) => !newMoveKeysMap.has(targetKey));

// empty checked keys
const oppositeDirection = direction === 'right' ? 'left' : 'right';
Expand All @@ -236,7 +236,7 @@ class Transfer<RecordType extends TransferItem = TransferItem> extends React.Com
} else {
const selectedKeysMap = groupKeysMap(selectedKeys);
// Remove current keys from origin keys
mergedCheckedKeys = prevKeys.filter((key) => !selectedKeysMap[key]);
mergedCheckedKeys = prevKeys.filter((key) => !selectedKeysMap.has(key));
}

this.handleSelectChange(direction, mergedCheckedKeys);
Expand Down Expand Up @@ -354,8 +354,8 @@ class Transfer<RecordType extends TransferItem = TransferItem> extends React.Com
}
// rightDataSource should be ordered by targetKeys
// leftDataSource should be ordered by dataSource
if (targetKeysMap[record.key]) {
rightDataSource[targetKeysMap[record.key].i] = record;
if (targetKeysMap.has(record.key)) {
rightDataSource[targetKeysMap.get(record.key)!] = record;
} else {
leftDataSource.push(record);
}
Expand Down
4 changes: 2 additions & 2 deletions components/transfer/list.tsx
Expand Up @@ -104,8 +104,8 @@ export default class TransferList<
if (checkedKeys.length === 0) {
return 'none';
}
const keysMap = groupKeysMap(checkedKeys);
if (filteredItems.every((item) => keysMap[item.key] || !!item.disabled)) {
const checkedKeysMap = groupKeysMap(checkedKeys);
if (filteredItems.every((item) => checkedKeysMap.has(item.key) || !!item.disabled)) {
return 'all';
}
return 'part';
Expand Down

0 comments on commit 4faf47a

Please sign in to comment.