Skip to content

Commit 8cb7a7c

Browse files
authored
Merge pull request #28 from Lodin/fix/recompute-on-expand
Fix resetting height after re-computation (second attempt)
2 parents 7e317bf + 6d32b7b commit 8cb7a7c

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
lines changed

__tests__/VariableSizeTree.spec.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,16 @@ describe('VariableSizeTree', () => {
226226
});
227227

228228
describe('recomputeTree', () => {
229+
let resetAfterIndexSpy: jest.SpyInstance;
230+
231+
beforeEach(() => {
232+
const listInstance = component
233+
.find(VariableSizeList)
234+
.instance() as VariableSizeList;
235+
236+
resetAfterIndexSpy = jest.spyOn(listInstance, 'resetAfterIndex');
237+
});
238+
229239
it('updates tree order', async () => {
230240
tree = {
231241
children: [
@@ -284,6 +294,8 @@ describe('VariableSizeTree', () => {
284294
treeData: undefined,
285295
},
286296
);
297+
298+
expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
287299
});
288300

289301
it('updates tree nodes metadata', async () => {
@@ -344,6 +356,8 @@ describe('VariableSizeTree', () => {
344356
treeData: undefined,
345357
},
346358
);
359+
360+
expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
347361
});
348362

349363
it('resets current openness to default', async () => {
@@ -408,6 +422,8 @@ describe('VariableSizeTree', () => {
408422
treeData: undefined,
409423
},
410424
);
425+
426+
expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
411427
});
412428

413429
it('resets current openness to the new default provided by the node refreshing', async () => {
@@ -465,6 +481,8 @@ describe('VariableSizeTree', () => {
465481
treeData: undefined,
466482
},
467483
);
484+
485+
expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
468486
});
469487

470488
it('provides a toggle function that changes openness state of the specific node', async () => {
@@ -482,6 +500,8 @@ describe('VariableSizeTree', () => {
482500
expect(treeWalkerSpy).toHaveBeenCalledWith(false);
483501
expect(foo1.height).toBe(defaultHeight);
484502
expect(foo1.isOpen).toBeFalsy();
503+
504+
expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
485505
});
486506

487507
it('resets current height to default', async () => {
@@ -548,6 +568,8 @@ describe('VariableSizeTree', () => {
548568
treeData: undefined,
549569
},
550570
);
571+
572+
expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
551573
});
552574

553575
it('resets current height to the new default provided by the node refreshing', async () => {
@@ -605,6 +627,8 @@ describe('VariableSizeTree', () => {
605627
treeData: undefined,
606628
},
607629
);
630+
631+
expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
608632
});
609633

610634
it('opens and closes nodes as specified in opennessState', async () => {
@@ -646,6 +670,7 @@ describe('VariableSizeTree', () => {
646670
expect(foo1!.isOpen).toBeTruthy();
647671
expect(foo2!.isOpen).toBeTruthy();
648672
expect(foo3!.isOpen).not.toBeTruthy();
673+
expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
649674
});
650675

651676
it('opennessState is overridden by useDefaultOpenness', async () => {

src/FixedSizeTree.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ export class FixedSizeTree<T extends FixedSizeNodeData = NodeData> extends Tree<
7777
return (
7878
<FixedSizeList
7979
{...rest}
80-
itemData={this.state}
8180
itemCount={this.state.order!.length}
81+
itemData={this.state}
8282
ref={this.list}
8383
>
8484
{rowComponent!}

src/Tree.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ListProps,
1313
VariableSizeList,
1414
} from 'react-window';
15+
import {DefaultTreeProps, DefaultTreeState} from './utils';
1516

1617
export type NodeData = Readonly<{
1718
/**
@@ -261,14 +262,14 @@ class Tree<
261262
>,
262263
TListComponent extends FixedSizeList | VariableSizeList
263264
> extends PureComponent<TProps, TState> {
264-
public static defaultProps: Partial<TreeProps<any, any>> = {
265+
public static defaultProps: Partial<DefaultTreeProps> = {
265266
rowComponent: Row,
266267
};
267268

268269
public static getDerivedStateFromProps(
269-
props: TreeProps<any, any>,
270-
state: TreeState<any, any, any, any>,
271-
): Partial<TreeState<any, any, any, any>> {
270+
props: DefaultTreeProps,
271+
state: DefaultTreeState,
272+
): Partial<DefaultTreeState> {
272273
const {children: component, itemData: treeData, treeWalker} = props;
273274
const {computeTree, order, treeWalker: oldTreeWalker} = state;
274275

src/VariableSizeTree.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ export class VariableSizeTree<T extends VariableSizeNodeData> extends Tree<
138138

139139
public recomputeTree(options?: VariableSizeUpdateOptions): Promise<void> {
140140
return super.recomputeTree(options).then(() => {
141-
if (options?.useDefaultHeight) {
142-
this.list.current?.resetAfterIndex(0, true);
143-
}
141+
this.list.current?.resetAfterIndex(0, true);
144142
});
145143
}
146144

@@ -150,8 +148,8 @@ export class VariableSizeTree<T extends VariableSizeNodeData> extends Tree<
150148
return (
151149
<VariableSizeList
152150
{...rest}
153-
itemData={this.state}
154151
itemCount={this.state.order!.length}
152+
itemData={this.state}
155153
// eslint-disable-next-line @typescript-eslint/unbound-method
156154
itemSize={itemSize ?? this.getItemSize}
157155
ref={this.list}

src/utils.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,29 @@ import {
33
NodeData,
44
NodeRecord,
55
TreeCreatorOptions,
6+
TreeProps,
67
TreeState,
78
UpdateOptions,
89
} from './Tree';
910

11+
export type DefaultTreeProps = TreeProps<
12+
NodeComponentProps<NodeData>,
13+
NodeData
14+
>;
15+
16+
export type DefaultTreeState = TreeState<
17+
NodeComponentProps<NodeData>,
18+
NodeRecord<NodeData>,
19+
UpdateOptions,
20+
NodeData
21+
>;
22+
1023
export type DefaultTreeCreatorOptions = TreeCreatorOptions<
1124
NodeComponentProps<NodeData>,
1225
NodeRecord<NodeData>,
1326
UpdateOptions,
1427
NodeData,
15-
TreeState<
16-
NodeComponentProps<NodeData>,
17-
NodeRecord<NodeData>,
18-
UpdateOptions,
19-
NodeData
20-
>
28+
DefaultTreeState
2129
>;
2230

2331
export const identity = <T>(value: T): T => value;

0 commit comments

Comments
 (0)