Commit c065213b authored by 张烨's avatar 张烨

feat: auto fix on save

parent 35381dc1
{
"editor.codeActionsOnSave": {
"source.fixAll": true
}
}
\ No newline at end of file
// eslint-disable-next-line button-has-type
import React, { useState, useEffect } from 'react';
import BaseForm from '../../BaseForm';
......
......@@ -2,10 +2,8 @@ import React, { useEffect, useState } from 'react';
// import OperationBar from './OperationBar'
import { Table } from 'antd';
import { Resizable } from 'react-resizable';
import SearchBar from './SearchBar'
import SearchBar from './SearchBar';
import styles from './baseTable.less';
const ResizableTitle = props => {
const { onResize, width, ...restProps } = props;
......@@ -34,17 +32,17 @@ const ResizableTitle = props => {
};
// 表格头变形的hook
const useResizableClumns = (clumns) => {
const useResizableClumns = clumns => {
const [clmns, setClmns] = useState(clumns);
if (!clumns) return null;
const [clmns, setClmns] = useState(clumns)
const handleResize = (index) => (e, { size }) => {
const nextClmns = [...clmns]
const handleResize = index => (e, { size }) => {
const nextClmns = [...clmns];
nextClmns[index] = {
...nextClmns[index],
width: size.width
}
setClmns(nextClmns)
}
width: size.width,
};
setClmns(nextClmns);
};
return clmns.map((col, index) => ({
...col,
onHeaderCell: column => ({
......@@ -52,84 +50,94 @@ const useResizableClumns = (clumns) => {
onResize: handleResize(index),
}),
}));
}
};
// 渲染列表组件
const renderComponents = (componentOption = {}) => {
const defaultComponents = {
header: {
cell: ResizableTitle,
}
}
},
};
return {
...defaultComponents,
...componentOption,
}
}
};
};
// 过滤列表数据
const filterData = (datas, searches, searchVal) => {
return datas.filter(data => {
return searches.some(s => s.filter(data[s.dataIndex], searchVal[s.dataIndex]))
})
}
const filterData = (datas, searches, searchVal) =>
datas.filter(data =>
searches.some(s => s.filter(data[s.dataIndex], searchVal[s.dataIndex])),
);
const BaseTable = (props) => {
const { searchBar, // 筛选栏属性{list: [{filter: () => boolean, key, dataIndex, label, type, initialValue?, options?}]}
operation, // 操作栏属性,可选
dataSource, // 数据源,可选
columns, // 列,必填
resizable, // 列宽是否可调整,可选
components, // 自定义table渲染,同antd/table, 可选
onRequest, // 请求数据接口,非必须,需返回Promise<{data, pagenation}>,可选,与dataSource冲突,优先onRequest
onSearchCommit, // 筛选回调, 可选
const BaseTable = props => {
const {
searchBar, // 筛选栏属性{list: [{filter: () => boolean, key, dataIndex, label, type, initialValue?, options?}]}
operation, // 操作栏属性,可选
dataSource, // 数据源,可选
columns, // 列,必填
resizable, // 列宽是否可调整,可选
components, // 自定义table渲染,同antd/table, 可选
onRequest, // 请求数据接口,非必须,需返回Promise<{data, pagenation}>,可选,与dataSource冲突,优先onRequest
onSearchCommit, // 筛选回调, 可选
// onPageChange, // 翻页时回调
...restProps } = props
const resizableClumns = resizable ? useResizableClumns(columns) : columns
const [dataList, setDataList] = useState(dataSource || [])
const [pagenation, setPageNation] = useState({})
const [loading, setLoading] = useState(false)
const [seachVal, setSearchVal] = useState({})
...restProps
} = props;
let resizableClumns = useResizableClumns(columns);
resizableClumns = resizable ? resizableClumns : columns;
const [dataList, setDataList] = useState(dataSource || []);
const [pagenation, setPageNation] = useState({});
const [loading, setLoading] = useState(false);
const [seachVal, setSearchVal] = useState({});
const fetch = (page, search) => {
search = search || seachVal
search = search || seachVal;
if (onRequest) {
setLoading(true)
setLoading(true);
onRequest(page || pagenation, search).then(res => {
console.log(res)
setDataList(searchBar ? filterData(res.data, searchBar.list, search) : res.data)
setPageNation(res.pagenation)
setLoading(false)
})
console.log(res);
setDataList(
searchBar ? filterData(res.data, searchBar.list, search) : res.data,
);
setPageNation(res.pagenation);
setLoading(false);
});
} else {
setDataList(searchBar ? filterData(dataSource, searchBar.list, search) : dataSource)
setPageNation(page)
setDataList(
searchBar ? filterData(dataSource, searchBar.list, search) : dataSource,
);
setPageNation(page);
}
}
};
const handleChange = (page) => {
fetch(page)
}
const handleChange = page => {
fetch(page);
};
const handleSearchCommit = (value) => {
console.log(value)
}
const handleSearchCommit = value => {
console.log(value);
};
useEffect(() => {
fetch(pagenation)
}, [dataSource])
fetch(pagenation);
}, [dataSource]);
return (
<div className={styles.BaseTable}>
{searchBar?.list?.length > 0 && (
<SearchBar {...searchBar} onCommit={handleSearchCommit} />
)}
<Table
dataSource={dataList}
loading={loading}
components={renderComponents(components)}
columns={resizableClumns}
onChange={handleChange}
{...restProps}
/>
</div>
);
};
return <div className={styles.BaseTable}>
{searchBar?.list?.length > 0 && <SearchBar {...searchBar} onCommit={handleSearchCommit} />}
<Table dataSource={dataList}
loading={loading}
components={renderComponents(components)}
columns={resizableClumns}
onChange={handleChange}
{...restProps}
/>
</div>
}
export default BaseTable
\ No newline at end of file
export default BaseTable;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment