-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlist.tsx
More file actions
174 lines (157 loc) · 5.27 KB
/
list.tsx
File metadata and controls
174 lines (157 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import React, { useContext, useState } from 'react';
import classNames from 'classnames';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
import { useVirtualScroll } from '../_utils/use-virtual-scroll';
import Pagination from '../pagination';
import { ListProps } from './types';
const ITEM_HEIGHT_MAP = { sm: 41, md: 49, lg: 57 } as const;
const List = React.forwardRef<HTMLDivElement, ListProps>((props, ref) => {
const {
dataSource = [],
renderItem,
header,
footer,
loading = false,
bordered = false,
split = true,
size,
grid,
locale,
virtual = false,
height,
itemHeight: itemHeightProp,
pagination,
prefixCls: customisedCls,
className,
style,
children,
...otherProps
} = props;
const configContext = useContext(ConfigContext);
const prefixCls = getPrefixCls('list', configContext.prefixCls, customisedCls);
const listSize = size || configContext.componentSize || 'md';
if (virtual && height == null) {
console.warn('[tiny-design: List] `height` is required when `virtual` is enabled.');
}
if (virtual && grid) {
console.warn('[tiny-design: List] `virtual` is not supported with `grid` mode. Falling back to normal rendering.');
}
const isVirtual = virtual && height != null && !grid;
const [currentPage, setCurrentPage] = useState(1);
const pageSize = pagination && pagination.pageSize ? pagination.pageSize : 10;
const itemHeight = itemHeightProp ?? ITEM_HEIGHT_MAP[listSize] ?? ITEM_HEIGHT_MAP.md;
const { visibleRange, totalHeight, offsetY, onScroll } = useVirtualScroll({
itemCount: dataSource.length,
itemHeight,
containerHeight: height ?? 0,
});
const cls = classNames(prefixCls, className, {
[`${prefixCls}_${listSize}`]: listSize,
[`${prefixCls}_bordered`]: bordered,
[`${prefixCls}_split`]: split,
[`${prefixCls}_loading`]: loading,
[`${prefixCls}_grid`]: grid,
});
const paginatedData = () => {
if (!pagination) return dataSource;
const page = pagination.current ?? currentPage;
const start = (page - 1) * pageSize;
return dataSource.slice(start, start + pageSize);
};
const handlePageChange = (page: number) => {
setCurrentPage(page);
pagination && pagination.onChange?.(page, pageSize);
};
const renderItems = () => {
if (isVirtual) {
if (dataSource.length === 0) {
return (
<div className={`${prefixCls}__empty`}>
{locale?.emptyText ?? 'No Data'}
</div>
);
}
if (renderItem) {
const [start, end] = visibleRange;
const visibleItems = dataSource.slice(start, end + 1).map((item, i) => (
<React.Fragment key={start + i}>{renderItem(item, start + i)}</React.Fragment>
));
return (
<div style={{ height: totalHeight, position: 'relative' }}>
<div style={{ position: 'absolute', top: 0, left: 0, right: 0, transform: `translateY(${offsetY}px)` }}>
{visibleItems}
</div>
</div>
);
}
return children;
}
const items = paginatedData();
if (items.length === 0 && !children) {
return (
<div className={`${prefixCls}__empty`}>
{locale?.emptyText ?? 'No Data'}
</div>
);
}
if (renderItem) {
const rendered = items.map((item, index) => (
<React.Fragment key={index}>{renderItem(item, index)}</React.Fragment>
));
if (grid) {
return (
<div
className={`${prefixCls}__grid`}
style={{
display: 'grid',
gridTemplateColumns: `repeat(${grid.column || 3}, 1fr)`,
gap: grid.gutter ? `${grid.gutter}px` : undefined,
}}
>
{rendered}
</div>
);
}
return rendered;
}
return children;
};
const showPagination = pagination && !isVirtual;
const paginationConfig = pagination && typeof pagination === 'object' ? pagination : undefined;
const totalItems = paginationConfig?.total ?? dataSource.length;
const activePage = paginationConfig?.current ?? currentPage;
const bodyCls = classNames(`${prefixCls}__body`, {
[`${prefixCls}__body_virtual`]: isVirtual,
});
const bodyStyle: React.CSSProperties | undefined = isVirtual
? { height, overflowY: 'auto' }
: undefined;
return (
<div {...otherProps} ref={ref} className={cls} style={style}>
{header && <div className={`${prefixCls}__header`}>{header}</div>}
<div className={bodyCls} style={bodyStyle} onScroll={isVirtual ? onScroll : undefined}>
{loading ? (
<div className={`${prefixCls}__loading`}>Loading...</div>
) : (
renderItems()
)}
</div>
{footer && <div className={`${prefixCls}__footer`}>{footer}</div>}
{showPagination && (
<Pagination
current={activePage}
total={totalItems}
pageSize={pageSize}
align={paginationConfig?.align ?? 'right'}
size={paginationConfig?.size}
disabled={paginationConfig?.disabled}
onChange={(page) => handlePageChange(page)}
style={{ padding: 16 }}
/>
)}
</div>
);
});
List.displayName = 'List';
export default List;