import { MenuOutlined } from '@ant-design/icons'; import { css } from '@emotion/css'; import { ArrayField, Field } from '@formily/core'; import { observer, RecursionField, Schema, useField, useFieldSchema } from '@formily/react'; import { Table, TableColumnProps } from 'antd'; import { default as classNames, default as cls } from 'classnames'; import React, { useState } from 'react'; import ReactDragListView from 'react-drag-listview'; import { DndContext } from '../..'; import { RecordIndexProvider, RecordProvider, useRequest, useSchemaInitializer } from '../../../'; const isColumnComponent = (schema: Schema) => { return schema['x-component']?.endsWith('.Column') > -1; }; const useTableColumns = () => { const start = Date.now(); const field = useField(); const schema = useFieldSchema(); const { exists, render } = useSchemaInitializer(schema['x-initializer']); const columns = schema .reduceProperties((buf, s) => { if (isColumnComponent(s)) { return buf.concat([s]); } }, []) .map((s: Schema) => { return { title: , dataIndex: s.name, key: s.name, render: (v, record) => { const index = field.value?.indexOf(record); console.log((Date.now() - start) / 1000); return ( ); }, } as TableColumnProps; }); if (!exists) { return columns; } return columns.concat({ title: render(), dataIndex: 'TABLE_COLUMN_INITIALIZER', key: 'TABLE_COLUMN_INITIALIZER', }); }; export const components = { header: { wrapper: (props) => { return ( ); }, cell: (props) => { return ( ); }, }, body: { wrapper: (props) => { return ( ); }, row: (props) => { return ; }, cell: (props) => ( ), }, }; const useDef = () => { const [selectedRowKeys, setSelectedRowKeys] = useState([]); return [selectedRowKeys, setSelectedRowKeys]; }; const useDefDataSource = (options, props) => { const field = useField(); return useRequest(() => { return Promise.resolve({ data: field.value, }); }, options); }; const SortHandle = () => { return ; }; const TableIndex = (props) => { const { index } = props; return (
{index + 1}
); }; const useDefAction = () => { return { async move() {}, }; }; export const TableArray: React.FC = observer((props) => { const field = useField(); const columns = useTableColumns(); const { dragSort = false, showIndex = true, useSelectedRowKeys = useDef, useDataSource = useDefDataSource, useAction = useDefAction, onChange, ...others } = props; const [selectedRowKeys, setSelectedRowKeys] = useSelectedRowKeys(); useDataSource({ onSuccess(data) { field.value = data?.data || []; }, }); const { move } = useAction(); const restProps = { rowSelection: props.rowSelection ? { type: 'checkbox', selectedRowKeys, onChange(selectedRowKeys: any[]) { setSelectedRowKeys(selectedRowKeys); }, renderCell: (checked, record, index, originNode) => { const current = props?.pagination?.current; const pageSize = props?.pagination?.pageSize || 20; if (current) { index = index + (current - 1) * pageSize; } return (
{dragSort && } {showIndex && }
{originNode}
); }, ...props.rowSelection, } : undefined, }; const defaultRowKey = (record: any) => { return field.value?.indexOf?.(record); }; return (
{ const from = field.value[fromIndex]; const to = field.value[toIndex]; field.move(fromIndex, toIndex); await move(from, to); }} lineClassName={css` border-bottom: 2px solid rgba(241, 139, 98, 0.6) !important; `} > ); });