Skip to content

Commit d20b5e8

Browse files
authoredMar 18, 2022
feat(Table): Table 添加 scroll 属性,解决表格冗余 div fix #687 (#690)
1 parent 1982a14 commit d20b5e8

File tree

3 files changed

+88
-9
lines changed

3 files changed

+88
-9
lines changed
 

‎packages/react-table/README.md

+61-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ const Demo = () => (
6565
);
6666
ReactDOM.render(<Demo />, _mount_);
6767
```
68-
6968
### 表头分组
7069

7170
表头分组通过 `columns` 数组中对象的 `children` 来实现,以渲染分组表头。。
@@ -814,6 +813,66 @@ const Demo = () => {
814813
ReactDOM.render(<Demo />, _mount_);
815814
```
816815

816+
### 表格列过宽导致 footer 滑动出表格底部
817+
818+
使用 scroll 属性给表格设置宽(x)或高(y)即可
819+
820+
<!--rehype:bgWhite=true&codeSandbox=true&codePen=true-->
821+
```jsx
822+
import ReactDOM from 'react-dom';
823+
import { Table, Button } from 'uiw';
824+
825+
const columns = [
826+
{
827+
// title: '姓名',
828+
ellipsis: true,
829+
width: 1000,
830+
title: ({ key }) => {
831+
return (
832+
<span>字段: {key}</span>
833+
)
834+
},
835+
key: 'name',
836+
}, {
837+
title: '年龄',
838+
style: { color: 'red' },
839+
key: 'age',
840+
}, {
841+
title: '地址',
842+
key: 'info',
843+
}, {
844+
title: '操作',
845+
key: 'edit',
846+
width: 98,
847+
render: (text, key, rowData, rowNumber, columnNumber) => (
848+
<div>
849+
<Button size="small" type="danger">删除</Button>
850+
<Button size="small" type="success">修改</Button>
851+
</div>
852+
),
853+
},
854+
];
855+
const dataSource = [
856+
{ name: '邓紫棋', age: '12', info: '又名G.E.M.,原名邓诗颖,1991年8月16日生于中国上海,中国香港创作型女歌手。', edit: '' },
857+
{ name: '李易峰', age: '32', info: '1987年5月4日出生于四川成都,中国内地男演员、流行乐歌手、影视制片人', edit: '' },
858+
{ name: '范冰冰', age: '23', info: '1981年9月16日出生于山东青岛,中国影视女演员、制片人、流行乐女歌手', edit: '' },
859+
{ name: '杨幂', age: '34', info: '1986年9月12日出生于北京市,中国内地影视女演员、流行乐歌手、影视制片人。', edit: '' },
860+
{ name: 'Angelababy', age: '54', info: '1989年2月28日出生于上海市,华语影视女演员、时尚模特。', edit: '' },
861+
{ name: '唐嫣', age: '12', info: '1983年12月6日出生于上海市,毕业于中央戏剧学院表演系本科班', edit: '' },
862+
{ name: '吴亦凡', age: '4', info: '1990年11月06日出生于广东省广州市,华语影视男演员、流行乐歌手。', edit: '' },
863+
];
864+
const Demo = () => (
865+
<div>
866+
<Table
867+
scroll={{x: 1800, y: 100}}
868+
footer={<div style={{height: 20, }}>这个是footer</div>}
869+
columns={columns} data={dataSource}
870+
/>
871+
</div>
872+
);
873+
ReactDOM.render(<Demo />, _mount_);
874+
```
875+
817876
## Props
818877

819878
### Table
@@ -845,6 +904,7 @@ ReactDOM.render(<Demo />, _mount_);
845904
| render | 生成复杂数据的渲染函数,参数分别为当前行的值,当前值的 `key`,行索引数据,当前行号,当前列号。| `Function(text, key, rowData, rowNumber, columnNumber)` | - |
846905
| align | 设置列的对齐方式 | "left"|"center"|"right" | - |
847906
| className | 列样式类名 | string | - |
907+
| scroll | 表格是否可滚动,也可以指定滚动区域的宽、高 | { x?: React.CSSProperties['width'], y?: React.CSSProperties['height'] } | - |
848908

