index.tsx
6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import React, { useContext, useEffect, useState, useRef } from 'react';
import { Form, Table } from 'antd';
import type { GetRef, InputRef, TableProps, } from 'antd';
import * as commonUtils from '@/utils/utils';
import FormItemType from '@/components/Common/CommonComponentNew';
import type { PropsType, RecordType, EditableRowProps, EditableCellProps, ShowConfigType } from './type';
import style from './index.less';
const TableNewEvent: React.FC<PropsType> = (props) => {
const addState: any = {};
// 保存行数据
addState.onSaveRowData = (record: any) => {
const { name, data = [] } = props;
const { sId } = record;
const iIndex = data.findIndex((item: any) => item.sId === sId);
const rowDataNew = {
...record,
handleType: record.handleType || 'update',
}
const dataNew = [...data];
dataNew[iIndex] = rowDataNew;
props.onSaveState({
[`${name}Data`]: dataNew,
})
};
// 获取字段配置
addState.getShowConfig = (sName: string, bSlaveInfo: boolean): ShowConfigType => {
/* 返回值声明 */
let showConfig = { sName: "" };
/* 通过sName过滤出对应的showConfig */
const { config, customConfig } = props;
if (customConfig) {
const iIndex = customConfig.findIndex(item => item.sName === sName && item.bVisible);
if (iIndex !== -1) {
showConfig = customConfig[iIndex];
}
} else if (config) {
const iIndex = config.gdsconfigformslave.findIndex(item => item.sName === sName && item.bVisible);
if (iIndex !== -1) {
showConfig = config.gdsconfigformslave[iIndex];
}
if (bSlaveInfo && props.slaveInfo?.config) {
const iIndex = props.slaveInfo.config.gdsconfigformslave.findIndex(item => item.sName === sName && item.bVisible);
if (iIndex !== -1) {
showConfig = props.slaveInfo.config.gdsconfigformslave[iIndex];
}
}
}
/* 返回值 */
return showConfig;
};
// 获取展示内容
addState.onGetTableChildNode = (sName: string, record: any, children: any): React.ReactNode => {
return children;
};
return {
...props,
...addState,
}
};
const TableNew: React.FC<PropsType> = (baseProps) => {
const props: any = TableNewEvent(baseProps);
const tableProps = {
columns: TableColumns(props),
dataSource: TableData(props),
components: TableComponents(props),
rowClassName: 'editable-row',
scroll: {
// x: 'max-content',
y: '100%',
},
bordered: true,
};
return (
<div className={style.tableNew}>
<Table<TableProps> {...tableProps} />
</div>
);
};
// 处理数据
const TableData = (props: PropsType): TableProps['dataSource'] => {
const { data = [] } = props;
return data.map((item: any) => ({
...item,
editable: true,
}));
}
// 处理表头
const TableColumns = (props: PropsType): PropsType['headerColumn'] => {
const { headerColumn = [], data = [], enabled } = props;
return headerColumn.map((columnOld) => {
const column = { ...columnOld };
column.ellipsis = true;
column.title = (
<>
{column.bNotEmpty && <span style={{ color: 'red' }}>*</span>}
{column.title}
</>
);
if (!('dataIndex' in column)) return column;
const { dataIndex } = column;
if (dataIndex === 'sPartNameStatus') {
interface TextJsonItem {
sProcessName: string;
sState: number;
}
return {
...column,
render: (text: string, record: object) => {
const textJson: TextJsonItem[] = commonUtils.convertStrToObj(text.replace(",]", ']'), []) as TextJsonItem[];
return textJson.map(item => item.sProcessName).join('->');
},
};
}
if (!enabled) {
return column;
}
return {
...column,
onCell: (record: RecordType) => ({
record,
...column,
}),
};
});
}
type FormInstance<T> = GetRef<typeof Form<T>>;
const EditableContext = React.createContext<FormInstance<any> | null>(null);
const TableComponents = (props: PropsType): TableProps['components'] => {
const { enabled } = props;
const EditableRow: React.FC<EditableRowProps> = ({ index, ...rest }) => {
const [form] = Form.useForm();
return (
<Form form={form} component={false}>
<EditableContext.Provider value={form}>
<tr {...rest} />
</EditableContext.Provider>
</Form>
);
};
const EditableCell: React.FC<EditableCellProps> = ({
title,
dataIndex,
record,
children,
bNotEmpty,
bReadonly,
...restProps
}) => {
const [editing, setEditing] = useState(false);
// const inputRef = useRef<any>(null);
const form = useContext(EditableContext) as FormInstance<any>;
const showConfig = props.getShowConfig(dataIndex);
// useEffect(() => {
// if (editing) {
// inputRef.current?.focus();
// }
// }, [editing]);
const toggleEdit = () => {
setEditing(!editing);
form.setFieldsValue({ [dataIndex]: record[dataIndex] });
};
const save = async () => {
try {
const values = await form.validateFields();
toggleEdit();
props.onSaveRowData({ ...record, ...values });
} catch (errInfo) {
console.log('Save failed:', errInfo);
}
};
// 是否可编辑
const bEidtable = enabled && !bReadonly;
// 是否显示编辑框
const bShowType = bEidtable && editing;
let childNode = bShowType ? '' : props.onGetTableChildNode(dataIndex, record, children);
const FormItemTypeProps = {
...props,
record,
showConfig,
form,
// ref: inputRef,
onSave: save,
onChange: props.onDataChange,
getDropDownData: props.getDropDownData,
}
if (bEidtable) {
childNode = editing ?
<FormItemType {...FormItemTypeProps} />
: (
<div
className="editable-cell-value-wrap"
onClick={toggleEdit}
>
{children}
</div>
);
}
return <td {...restProps}>{childNode}</td>;
};
return {
body: {
row: EditableRow,
cell: EditableCell,
},
};
}
export default TableNew;