import React, { useState } from 'react';
import { Button, Icon, Table, Card, Field, Pagination, Form } from '@alifd/next';
import { useFullscreen, useFusionTable } from 'ahooks';
import classnames from 'classnames';
import { ITablePro_PC } from './type';
import { getTableDataFunc } from './model';
import './index.scss';
const FormItem = Form.Item;
const TableActionIcon = Icon.createFromIconfontCN({
scriptUrl: 'https://at.alicdn.com/t/font_1899388_oxn3zhg34oj.js',
});
const TablePro: ITablePro_PC = (props) => {
const {
columns = [],
getListUri,
getListHandle,
actionBar,
filterBar = [],
filterPerLineNumber = 4,
simple = false,
actionColumn = [],
actionColumnWidth,
onReset,
...restTableProps
} = props;
const renderActionBar = !!(actionBar && actionBar.length);
const renderFilterBar = !!(filterBar && filterBar.length);
const renderActionColumn = !!(actionColumn && actionColumn.length);
const colSpan = 12 / filterPerLineNumber;
const ActionBar = ({ hiddenRightAction = false }) => {
return (
{renderActionBar &&
actionBar?.map((action, index) => {
const { icon, title, render, ...restProps } = action;
return (
{!!render ? (
render
) : (
// @ts-ignore
)}
);
})}
{!hiddenRightAction && (
)}
);
};
// 切换紧凑模式
const [sizeStatus, changeSize] = useState<'small' | 'medium'>('small');
// 切换斑马线
const [zebraStatus, changeZebra] = useState(false);
// 切换全屏
const [isFullscreen, { toggleFullscreen }] = useFullscreen(document.getElementById('card-container'));
const Container = classnames('oc-container', { fullscreen: isFullscreen });
// 表格操作列
const columnPadding = 16;
const fontSize = 12;
const gutter = 10;
const finalColumns = renderActionColumn
? [
...columns,
{
title: '操作',
data: '_action',
lock: 'right',
width: !!actionColumnWidth
? actionColumnWidth
: actionColumn.reduce(
(previousValue, currentValue) =>
previousValue + (currentValue.title as string).length + (currentValue.icon ? 2 : 0),
0,
) *
fontSize +
columnPadding * 2 +
(actionColumn.length - 1) * gutter,
cell: (value: any, index: any, record: any) => {
return (
{actionColumn.map((action, i, arr) => {
const { icon, title, hidden = () => {}, onClick = () => {}, color, ...restProps } = action;
const isHidden = hidden({ value, index, record });
const lastOne = arr.length - 1 === i;
return (
);
})}
);
},
},
]
: [...columns];
// 获取表格数据
let getTableList;
if (!!getListHandle) {
getTableList = getListHandle;
} else if (!!getListUri) {
getTableList = getTableDataFunc(getListUri);
} else {
getTableList = () => {};
}
const field = Field.useField([]);
const useTable = useFusionTable(getTableList, { field });
const { paginationProps, tableProps, search, refresh, run, runAsync } = useTable;
TablePro.onRefresh = refresh;
TablePro.run = run;
TablePro.runAsync = runAsync;
const { submit, reset } = search;
const SimpleTable = () => {
return (
);
};
if (simple) {
return (
);
}
return (
{renderFilterBar && (
)}
`共${t}项,当前显示${range[0]}-${range[1]}项`}
pageSizeSelector={'filter'}
pageSizePosition={'end'}
pageSizeList={[10, 20, 50]}
{...paginationProps}
/>
);
};
TablePro.Column = Table.Column;
export default TablePro;