Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(table): table lazy-load reset bug #2041

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/table/TR.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const TABLE_PROPS = [
'onRowMouseup',
] as const;

export type TrPropsKeys = typeof TABLE_PROPS[number];
export type TrPropsKeys = (typeof TABLE_PROPS)[number];

export interface TrProps extends TrCommonProps {
rowKey: string;
Expand Down Expand Up @@ -102,7 +102,7 @@ export default function TR(props: TrProps) {
}, [row, rowClassName, rowIndex, rowKey, trStyles?.classes]);

const useLazyLoadParams = useMemo(() => ({ ...scroll, rowIndex }), [scroll, rowIndex]);
const { hasLazyLoadHolder, tRowHeight } = useLazyLoad(tableContentElm, trRef.current, useLazyLoadParams);
const { hasLazyLoadHolder, tRowHeight } = useLazyLoad(tableContentElm, trRef, useLazyLoadParams);

useEffect(() => {
if (virtualConfig.isVirtualScroll && trRef.current) {
Expand Down
7 changes: 4 additions & 3 deletions src/table/hooks/useLazyLoad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* 原作者 @louiszhai 思路
*/
import { useState, useMemo, useEffect } from 'react';
import type React from 'react';
import observe from '../../_common/js/utils/observe';

export type UseLazyLoadParams = {
Expand All @@ -13,12 +14,12 @@ export type UseLazyLoadParams = {

export default function useLazyLoad(
containerRef: HTMLElement,
childRef: HTMLTableRowElement,
childRef: React.MutableRefObject<HTMLTableRowElement>,
params: UseLazyLoadParams,
) {
// useMemo 用意:1. 为了实时响应数据;2. 表格的计算量容易过大,提前存储
const tRowHeight = useMemo(() => Math.max(params.rowHeight || 48, 48), [params.rowHeight]);
const [isInit, setIsInit] = useState(params.rowIndex === 0);
const [isInit, setIsInit] = useState(params.rowIndex === -1);
const hasLazyLoadHolder = useMemo(() => params?.type === 'lazy' && !isInit, [isInit, params?.type]);

const requestAnimationFrame =
Expand All @@ -37,7 +38,7 @@ export default function useLazyLoad(
const timer = setTimeout(() => {
const bufferSize = Math.max(10, params.bufferSize || 10);
const height = tRowHeight * bufferSize;
childRef && observe(childRef, containerRef, init, height);
childRef && observe(childRef.current, containerRef, init, height);
clearTimeout(timer);
});
return () => {
Expand Down
94 changes: 4 additions & 90 deletions test/snap/__snapshots__/csr.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -213509,51 +213509,8 @@ exports[`csr snapshot test > csr test src/table/_example/lazy.jsx 1`] = `
class=""
>
<td
class=""
>
贾明
</td>
<td
class=""
>
<span
class="t-tag t-tag--success t-tag--light-outline t-tag--round"
>
<svg
class="t-icon t-icon-check-circle-filled"
fill="none"
height="1em"
viewBox="0 0 16 16"
width="1em"
>
<path
d="M8 15A7 7 0 108 1a7 7 0 000 14zM4.5 8.2l.7-.7L7 9.3l3.8-3.8.7.7L7 10.7 4.5 8.2z"
fill="currentColor"
fill-opacity="0.9"
/>
</svg>
<span
title="审批通过"
>
审批通过
</span>
</span>
</td>
<td
class=""
>
部分宣传物料制作费用
</td>
<td
class=""
>
w.cezkdudy@lhll.au
</td>
<td
class=""
>
2022-01-01
</td>
style="height: 48px;"
/>
</tr>
<tr
class=""
Expand Down Expand Up @@ -220678,51 +220635,8 @@ exports[`csr snapshot test > csr test src/table/_example/lazy.jsx 1`] = `
class=""
>
<td
class=""
>
贾明
</td>
<td
class=""
>
<span
class="t-tag t-tag--success t-tag--light-outline t-tag--round"
>
<svg
class="t-icon t-icon-check-circle-filled"
fill="none"
height="1em"
viewBox="0 0 16 16"
width="1em"
>
<path
d="M8 15A7 7 0 108 1a7 7 0 000 14zM4.5 8.2l.7-.7L7 9.3l3.8-3.8.7.7L7 10.7 4.5 8.2z"
fill="currentColor"
fill-opacity="0.9"
/>
</svg>
<span
title="审批通过"
>
审批通过
</span>
</span>
</td>
<td
class=""
>
部分宣传物料制作费用
</td>
<td
class=""
>
w.cezkdudy@lhll.au
</td>
<td
class=""
>
2022-01-01
</td>
style="height: 48px;"
/>
</tr>
<tr
class=""
Expand Down
Loading