Skip to content

Commit ac54afc

Browse files
committed
fix: tests
1 parent d1c4bf9 commit ac54afc

File tree

3 files changed

+16
-26
lines changed

3 files changed

+16
-26
lines changed

packages/s2-react/__tests__/spreadsheet/adaptive-spec.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ interface Props {
1313
containerId?: string;
1414
}
1515

16-
const s2Options: S2Options = Object.freeze({
16+
const s2Options: S2Options = {
1717
width: 200,
1818
height: 200,
1919
hdAdapter: false,
20-
});
20+
};
2121

2222
let s2: SpreadSheet;
2323

@@ -39,11 +39,7 @@ function MainLayout({
3939
adaptive={adaptive}
4040
sheetType="pivot"
4141
dataCfg={mockDataConfig}
42-
options={{
43-
width: 200,
44-
height: 200,
45-
hdAdapter: false,
46-
}}
42+
options={s2Options}
4743
themeCfg={{ name: 'default' }}
4844
getSpreadSheet={(instance) => {
4945
s2 = instance;
@@ -75,7 +71,7 @@ describe('SheetComponent adaptive Tests', () => {
7571
test('should use container width when container width less than options width and table first rendered', async () => {
7672
act(() => {
7773
ReactDOM.render(
78-
<MainLayout adaptive containerWidth={s2Options.width - 100} />,
74+
<MainLayout adaptive={true} containerWidth={s2Options.width - 100} />,
7975
getContainer(),
8076
);
8177
});

packages/s2-react/playground/index.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,12 @@ function MainLayout() {
8888
const s2Ref = React.useRef<SpreadSheet>();
8989

9090
// ================== Callback ========================
91-
const updateOptions = (newOptions: Partial<S2Options<React.ReactNode>>) => {
92-
setOptions(customMerge({}, options, newOptions));
93-
};
91+
const updateOptions = React.useCallback(
92+
(newOptions: Partial<S2Options<React.ReactNode>>) => {
93+
setOptions(customMerge({}, options, newOptions));
94+
},
95+
[options],
96+
);
9497

9598
const updateDataCfg = (newDataCfg: Partial<S2DataConfig>) => {
9699
const currentDataCfg =
@@ -189,7 +192,7 @@ function MainLayout() {
189192
updateOptions(defaultOptions);
190193
break;
191194
}
192-
}, [sheetType]);
195+
}, [sheetType, updateOptions]);
193196

194197
// ================== Config ========================
195198

@@ -281,7 +284,7 @@ function MainLayout() {
281284

282285
return (
283286
<div className="playground">
284-
<Collapse activeKey="filter">
287+
<Collapse defaultActiveKey="filter">
285288
<Collapse.Panel header="筛选器" key="filter">
286289
<Space style={{ marginBottom: 20 }}>
287290
<Switch

packages/s2-react/src/hooks/useResize.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react';
22
import { debounce } from 'lodash';
3-
import type { S2Options, SpreadSheet } from '@antv/s2';
4-
import { usePrevious } from './usePrevious';
3+
import type { SpreadSheet } from '@antv/s2';
54

65
export interface UseResizeEffectParams {
76
container: HTMLElement;
@@ -14,7 +13,6 @@ const RENDER_DELAY = 200; // ms
1413
export const useResize = (params: UseResizeEffectParams) => {
1514
const { container, s2, adaptive } = params;
1615

17-
const prevOptions = usePrevious<S2Options>(s2?.options);
1816
// 第一次自适应时不需要 debounce, 防止抖动
1917
const isFirstRender = React.useRef<boolean>(true);
2018

@@ -28,18 +26,11 @@ export const useResize = (params: UseResizeEffectParams) => {
2826

2927
// rerender by option
3028
React.useEffect(() => {
31-
if (!adaptive && s2 && prevOptions) {
32-
const isChanged =
33-
prevOptions.width !== s2.options.width ||
34-
prevOptions.height !== s2.options.height;
35-
36-
if (!isChanged) {
37-
return;
38-
}
29+
if (!adaptive && s2) {
3930
s2.changeSize(s2.options.width, s2.options.height);
4031
s2.render(false);
4132
}
42-
}, [s2?.options.width, s2?.options.height, adaptive, s2, prevOptions]);
33+
}, [s2?.options.width, s2?.options.height, adaptive, s2]);
4334

4435
// rerender by container resize or window resize
4536
React.useLayoutEffect(() => {
@@ -66,5 +57,5 @@ export const useResize = (params: UseResizeEffectParams) => {
6657
resizeObserver.unobserve(container);
6758
};
6859
// eslint-disable-next-line react-hooks/exhaustive-deps
69-
}, [adaptive, container, render]);
60+
}, [adaptive, container]);
7061
};

0 commit comments

Comments
 (0)