mirror of
https://github.com/ant-design/ant-design.git
synced 2025-08-06 16:06:28 +08:00
refactor: simplify code
This commit is contained in:
parent
6c94a5887e
commit
a081100cdc
@ -74,7 +74,10 @@ abstract class Transfer extends React.Component<TransferProps, any> {
|
|||||||
lazy: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
|
lazy: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
|
||||||
};
|
};
|
||||||
|
|
||||||
splitedDataSource: any;
|
splitedDataSource: {
|
||||||
|
leftDataSource: TransferItem[],
|
||||||
|
rightDataSource: TransferItem[],
|
||||||
|
} | null;
|
||||||
|
|
||||||
constructor(props: TransferProps) {
|
constructor(props: TransferProps) {
|
||||||
super(props);
|
super(props);
|
||||||
@ -102,15 +105,15 @@ abstract class Transfer extends React.Component<TransferProps, any> {
|
|||||||
// clear key nolonger existed
|
// clear key nolonger existed
|
||||||
// clear checkedKeys according to targetKeys
|
// clear checkedKeys according to targetKeys
|
||||||
const { dataSource, targetKeys = [] } = nextProps;
|
const { dataSource, targetKeys = [] } = nextProps;
|
||||||
|
|
||||||
const newSourceSelectedKeys: String[] = [];
|
const newSourceSelectedKeys: String[] = [];
|
||||||
const newTargetSelectedKeys: String[] = [];
|
const newTargetSelectedKeys: String[] = [];
|
||||||
|
dataSource.forEach(({ key }) => {
|
||||||
dataSource.forEach(record => {
|
if (sourceSelectedKeys.includes(key) && !targetKeys.includes(key)) {
|
||||||
if (sourceSelectedKeys.includes(record.key) && !targetKeys.includes(record.key)) {
|
newSourceSelectedKeys.push(key);
|
||||||
newSourceSelectedKeys.push(record.key);
|
|
||||||
}
|
}
|
||||||
if (targetSelectedKeys.includes(record.key) && targetKeys.includes(record.key)) {
|
if (targetSelectedKeys.includes(key) && targetKeys.includes(key)) {
|
||||||
newTargetSelectedKeys.push(record.key);
|
newTargetSelectedKeys.push(key);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.setState({
|
this.setState({
|
||||||
@ -123,8 +126,8 @@ abstract class Transfer extends React.Component<TransferProps, any> {
|
|||||||
if (nextProps.selectedKeys) {
|
if (nextProps.selectedKeys) {
|
||||||
const targetKeys = nextProps.targetKeys;
|
const targetKeys = nextProps.targetKeys;
|
||||||
this.setState({
|
this.setState({
|
||||||
sourceSelectedKeys: nextProps.selectedKeys.filter(key => targetKeys.indexOf(key) === -1),
|
sourceSelectedKeys: nextProps.selectedKeys.filter(key => !targetKeys.includes(key)),
|
||||||
targetSelectedKeys: nextProps.selectedKeys.filter(key => targetKeys.indexOf(key) > -1),
|
targetSelectedKeys: nextProps.selectedKeys.filter(key => targetKeys.includes(key)),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -134,34 +137,25 @@ abstract class Transfer extends React.Component<TransferProps, any> {
|
|||||||
return this.splitedDataSource;
|
return this.splitedDataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { rowKey, dataSource, targetKeys = [] } = props;
|
const { dataSource, rowKey, targetKeys = [] } = props;
|
||||||
|
|
||||||
const leftDataSource: TransferItem[] = [];
|
const leftDataSource: TransferItem[] = [];
|
||||||
const rightDataSource: TransferItem[] = [];
|
const rightDataSource: TransferItem[] = new Array(targetKeys.length);
|
||||||
const tempRightDataSource: TransferItem[] = [];
|
|
||||||
|
|
||||||
dataSource.forEach(record => {
|
dataSource.forEach(record => {
|
||||||
if (rowKey) {
|
if (rowKey) {
|
||||||
record.key = rowKey(record);
|
record.key = rowKey(record);
|
||||||
}
|
}
|
||||||
if (targetKeys.includes(record.key)) {
|
|
||||||
tempRightDataSource.push(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;
|
||||||
} else {
|
} else {
|
||||||
leftDataSource.push(record);
|
leftDataSource.push(record);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const itemMap = {};
|
|
||||||
tempRightDataSource.forEach((item) => {
|
|
||||||
itemMap[item.key] = item;
|
|
||||||
});
|
|
||||||
|
|
||||||
targetKeys.forEach((targetKey) => {
|
|
||||||
if (itemMap[targetKey]) {
|
|
||||||
rightDataSource.push(itemMap[targetKey]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
this.splitedDataSource = {
|
this.splitedDataSource = {
|
||||||
leftDataSource,
|
leftDataSource,
|
||||||
rightDataSource,
|
rightDataSource,
|
||||||
|
Loading…
Reference in New Issue
Block a user