Skip to content

Commit b1b56ef

Browse files
committed
reconstruction
1 parent d3f5ec6 commit b1b56ef

8 files changed

Lines changed: 391 additions & 296 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- ⭐【New Features】
77
- ⚡️【Optimize】After the update is completed, click restart to close the problem that cannot be automatically opened (hot update cannot fix this problem, you need to download a new version to cover the client)
88
- 🐞【Fixed】database and schema searches support case ambiguity matching
9+
- 🐞【Fixed】Where database was not displayed after being added
910

1011

1112
## 3.0.10

CHANGELOG_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- ⚡️【优化】
88
- 🐞【修复】更新完成后点击重启关闭后无法自动打开问题(热更新无法修复该问题,需要下载新版版本覆盖客户端)
99
- 🐞【修复】database和schema搜索支持大小写模糊匹配
10-
- 🐞【修复】
10+
- 🐞【修复】修复添加database后不显示问题
1111

1212
## 3.0.10
1313

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
import lodash from 'lodash';
2+
import { CRUD } from '@/constants';
3+
import { USER_FILLED_VALUE, IUpdateData } from './index';
4+
5+
export interface IProps {
6+
preCode: string;
7+
//
8+
tableData: { [key: string]: string | null }[];
9+
setTableData: (tableData: { [key: string]: string | null }[]) => void;
10+
//
11+
editingCell: [string, string, boolean] | null;
12+
setEditingCell: (editingCell: [string, string, boolean] | null) => void;
13+
//
14+
updateData: IUpdateData[];
15+
setUpdateData: (updateData: IUpdateData[]) => void;
16+
//
17+
curOperationRowNo: string | null;
18+
setCurOperationRowNo: (curOperationRowNo: string | null)=>void;
19+
//
20+
columns;
21+
oldDataList;
22+
queryResultData;
23+
tableBoxRef;
24+
oldTableData
25+
}
26+
27+
const useCurdTableData = (props: IProps) => {
28+
const {
29+
tableData,
30+
setTableData,
31+
preCode,
32+
editingCell,
33+
columns,
34+
curOperationRowNo,
35+
oldDataList,
36+
updateData,
37+
setUpdateData,
38+
queryResultData,
39+
setCurOperationRowNo,
40+
setEditingCell,
41+
tableBoxRef,
42+
oldTableData
43+
} = props;
44+
45+
// 编辑数据
46+
const updateTableData = (type: 'setCell' | 'setRow', _data: string | null | Array<string | null>) => {
47+
const newTableData = lodash.cloneDeep(tableData);
48+
let oldRowDataList: Array<string | null> = [];
49+
let newRowDataList: Array<string | null> = [];
50+
let curRowNo: string | null = '0';
51+
if (type === 'setCell' && (typeof _data === 'string' || _data === null)) {
52+
const [colId, rowId] = editingCell!;
53+
curRowNo = rowId;
54+
newTableData.forEach((item) => {
55+
if (item[`${preCode}0No.`] === rowId) {
56+
item[colId] = _data;
57+
newRowDataList = Object.keys(item).map((i) => item[i]);
58+
}
59+
});
60+
}
61+
62+
if (type === 'setRow' && Array.isArray(_data)) {
63+
curRowNo = curOperationRowNo;
64+
_data.unshift(curOperationRowNo);
65+
newTableData.forEach((t) => {
66+
if (t[`${preCode}0No.`] === curOperationRowNo) {
67+
const dataLength = Object.keys(t).length;
68+
Object.keys(t).forEach((item, index) => {
69+
if (index > dataLength) return;
70+
t[item] = _data[index] || null;
71+
});
72+
return;
73+
}
74+
});
75+
newRowDataList = _data;
76+
}
77+
78+
setTableData(newTableData);
79+
80+
oldDataList.forEach((item) => {
81+
if (item[0] === curRowNo) {
82+
oldRowDataList = item;
83+
}
84+
});
85+
86+
const index = updateData.findIndex((item) => item.rowId === curRowNo);
87+
// 如果newRowDataList和oldRowDataList的数据一样,代表用户虽然编辑过,但是又改回去了,则不需要更新
88+
if (oldRowDataList?.join(',') === newRowDataList?.join(',')) {
89+
if (index !== -1) {
90+
setUpdateData(updateData.filter((item) => item.rowId !== curRowNo && item.type !== CRUD.UPDATE));
91+
}
92+
return;
93+
}
94+
95+
if (index === -1) {
96+
setUpdateData([
97+
...updateData,
98+
{
99+
type: CRUD.UPDATE,
100+
oldDataList: oldRowDataList,
101+
dataList: newRowDataList,
102+
rowId: curRowNo!,
103+
},
104+
]);
105+
return;
106+
}
107+
108+
const newRowUpdateData = {
109+
...updateData[index],
110+
dataList: newRowDataList,
111+
};
112+
113+
// 如果是删除过的,则需要把type改为update
114+
if (newRowUpdateData.type === CRUD.DELETE) {
115+
newRowUpdateData.type = CRUD.UPDATE;
116+
}
117+
118+
updateData[index] = newRowUpdateData;
119+
setUpdateData([...updateData]);
120+
};
121+
122+
// 处理创建数据
123+
const handleCreateData = (_newData?: any) => {
124+
// 正常的新增
125+
const newTableData = lodash.cloneDeep(tableData);
126+
let newData = {};
127+
if (_newData) {
128+
newData = _newData;
129+
} else {
130+
columns.forEach((t, i) => {
131+
if (t.name === 'No.') {
132+
newData[`${preCode}${i}${t.name}`] = (newTableData.length + 1).toString();
133+
} else {
134+
// 判断是否有默认值
135+
const hasDefaultValue =
136+
queryResultData.headerList.find((item) => item.name === t.name)?.defaultValue !== null;
137+
if (hasDefaultValue) {
138+
newData[`${preCode}${i}${t.name}`] = USER_FILLED_VALUE.DEFAULT;
139+
return;
140+
}
141+
newData[`${preCode}${i}${t.name}`] = null;
142+
}
143+
});
144+
}
145+
newTableData.push(newData);
146+
setTableData(newTableData);
147+
setUpdateData([
148+
...updateData,
149+
{
150+
type: CRUD.CREATE,
151+
dataList: Object.keys(newData).map((item) => newData[item]),
152+
rowId: newTableData.length.toString(),
153+
},
154+
]);
155+
setCurOperationRowNo(newTableData.length.toString());
156+
setEditingCell(null);
157+
158+
// 新增一条数据,tableBox需要滚动到最下方
159+
setTimeout(() => {
160+
tableBoxRef.current?.scrollTo(0, tableBoxRef.current?.scrollHeight + 31);
161+
}, 0);
162+
};
163+
164+
// 处理删除数据
165+
const handleDeleteData = () => {
166+
const rowId = curOperationRowNo || editingCell?.[1];
167+
if (rowId === null) {
168+
return;
169+
}
170+
// 如果是新增的行,则直接删除
171+
const index = updateData.findIndex((item) => item.rowId === rowId && item.type === CRUD.CREATE);
172+
if (index !== -1) {
173+
updateData.splice(index, 1);
174+
setUpdateData([...updateData]);
175+
setTableData(tableData.filter((item) => item[`${preCode}0No.`] !== rowId));
176+
setCurOperationRowNo(null);
177+
return;
178+
}
179+
180+
// 正常的删除数据
181+
const deleteIndex = updateData.findIndex((t) => t.rowId === rowId);
182+
if (deleteIndex !== -1) {
183+
updateData.splice(deleteIndex, 1);
184+
}
185+
186+
// 如果删除的这个数据时编辑过的,要把这个数据恢复
187+
setTableData(
188+
tableData.map((item) =>
189+
item[`${preCode}0No.`] === rowId ? oldTableData.find((i) => i[`${preCode}0No.`] === rowId)! : item,
190+
),
191+
);
192+
const newDataOldList = oldDataList.find((item) => item[0] === rowId);
193+
setUpdateData([
194+
...updateData,
195+
{
196+
type: CRUD.DELETE,
197+
oldDataList: newDataOldList,
198+
rowId: rowId!,
199+
},
200+
]);
201+
setEditingCell(null);
202+
setCurOperationRowNo(null);
203+
};
204+
205+
return {
206+
updateTableData,
207+
handleCreateData,
208+
handleDeleteData
209+
};
210+
};
211+
212+
export default useCurdTableData;

0 commit comments

Comments
 (0)