849909
### expandable
850910

‎packages/react-table/src/TableTr.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default function TableTr<T extends { [key: string]: any }>(props: TableTr
7575
children: trData[keyName.key!],
7676
};
7777
if (render[keyName.key!]) {
78-
const child = render[keyName.key!](trData[keyName.key!], keyName.key!, trData, rowNum, colNum);
78+
const child = render[keyName.key!](trData[keyName.key!], keyName.key, trData, rowNum, colNum);
7979
if (React.isValidElement(child)) {
8080
objs.children = child;
8181
} else {
@@ -106,7 +106,7 @@ export default function TableTr<T extends { [key: string]: any }>(props: TableTr
106106
{...objs}
107107
key={colNum}
108108
// style={keyName?.style}
109-
className={`${objs.className} ${prefixCls}-tr-children-${keyName?.align || 'left'} ${
109+
className={`${objs.className || ''} ${prefixCls}-tr-children-${keyName.align || 'left'} ${
110110
keyName.className || ''
111111
}`}
112112
onClick={(evn) => onCell(trData, { rowNum, colNum, keyName: keyName.key! }, evn)}

‎packages/react-table/src/index.tsx

+25-6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export interface TableProps<T extends { [key: string]: V } = any, V = any> exten
6666
) => void;
6767
expandable?: ExpandableType<T>;
6868
rowKey?: keyof T;
69+
scroll?: { x?: React.CSSProperties['width']; y?: React.CSSProperties['height'] };
6970
}
7071

7172
export interface ICellOptions {
@@ -88,6 +89,7 @@ export default function Table<T extends { [key: string]: V }, V>(props: TablePro
8889
children,
8990
expandable,
9091
rowKey,
92+
scroll,
9193
...other
9294
} = props;
9395
const [expandIndex, setExpandIndex] = useState<Array<T[keyof T] | number>>([]);
@@ -187,14 +189,31 @@ export default function Table<T extends { [key: string]: V }, V>(props: TablePro
187189
selfColumns,
188190
};
189191
}, [columns, expandIndex]);
190-
192+
const style: { table: React.CSSProperties; div: React.CSSProperties } = useMemo(() => {
193+
const style: { table: React.CSSProperties; div: React.CSSProperties } = {
194+
table: {},
195+
div: {},
196+
};
197+
if (scroll) {
198+
if (scroll.x !== undefined) {
199+
style.table.minWidth = '100%';
200+
style.table.width = scroll.x;
201+
style.div.overflowX = 'auto';
202+
style.div.overflowY = 'hidden';
203+
}
204+
if (scroll.y !== undefined) {
205+
style.div.maxHeight = scroll.y;
206+
style.div.overflowY = 'scroll';
207+
}
208+
}
209+
return style;
210+
}, [scroll]);
191211
const cls = [prefixCls, className, bordered ? `${prefixCls}-bordered` : null].filter(Boolean).join(' ').trim();
192212
const { header, render, ellipsis } = getLevelItems(self.selfColumns);
193-
194213
return (
195-
<div>
196-
<div style={{ overflowY: 'scroll' }} className={cls} {...other}>
197-
<table style={ellipsis ? { tableLayout: 'fixed' } : {}}>
214+
<React.Fragment>
215+
<div className={cls} {...other} style={{ ...other.style, ...style.div }}>
216+
<table style={{ tableLayout: ellipsis ? 'fixed' : 'auto', ...style.table }}>
198217
{title && <caption>{title}</caption>}
199218
{columns && columns.length > 0 && <Thead onCellHead={onCellHead} data={header} />}
200219
{data && data.length > 0 && (
@@ -227,6 +246,6 @@ export default function Table<T extends { [key: string]: V }, V>(props: TablePro
227246
</table>
228247
</div>
229248
{footer && <div className={`${prefixCls}-footer`}>{footer}</div>}
230-
</div>
249+
</React.Fragment>
231250
);
232251
}

0 commit comments

Comments
 (0)
Please sign in to comment.