Skip to content

Commit bc65caa

Browse files
authored
Merge pull request #26 from Lodin/fix/reset-height-after-recomputation
Fix resetting height after re-computation
2 parents 052e5b7 + dcacfc1 commit bc65caa

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

__tests__/VariableSizeTree.spec.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,5 +686,21 @@ describe('VariableSizeTree', () => {
686686
);
687687
expect(foo3.height).toBe(100);
688688
});
689+
690+
it('properly resets heights after re-computation', async () => {
691+
const listInstance: VariableSizeList = component
692+
.find(VariableSizeList)
693+
.instance() as VariableSizeList;
694+
695+
const resetAfterIndexSpy = jest.spyOn(listInstance, 'resetAfterIndex');
696+
const foo1 = component.state('records')['foo-1']!;
697+
const foo3 = component.state('records')['foo-3']!;
698+
699+
foo3.resize(100, true);
700+
await foo1.toggle();
701+
702+
expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
703+
expect(foo3.height).toBe(30);
704+
});
689705
});
690706
});

src/FixedSizeTree.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
createRecord,
1414
shouldUpdateRecords,
1515
updateRecord,
16-
updateRecordOnWalk,
16+
updateRecordOnNewData,
1717
} from './utils';
1818

1919
export type FixedSizeNodeData = NodeData;
@@ -50,7 +50,7 @@ const computeTree = createTreeComputer<
5050
createRecord,
5151
shouldUpdateRecords,
5252
updateRecord,
53-
updateRecordOnWalk,
53+
updateRecordOnNewData,
5454
});
5555

5656
export class FixedSizeTree<T extends FixedSizeNodeData = NodeData> extends Tree<

src/Tree.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export type TreeCreatorOptions<
136136
recordId: string | symbol,
137137
options: TUpdateOptions,
138138
) => void;
139-
updateRecordOnWalk: (record: TNodeRecord, options: TUpdateOptions) => void;
139+
updateRecordOnNewData: (record: TNodeRecord, options: TUpdateOptions) => void;
140140
}>;
141141

142142
export type TreeComputer<
@@ -173,7 +173,7 @@ export const createTreeComputer = <
173173
createRecord,
174174
shouldUpdateRecords,
175175
updateRecord,
176-
updateRecordOnWalk,
176+
updateRecordOnNewData,
177177
}: TreeCreatorOptions<
178178
TNodeComponentProps,
179179
TNodeRecord,
@@ -228,7 +228,7 @@ export const createTreeComputer = <
228228
records[id as string] = createRecord(value, state);
229229
} else {
230230
record.data = value;
231-
updateRecordOnWalk(record, options);
231+
updateRecordOnNewData(record, options);
232232
}
233233
}
234234

src/VariableSizeTree.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import Tree, {
99
TreeState,
1010
UpdateOptions,
1111
} from './Tree';
12-
import {shouldUpdateRecords, updateRecord, updateRecordOnWalk} from './utils';
12+
import {
13+
shouldUpdateRecords,
14+
updateRecord,
15+
updateRecordOnNewData,
16+
} from './utils';
1317

1418
export type VariableSizeNodeData = Readonly<{
1519
/** Default node height. Can be used only with VariableSizeTree */
@@ -94,8 +98,8 @@ const computeTree = createTreeComputer<
9498
updateRecord(record, recordId, options);
9599
},
96100

97-
updateRecordOnWalk: (record, options) => {
98-
updateRecordOnWalk(record, options);
101+
updateRecordOnNewData: (record, options) => {
102+
updateRecordOnNewData(record, options);
99103

100104
if (options.useDefaultHeight) {
101105
record.height = record.data.defaultHeight;
@@ -132,6 +136,14 @@ export class VariableSizeTree<T extends VariableSizeNodeData> extends Tree<
132136
);
133137
}
134138

139+
public recomputeTree(options?: VariableSizeUpdateOptions): Promise<void> {
140+
return super.recomputeTree(options).then(() => {
141+
if (options?.useDefaultHeight) {
142+
this.list.current?.resetAfterIndex(0, true);
143+
}
144+
});
145+
}
146+
135147
public render(): ReactNode {
136148
const {children, itemSize, rowComponent, treeWalker, ...rest} = this.props;
137149

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const updateRecord: DefaultTreeCreatorOptions['updateRecord'] = (
5454
: opennessState?.[recordId as string] ?? record.isOpen;
5555
};
5656

57-
export const updateRecordOnWalk: DefaultTreeCreatorOptions['updateRecordOnWalk'] = (
57+
export const updateRecordOnNewData: DefaultTreeCreatorOptions['updateRecordOnNewData'] = (
5858
record,
5959
{useDefaultOpenness = false},
6060
) => {

0 commit comments

Comments
 (0)