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:多次搜索导致已选库表被清空问题 #56

Merged
merged 1 commit into from
May 7, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/component/Task/component/DatabaseSelecter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import ExportCard from '@/component/ExportCard';
import DataBaseStatusIcon from '@/component/StatusIcon/DatabaseIcon';
import { DeleteOutlined } from '@ant-design/icons';
import { Empty, Popconfirm, Space, Spin, Tree, Typography, Checkbox } from 'antd';
import React, { useEffect, useState } from 'react';
import { DataNode } from 'antd/lib/tree';
import React, { useCallback, useEffect, useState } from 'react';
import { DataNode, TreeProps } from 'antd/lib/tree';
import classnames from 'classnames';
import styles from './index.less';

Expand Down Expand Up @@ -150,6 +150,19 @@ const DatabaseSelecter: React.FC<IProps> = function ({
const handleSearch = (value) => {
setSourceSearchValue(value);
};
/**
* 选中一个库后
*/
const handleChosenDataBase: TreeProps['onCheck'] = useCallback(
(_checkedKeys, { checked, node: { key: curNodeKey } }) => {
if (checked) {
onChange([...checkedKeys, curNodeKey]);
} else {
onChange(checkedKeys.filter((key) => key !== curNodeKey));
}
},
[checkedKeys, onChange],
);

const allTreeDataKeys = getAllTreeDataKeys();
const checkAll = allTreeDataKeys?.length && allTreeDataKeys.length === checkedKeys.length;
Expand Down Expand Up @@ -192,9 +205,7 @@ const DatabaseSelecter: React.FC<IProps> = function ({
className={styles.allTree}
treeData={allTreeData}
checkedKeys={checkedKeys}
onCheck={(_checkedKeys) => {
onChange(_checkedKeys as string[]);
}}
onCheck={handleChosenDataBase}
/>
</ExportCard>
</Spin>
Expand Down
24 changes: 14 additions & 10 deletions src/component/Task/component/TableSelecter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const getTreeData = (validTableList: IDataBaseWithTable[]) => {
</Space>
),
key: id,
icon: getDataSourceStyleByConnectType(dataSource.type).dbIcon.component,
icon: <Icon component={getDataSourceStyleByConnectType(dataSource.type).dbIcon.component} />,
checkable: false,
expandable: true,
children,
Expand Down Expand Up @@ -269,7 +269,10 @@ const TableSelecter: React.ForwardRefRenderFunction<TableSelecterRef, IProps> =
const nodeKey = key as string;
remainKeys = checkedKeys.filter((key) => key !== nodeKey);
}
onChange(remainKeys.map(parseDataBaseIdAndTableNamebByKey));
const newValue = remainKeys.map(parseDataBaseIdAndTableNamebByKey);
onChange(newValue);
const willExpandKeys: number[] = newValue.map(({ databaseId }) => databaseId);
setSelectedExpandKeys(Array.from(new Set(willExpandKeys)));
},
[checkedKeys, onChange],
);
Expand Down Expand Up @@ -309,18 +312,19 @@ const TableSelecter: React.ForwardRefRenderFunction<TableSelecterRef, IProps> =
* 选中一张表后
*/
const handleChosenTable: TreeProps['onCheck'] = useCallback(
(_checkedKeys: string[]) => {
const newValue: TableItem[] = [];
const willExpandKeys: number[] = [];
_checkedKeys.forEach((key) => {
const tableItem = parseDataBaseIdAndTableNamebByKey(key);
(_checkedKeys: string[], { checked, node: { key: curNodeKey } }) => {
const preCheckKeys = checked ? checkedKeys : checkedKeys.filter((key) => key !== curNodeKey);
const newValue: TableItem[] = preCheckKeys.map(parseDataBaseIdAndTableNamebByKey);
const willExpandKeys: number[] = newValue.map(({ databaseId }) => databaseId);
if (checked) {
const tableItem = parseDataBaseIdAndTableNamebByKey(curNodeKey as string);
newValue.push(tableItem);
willExpandKeys.push(tableItem.databaseId);
});
}
onChange(newValue);
setSelectedExpandKeys(willExpandKeys);
setSelectedExpandKeys(Array.from(new Set(willExpandKeys)));
},
[onChange],
[checkedKeys, onChange],
);

useImperativeHandle(
Expand Down
Loading