|
@@ -0,0 +1,166 @@
|
|
|
+import React, { useState } from 'react';
|
|
|
+// @ts-ignore
|
|
|
+import { Button, Input, Table } from 'antd';
|
|
|
+import type { ColumnsType } from 'antd/lib/table';
|
|
|
+import DDSelectField from '@/components/DDComponents/DDSelectField';
|
|
|
+import './index.css';
|
|
|
+import DDDateField from '@/components/DDComponents/DDDateField';
|
|
|
+import NumberField from '@/components/DDComponents/NumberField';
|
|
|
+import TextNote from '@/components/DDComponents/TextNote';
|
|
|
+import { PlusOutlined } from '@ant-design/icons';
|
|
|
+
|
|
|
+interface IProps {
|
|
|
+ tableID?: string;
|
|
|
+ columns?: Array<any>;
|
|
|
+ onChange: (e?: any, id?: string, label?: string) => void;
|
|
|
+}
|
|
|
+
|
|
|
+type TableDataType = {
|
|
|
+ index: number;
|
|
|
+ id?: string;
|
|
|
+ col1?: any;
|
|
|
+ col2?: any;
|
|
|
+ col3?: any;
|
|
|
+ col4?: any;
|
|
|
+ col5?: any;
|
|
|
+};
|
|
|
+
|
|
|
+function DIYTable(props: IProps) {
|
|
|
+ const { tableID, columns, onChange } = props;
|
|
|
+
|
|
|
+ const [tableData, setTableData] = useState<TableDataType[]>([{ index: 1 }]);
|
|
|
+ // 列配置
|
|
|
+ const tableColumnDef: ColumnsType<any> = [
|
|
|
+ {
|
|
|
+ title: '序号',
|
|
|
+ dataIndex: 'index',
|
|
|
+ className: 'hidden',
|
|
|
+ },
|
|
|
+ ];
|
|
|
+ if (columns !== undefined && columns.length) {
|
|
|
+ for (let index = 0; index < columns.length; index++) {
|
|
|
+ let column = columns[index];
|
|
|
+ let colDef: any = {
|
|
|
+ dataIndex: 'col' + (index + 1),
|
|
|
+ title: column.props.label,
|
|
|
+ className: 'p-8',
|
|
|
+ };
|
|
|
+ switch (column.componentName) {
|
|
|
+ case 'DDSelectField':
|
|
|
+ colDef.render = (_: any, __: any, rowIndex: number) => {
|
|
|
+ return (
|
|
|
+ <DDSelectField
|
|
|
+ id={
|
|
|
+ rowIndex + ',' + index + ';' + column.props.id + '>' + tableID
|
|
|
+ }
|
|
|
+ label={column.props.label + ';' + rowIndex + ',' + index}
|
|
|
+ style={{ padding: '0', margin: '0' }}
|
|
|
+ options={column.props.options}
|
|
|
+ disabled={column.props.disabled}
|
|
|
+ onChange={onChange}
|
|
|
+ />
|
|
|
+ );
|
|
|
+ };
|
|
|
+ break;
|
|
|
+ case 'DDDateField':
|
|
|
+ colDef.render = (_: any, __: any, rowIndex: number) => {
|
|
|
+ return (
|
|
|
+ <DDDateField
|
|
|
+ id={
|
|
|
+ rowIndex + ',' + index + ';' + column.props.id + '>' + tableID
|
|
|
+ }
|
|
|
+ label={column.props.label + ';' + rowIndex + ',' + index}
|
|
|
+ key={index + rowIndex + ''}
|
|
|
+ style={{ padding: '0', margin: '0' }}
|
|
|
+ placeholder={column.props.placeholder}
|
|
|
+ format={column.props.format}
|
|
|
+ disabled={column.props.disabled}
|
|
|
+ onChange={onChange}
|
|
|
+ />
|
|
|
+ );
|
|
|
+ };
|
|
|
+ break;
|
|
|
+ case 'NumberField':
|
|
|
+ colDef.render = (_: any, __: any, rowIndex: number) => {
|
|
|
+ return (
|
|
|
+ <NumberField
|
|
|
+ id={
|
|
|
+ rowIndex + ',' + index + ';' + column.props.id + '>' + tableID
|
|
|
+ }
|
|
|
+ label={column.props.label + ';' + rowIndex + ',' + index}
|
|
|
+ size="small"
|
|
|
+ width="50%"
|
|
|
+ style={{ padding: '4px 11px' }}
|
|
|
+ disabled={column.props.disabled}
|
|
|
+ unit={column.props.unit}
|
|
|
+ onChange={onChange}
|
|
|
+ />
|
|
|
+ );
|
|
|
+ };
|
|
|
+ break;
|
|
|
+ case 'TextField':
|
|
|
+ colDef.render = (_: any, __: any, rowIndex: number) => {
|
|
|
+ return (
|
|
|
+ <Input
|
|
|
+ disabled={column.props.disabled}
|
|
|
+ placeholder={column.props.placeholder}
|
|
|
+ onChange={(e) =>
|
|
|
+ onChange?.(
|
|
|
+ e.target.value,
|
|
|
+ rowIndex +
|
|
|
+ ',' +
|
|
|
+ index +
|
|
|
+ ';' +
|
|
|
+ column.props.id +
|
|
|
+ '>' +
|
|
|
+ tableID,
|
|
|
+ column.props.label + ';' + rowIndex + ',' + index,
|
|
|
+ )
|
|
|
+ }
|
|
|
+ />
|
|
|
+ );
|
|
|
+ };
|
|
|
+ break;
|
|
|
+ case 'TextNote':
|
|
|
+ colDef.title = '说明';
|
|
|
+ colDef.render = (_: any, __: any, rowIndex: number) => {
|
|
|
+ return (
|
|
|
+ <TextNote
|
|
|
+ id={
|
|
|
+ rowIndex + ',' + index + ';' + column.props.id + '>' + tableID
|
|
|
+ }
|
|
|
+ style={{ padding: '0', margin: '0' }}
|
|
|
+ value={column.props.placeholder}
|
|
|
+ />
|
|
|
+ );
|
|
|
+ };
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ tableColumnDef.push(colDef);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const handleRowChange = () => {
|
|
|
+ setTableData([...tableData, { index: tableData.length }]);
|
|
|
+ };
|
|
|
+
|
|
|
+ return (
|
|
|
+ <>
|
|
|
+ <Table
|
|
|
+ columns={tableColumnDef}
|
|
|
+ dataSource={tableData}
|
|
|
+ pagination={false}
|
|
|
+ />
|
|
|
+ <Button
|
|
|
+ type="dashed"
|
|
|
+ icon={<PlusOutlined />}
|
|
|
+ block
|
|
|
+ onClick={handleRowChange}
|
|
|
+ >
|
|
|
+ 新增行
|
|
|
+ </Button>
|
|
|
+ </>
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+export default DIYTable;
|