This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 306
/
table.tsx
649 lines (573 loc) · 20.9 KB
/
table.tsx
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
import cx from 'classnames'
import React, { CSSProperties, ReactNode } from 'react'
import { BehaviorSubject, combineLatest, noop, Subscription } from 'rxjs'
import * as op from 'rxjs/operators'
import { ArtColumn } from '../interfaces'
import { calculateRenderInfo } from './calculations'
import { EmptyHtmlTable } from './empty'
import TableHeader from './header'
import { getRichVisibleRectsStream } from './helpers/getRichVisibleRectsStream'
import { getFullRenderRange, makeRowHeightManager } from './helpers/rowHeightManager'
import { TableDOMHelper } from './helpers/TableDOMUtils'
import { HtmlTable } from './html-table'
import { RenderInfo, ResolvedUseVirtual, VerticalRenderRange, VirtualEnum } from './interfaces'
import Loading, { LoadingContentWrapperProps } from './loading'
import { BaseTableCSSVariables, Classes, LOCK_SHADOW_PADDING, StyledArtTableWrapper } from './styles'
import {
getScrollbarSize,
OVERSCAN_SIZE,
shallowEqual,
STYLED_REF_PROP,
sum,
syncScrollLeft,
throttledWindowResize$,
} from './utils'
let emptyContentDeprecatedWarned = false
function warnEmptyContentIsDeprecated() {
if (!emptyContentDeprecatedWarned) {
emptyContentDeprecatedWarned = true
console.warn(
'[ali-react-table] BaseTable props.emptyContent 已经过时,请使用 props.components.EmptyContent 来自定义数据为空时的表格表现',
)
}
}
let flowRootDeprecatedWarned = false
function warnFlowRootIsDeprecated() {
if (!flowRootDeprecatedWarned) {
flowRootDeprecatedWarned = true
console.warn('[ali-react-table] BaseTable v2.4 版本之后已经不再需要指定 flowRoot')
}
}
export type PrimaryKey = string | ((row: any) => string)
export interface BaseTableProps {
/** 主键 */
primaryKey?: PrimaryKey
/** 表格展示的数据源 */
dataSource: any[]
/** 表格页脚数据源 */
footerDataSource?: any[]
/** 表格的列配置 */
columns: ArtColumn[]
/** 是否开启虚拟滚动 */
useVirtual?: VirtualEnum | { horizontal?: VirtualEnum; vertical?: VirtualEnum; header?: VirtualEnum }
/** 虚拟滚动开启情况下,表格中每一行的预估高度 */
estimatedRowHeight?: number
/** @deprecated 表格头部是否置顶,默认为 true。请使用 isStickyHeader 代替 */
isStickyHead?: boolean
/** 表格头部是否置顶,默认为 true */
isStickyHeader?: boolean
/** 表格置顶后,距离顶部的距离 */
stickyTop?: number
/** 表格页脚是否置底,默认为 true */
isStickyFooter?: boolean
/** 表格页脚置底后,距离底部的距离 */
stickyBottom?: number
/** 自定义类名 */
className?: string
/** 自定义内联样式 */
style?: CSSProperties & BaseTableCSSVariables
/** 表格是否具有头部 */
hasHeader?: boolean
/** 表格是否具有横向的粘性滚动条 */
hasStickyScroll?: boolean
/** 横向粘性滚动条高度 */
stickyScrollHeight?: 'auto' | number
/** 使用来自外层 div 的边框代替单元格的外边框 */
useOuterBorder?: boolean
/** 表格是否在加载中 */
isLoading?: boolean
/** 数据为空时,单元格的高度 */
emptyCellHeight?: number
/** @deprecated 数据为空时,表格的展示内容。请使用 components.EmptyContent 代替 */
emptyContent?: ReactNode
/** 覆盖表格内部用到的组件 */
components?: {
/** 表格加载时,表格内容的父组件 */
LoadingContentWrapper?: React.ComponentType<LoadingContentWrapperProps>
/** 表格加载时的加载图标 */
LoadingIcon?: React.ComponentType
/** 数据为空时,表格的展示内容。*/
EmptyContent?: React.ComponentType
/** 覆盖内部渲染 tbody>tr 元素的组件,一般用于在 tr 上设置 context */
Row?: React.ComponentType<{ row: any; rowIndex: number; trProps: unknown }>
/** 覆盖内部渲染 tbody>td 元素的组件,一般用于在 td 上设置 context */
Cell?: React.ComponentType<{ row: any; rowIndex: number; colIndex: number; tdProps: unknown; column: ArtColumn }>
/** 覆盖内部渲染 tbody 元素的组件 */
TableBody?: React.ComponentType<{ tbodyProps: unknown }>
}
/** 列的默认宽度 */
defaultColumnWidth?: number
/**
* @deprecated
* flowRoot 在表格 v2.4 后不再需要提供,请移除该属性
* */
flowRoot?: never
/** 虚拟滚动调试标签,用于表格内部调试使用 */
virtualDebugLabel?: string
getRowProps?(row: any, rowIndex: number): React.HTMLAttributes<HTMLTableRowElement>
}
interface BaseTableState {
/** 是否要展示自定义滚动条(stickyScroll) */
hasScroll: boolean
/** 是否需要渲染 lock sections
* 当表格较宽时,所有的列都能被完整的渲染,此时不需要渲染 lock sections
* 只有当「整个表格的宽度」小于「每一列渲染宽度之和」时,lock sections 才需要被渲染 */
needRenderLock: boolean
/** 纵向虚拟滚动偏移量 */
offsetY: number
/** 纵向虚拟滚动 最大渲染尺寸 */
maxRenderHeight: number
/** 横向虚拟滚动偏移量 */
offsetX: number
/** 横向虚拟滚动 最大渲染尺寸 */
maxRenderWidth: number
}
export class BaseTable extends React.Component<BaseTableProps, BaseTableState> {
static defaultProps = {
hasHeader: true,
isStickyHeader: true,
stickyTop: 0,
footerDataSource: [] as any[],
isStickyFooter: true,
stickyBottom: 0,
hasStickyScroll: true,
stickyScrollHeight: 'auto',
useVirtual: 'auto',
estimatedRowHeight: 48,
isLoading: false,
components: {},
getRowProps: noop,
dataSource: [] as any[],
}
private rowHeightManager = makeRowHeightManager(this.props.dataSource.length, this.props.estimatedRowHeight)
private artTableWrapperRef = React.createRef<HTMLDivElement>()
private domHelper: TableDOMHelper
private rootSubscription = new Subscription()
// 最近一次渲染的计算结果缓存
private lastInfo: RenderInfo
private props$: BehaviorSubject<BaseTableProps>
/** @deprecated BaseTable.getDoms() 已经过时,请勿调用 */
getDoms() {
console.warn('[ali-react-table] BaseTable.getDoms() 已经过时')
return this.domHelper
}
constructor(props: Readonly<BaseTableProps>) {
super(props)
this.state = {
hasScroll: true,
needRenderLock: true,
offsetY: 0,
offsetX: 0,
// 因为 ResizeObserver 在一开始总是会调用一次所提供的回调函数
// 故这里为 maxRenderHeight/maxRenderWidth 设置一个默认值即可(因为这两个默认值很快就会被覆盖)
// https://stackoverflow.com/questions/60026223/does-resizeobserver-invokes-initially-on-page-load
maxRenderHeight: 600,
maxRenderWidth: 800,
}
}
/** 自定义滚动条宽度为table宽度,使滚动条滑块宽度相同 */
private updateStickyScroll() {
const { stickyScroll, artTable, stickyScrollItem } = this.domHelper
if (!artTable) {
return
}
const tableBodyHtmlTable = this.domHelper.getTableBodyHtmlTable()
const innerTableWidth = tableBodyHtmlTable.offsetWidth
const artTableWidth = artTable.offsetWidth
const stickyScrollHeightProp = this.props.stickyScrollHeight
const stickyScrollHeight = stickyScrollHeightProp === 'auto' ? getScrollbarSize().height : stickyScrollHeightProp
stickyScroll.style.marginTop = `-${stickyScrollHeight + 1}px`
if (artTableWidth >= innerTableWidth) {
if (this.state.hasScroll) {
this.setState({ hasScroll: false })
}
} else {
if (!this.state.hasScroll && stickyScrollHeight > 5) {
// 考虑下mac下面隐藏滚动条的情况
this.setState({ hasScroll: true })
}
}
// 设置子节点宽度
stickyScrollItem.style.width = `${innerTableWidth}px`
}
private renderTableHeader(info: RenderInfo) {
const { stickyTop, hasHeader } = this.props
return (
<div
className={cx(Classes.tableHeader, 'no-scrollbar')}
style={{
top: stickyTop === 0 ? undefined : stickyTop,
display: hasHeader ? undefined : 'none',
}}
>
<TableHeader info={info} />
</div>
)
}
private updateOffsetX(nextOffsetX: number) {
if (this.lastInfo.useVirtual.horizontal) {
if (Math.abs(nextOffsetX - this.state.offsetX) >= OVERSCAN_SIZE / 2) {
this.setState({ offsetX: nextOffsetX })
}
}
}
private syncHorizontalScrollFromTableBody() {
this.syncHorizontalScroll(this.domHelper.tableBody.scrollLeft)
}
/** 同步横向滚动偏移量 */
private syncHorizontalScroll(x: number) {
this.updateOffsetX(x)
const { tableBody } = this.domHelper
const { flat } = this.lastInfo
const leftLockShadow = this.domHelper.getLeftLockShadow()
if (leftLockShadow) {
const shouldShowLeftLockShadow = flat.left.length > 0 && this.state.needRenderLock && x > 0
if (shouldShowLeftLockShadow) {
leftLockShadow.classList.add('show-shadow')
} else {
leftLockShadow.classList.remove('show-shadow')
}
}
const rightLockShadow = this.domHelper.getRightLockShadow()
if (rightLockShadow) {
const shouldShowRightLockShadow =
flat.right.length > 0 && this.state.needRenderLock && x < tableBody.scrollWidth - tableBody.clientWidth
if (shouldShowRightLockShadow) {
rightLockShadow.classList.add('show-shadow')
} else {
rightLockShadow.classList.remove('show-shadow')
}
}
}
getVerticalRenderRange(useVirtual: ResolvedUseVirtual): VerticalRenderRange {
const { dataSource } = this.props
const { offsetY, maxRenderHeight } = this.state
const rowCount = dataSource.length
if (useVirtual.vertical) {
return this.rowHeightManager.getRenderRange(offsetY, maxRenderHeight, rowCount)
} else {
return getFullRenderRange(rowCount)
}
}
private renderTableBody(info: RenderInfo) {
const { dataSource, getRowProps, primaryKey, isLoading, emptyCellHeight, footerDataSource, components } = this.props
const tableBodyClassName = cx(Classes.tableBody, Classes.horizontalScrollContainer, {
'no-scrollbar': footerDataSource.length > 0,
})
if (dataSource.length === 0) {
const { components, emptyContent } = this.props
let EmptyContent = components.EmptyContent
if (EmptyContent == null && emptyContent != null) {
warnEmptyContentIsDeprecated()
EmptyContent = (() => emptyContent) as unknown as React.ComponentType
}
return (
<div className={tableBodyClassName}>
<EmptyHtmlTable
descriptors={info.visible}
isLoading={isLoading}
EmptyContent={EmptyContent}
emptyCellHeight={emptyCellHeight}
/>
</div>
)
}
const { topIndex, bottomBlank, topBlank, bottomIndex } = info.verticalRenderRange
return (
<div className={tableBodyClassName}>
{topBlank > 0 && (
<div key="top-blank" className={cx(Classes.virtualBlank, 'top')} style={{ height: topBlank }} />
)}
<HtmlTable
components={components}
tbodyHtmlTag="tbody"
getRowProps={getRowProps}
primaryKey={primaryKey}
data={dataSource.slice(topIndex, bottomIndex)}
horizontalRenderInfo={info}
verticalRenderInfo={{
first: 0,
offset: topIndex,
limit: bottomIndex,
last: dataSource.length - 1,
}}
/>
{bottomBlank > 0 && (
<div key="bottom-blank" className={cx(Classes.virtualBlank, 'bottom')} style={{ height: bottomBlank }} />
)}
</div>
)
}
private renderTableFooter(info: RenderInfo) {
const { footerDataSource = [], getRowProps, primaryKey, stickyBottom, components } = this.props
return (
<div
className={cx(Classes.tableFooter, Classes.horizontalScrollContainer)}
style={{ bottom: stickyBottom === 0 ? undefined : stickyBottom }}
>
<HtmlTable
components={components}
tbodyHtmlTag="tfoot"
data={footerDataSource}
horizontalRenderInfo={info}
getRowProps={getRowProps}
primaryKey={primaryKey}
verticalRenderInfo={{
offset: 0,
first: 0,
last: footerDataSource.length - 1,
limit: Infinity,
}}
/>
</div>
)
}
private renderLockShadows(info: RenderInfo) {
return (
<>
<div
className={Classes.lockShadowMask}
style={{ left: 0, width: info.leftLockTotalWidth + LOCK_SHADOW_PADDING }}
>
<div className={cx(Classes.lockShadow, Classes.leftLockShadow)} />
</div>
<div
className={Classes.lockShadowMask}
style={{ right: 0, width: info.rightLockTotalWidth + LOCK_SHADOW_PADDING }}
>
<div className={cx(Classes.lockShadow, Classes.rightLockShadow)} />
</div>
</>
)
}
private renderStickyScroll(info: RenderInfo) {
const { hasStickyScroll, stickyBottom } = this.props
const { hasScroll } = this.state
return (
<div
className={cx(Classes.stickyScroll, Classes.horizontalScrollContainer)}
style={{
display: hasStickyScroll && hasScroll ? 'block' : 'none',
bottom: stickyBottom,
}}
>
<div className={Classes.stickyScrollItem} />
</div>
)
}
render() {
const info = calculateRenderInfo(this)
this.lastInfo = info
const {
dataSource,
className,
style,
hasHeader,
useOuterBorder,
isStickyHead,
isStickyHeader,
isStickyFooter,
isLoading,
footerDataSource,
components,
flowRoot,
} = this.props
if (flowRoot != null) {
warnFlowRootIsDeprecated()
}
const artTableWrapperClassName = cx(
Classes.artTableWrapper,
{
'use-outer-border': useOuterBorder,
empty: dataSource.length === 0,
lock: info.hasLockColumn,
'has-header': hasHeader,
'sticky-header': isStickyHeader ?? isStickyHead,
'has-footer': footerDataSource.length > 0,
'sticky-footer': isStickyFooter,
},
className,
)
const artTableWrapperProps = {
className: artTableWrapperClassName,
style,
[STYLED_REF_PROP]: this.artTableWrapperRef,
}
return (
<StyledArtTableWrapper {...artTableWrapperProps}>
<Loading
visible={isLoading}
LoadingIcon={components.LoadingIcon}
LoadingContentWrapper={components.LoadingContentWrapper}
>
<div className={Classes.artTable}>
{this.renderTableHeader(info)}
{this.renderTableBody(info)}
{this.renderTableFooter(info)}
{this.renderLockShadows(info)}
</div>
{this.renderStickyScroll(info)}
</Loading>
</StyledArtTableWrapper>
)
}
componentDidMount() {
this.updateDOMHelper()
this.props$ = new BehaviorSubject(this.props)
this.initSubscriptions()
this.didMountOrUpdate()
}
componentDidUpdate(prevProps: Readonly<BaseTableProps>, prevState: Readonly<BaseTableState>) {
this.updateDOMHelper()
this.props$.next(this.props)
this.didMountOrUpdate(prevProps, prevState)
}
private didMountOrUpdate(prevProps?: Readonly<BaseTableProps>, prevState?: Readonly<BaseTableState>) {
this.syncHorizontalScrollFromTableBody()
this.updateStickyScroll()
this.adjustNeedRenderLock()
this.updateRowHeightManager()
this.updateScrollLeftWhenLayoutChanged(prevProps, prevState)
}
private updateScrollLeftWhenLayoutChanged(
prevProps?: Readonly<BaseTableProps>,
prevState?: Readonly<BaseTableState>,
) {
if (prevState != null) {
if (!prevState.hasScroll && this.state.hasScroll) {
this.domHelper.stickyScroll.scrollLeft = 0
}
}
if (prevProps != null) {
const prevHasFooter = prevProps.footerDataSource.length > 0
const currentHasFooter = this.props.footerDataSource.length > 0
if (!prevHasFooter && currentHasFooter) {
this.domHelper.tableFooter.scrollLeft = this.domHelper.tableBody.scrollLeft
}
}
}
private initSubscriptions() {
const { tableHeader, tableBody, tableFooter, stickyScroll } = this.domHelper
this.rootSubscription.add(
throttledWindowResize$.subscribe(() => {
this.updateStickyScroll()
this.adjustNeedRenderLock()
}),
)
// 滚动同步
this.rootSubscription.add(
syncScrollLeft([tableHeader, tableBody, tableFooter, stickyScroll], (scrollLeft) => {
this.syncHorizontalScroll(scrollLeft)
}),
)
const richVisibleRects$ = getRichVisibleRectsStream(
this.domHelper.artTable,
this.props$.pipe(op.skip(1), op.mapTo('structure-may-change')),
this.props.virtualDebugLabel,
).pipe(op.shareReplay())
// 每当可见部分发生变化的时候,调整 loading icon 的未知(如果 loading icon 存在的话)
this.rootSubscription.add(
combineLatest([
richVisibleRects$.pipe(
op.map((p) => p.clipRect),
op.distinctUntilChanged(shallowEqual),
),
this.props$.pipe(
op.startWith(null),
op.pairwise(),
op.filter(([prevProps, props]) => prevProps == null || (!prevProps.isLoading && props.isLoading)),
),
]).subscribe(([clipRect]) => {
const loadingIndicator = this.domHelper.getLoadingIndicator()
if (!loadingIndicator) {
return
}
const height = clipRect.bottom - clipRect.top
// fixme 这里的定位在有些特殊情况下可能会出错 see #132
loadingIndicator.style.top = `${height / 2}px`
loadingIndicator.style.marginTop = `${height / 2}px`
}),
)
// 每当可见部分发生变化的时候,如果开启了虚拟滚动,则重新触发 render
this.rootSubscription.add(
richVisibleRects$
.pipe(
op.filter(() => {
const { horizontal, vertical } = this.lastInfo.useVirtual
return horizontal || vertical
}),
op.map(({ clipRect, offsetY }) => ({
maxRenderHeight: clipRect.bottom - clipRect.top,
maxRenderWidth: clipRect.right - clipRect.left,
offsetY,
})),
op.distinctUntilChanged((x, y) => {
// 因为 overscan 的存在,滚动较小的距离时不需要触发组件重渲染
return (
Math.abs(x.maxRenderWidth - y.maxRenderWidth) < OVERSCAN_SIZE / 2 &&
Math.abs(x.maxRenderHeight - y.maxRenderHeight) < OVERSCAN_SIZE / 2 &&
Math.abs(x.offsetY - y.offsetY) < OVERSCAN_SIZE / 2
)
}),
)
.subscribe((sizeAndOffset) => {
this.setState(sizeAndOffset)
}),
)
}
componentWillUnmount() {
this.rootSubscription.unsubscribe()
}
/** 更新 DOM 节点的引用,方便其他方法直接操作 DOM */
private updateDOMHelper() {
this.domHelper = new TableDOMHelper(this.artTableWrapperRef.current)
}
private updateRowHeightManager() {
const virtualTop = this.domHelper.getVirtualTop()
const virtualTopHeight = virtualTop?.clientHeight ?? 0
let zeroHeightRowCount = 0
let maxRowIndex = -1
let maxRowBottom = -1
for (const tr of this.domHelper.getTableRows()) {
const rowIndex = Number(tr.dataset.rowindex)
if (isNaN(rowIndex)) {
continue
}
maxRowIndex = Math.max(maxRowIndex, rowIndex)
const offset = tr.offsetTop + virtualTopHeight
const size = tr.offsetHeight
if (size === 0) {
zeroHeightRowCount += 1
}
maxRowBottom = Math.max(maxRowBottom, offset + size)
this.rowHeightManager.updateRow(rowIndex, offset, size)
}
// 当 estimatedRowHeight 过大时,可能出现「渲染行数过少,无法覆盖可视范围」的情况
// 出现这种情况时,我们判断「下一次渲染能够渲染更多行」是否满足,满足的话就直接调用 forceUpdate
// zeroHeightRowCount === 0 用于确保当前没有 display=none 的情况
if (maxRowIndex !== -1 && zeroHeightRowCount === 0) {
if (maxRowBottom < this.state.offsetY + this.state.maxRenderHeight) {
const vertical = this.getVerticalRenderRange(this.lastInfo.useVirtual)
if (vertical.bottomIndex - 1 > maxRowIndex) {
this.forceUpdate()
}
}
}
}
/** 计算表格所有列的渲染宽度之和,判断表格是否需要渲染锁列 */
private adjustNeedRenderLock() {
const { needRenderLock } = this.state
const { flat, hasLockColumn } = this.lastInfo
if (hasLockColumn) {
const sumOfColWidth = sum(flat.full.map((col) => col.width))
const nextNeedRenderLock = sumOfColWidth > this.domHelper.artTable.clientWidth
if (needRenderLock !== nextNeedRenderLock) {
this.setState({ needRenderLock: nextNeedRenderLock })
}
} else {
if (needRenderLock) {
this.setState({ needRenderLock: false })
}
}
}
}