Skip to content

Commit

Permalink
Merge pull request #765 from thundersdata-frontend/feat/lego
Browse files Browse the repository at this point in the history
feat: table组件优化
  • Loading branch information
chj-damon authored Nov 20, 2023
2 parents 69249b8 + ae43aa0 commit 652ce70
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-socks-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@td-design/lego': patch
---

feat: lego的table组件的优化
15 changes: 8 additions & 7 deletions packages/lego/example/TableDemo/demo1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ const columns = [

export default () => {
return (
<Table
data={data}
columns={columns}
height={120}
pageSize={4}
autoplay={true}
/>
<div style={{ width: '100%', height: 200 }}>
<Table
data={data}
columns={columns}
pageSize={4}
autoplay={true}
/>
</div>
);
};
7 changes: 6 additions & 1 deletion packages/lego/src/table/index.less
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
.td-lego-table-container {
box-sizing: border-box;
width: 100%;
height: calc(~'100% - 36px');
height: 100%;
padding: 8px;

.table-view {
padding: 3px;
background-size: 100% 100%;
height: 100%;

.td-lego-table-header {
width: 100%;
height: 36px;
color: #fff;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
Expand All @@ -30,6 +32,9 @@
flex-direction: row;
align-items: center;
justify-content: space-between;
/** 100% */
height: 100%;

.text {
text-align: center;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/lego/src/table/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ group:
| --- | --- | --- | --- | --- |
| columns | `true` | 列数据 | `Column[]` | |
| data | `true` | 数据源 | `T[]` | |
| height | `true` | 除了表头的表格内容高度 | `number` | |
| height | `false` | 除了表头的表格内容高度, 如果不填默认撑满父组件 | `number` | `calc(100% - 36px)` |
| pageSize | `true` | 每屏显示几条数据 | `number` | |
| speed | `false` | 速度(ms) | `number` | |
| autoplay | `false` | 自动轮播, 注:pageSize必须小于等于数量的一半才能自动轮播 | `boolean` | |
Expand Down
23 changes: 13 additions & 10 deletions packages/lego/src/table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type CustomTableProps<T> = {
/** 数据源 */
data: T[];
/** 容器高度 */
height: number;
height?: number;
/** 每屏显示几条数据 */
pageSize: number;
/** 速度(ms) */
Expand All @@ -48,7 +48,7 @@ export type CustomTableProps<T> = {
function Table<T extends Record<string, any>>({
columns = [],
data = [],
height,
height = 0,
pageSize = 1,
speed = 1000,
autoplay = true,
Expand All @@ -68,18 +68,21 @@ function Table<T extends Record<string, any>>({
};

const slidesPerViewParams = pageSize > data.length ? data.length : pageSize;
const lineHeight = height / slidesPerViewParams;

const bgHeight: string = height
? `${(height / slidesPerViewParams) * 2}px`
: `calc(100% / ${slidesPerViewParams / 2})`;

return (
<div className="td-lego-table-container">
<div style={{ width: '100%' }}>
<div style={{ width: '100%', height: '100%' }}>
<div className="table-view">
<div
className={classnames('td-lego-table-header', headerClass)}
style={{ backgroundColor: colors?.[2] ?? colors?.[1] }}
>
{!isEmpty(columns) && (
<div className="td-lego-table-content" style={{ height: lineHeight }}>
<div className="td-lego-table-content">
{columns.map(item => {
return (
<div
Expand Down Expand Up @@ -107,15 +110,15 @@ function Table<T extends Record<string, any>>({
style={{
// 如果直接设置background,切换colors会报错(background和backgroundSize属性冲突)
backgroundImage: `linear-gradient( ${colors[0]} 50%, ${colors[1]} 0)`,
backgroundSize: `100% ${2 * lineHeight}px`,
height,
backgroundSize: `100% ${bgHeight}`,
height: `${height === 0 ? 'calc(100% - 36px)' : height}`,
overflow: autoplay ? 'hidden' : 'auto',
}}
>
{!isEmpty(data) && !isEmpty(columns) && (
<Container
{...{
height,
height: `${height ? height + 'px' : '100%'}`,
pageSize: slidesPerViewParams,
speed,
autoplay,
Expand All @@ -129,7 +132,6 @@ function Table<T extends Record<string, any>>({
style={{
...theme.typography[inModal ? 'p0' : 'p2'],
lineHeight: inModal ? '25px' : '19px',
height: lineHeight,
}}
>
{columns.map(term => {
Expand All @@ -142,6 +144,7 @@ function Table<T extends Record<string, any>>({
width: term.width || `${100 / columns?.length}%`,
flex: term.flex,
}),
alignItems: 'center',
textAlign: term.textAlign,
}}
>
Expand Down Expand Up @@ -170,7 +173,7 @@ const Container = memo(
speed,
children,
}: PropsWithChildren<{
height: number;
height: string | number;
pageSize: number;
autoplay: boolean;
speed: number;
Expand Down

0 comments on commit 652ce70

Please sign in to comment.