Skip to content

Commit 025f441

Browse files
authored
Merge pull request #15 from Lodin/fix/context-regression
Fix context regression in toggle function
2 parents 58e1b90 + 9571094 commit 025f441

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

__tests__/FixedSizeTree.spec.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,11 @@ describe('FixedSizeTree', () => {
428428
const foo1 = component.state('records')['foo-1'];
429429

430430
treeWalkerSpy.mockClear();
431-
await foo1.toggle();
431+
432+
// Imitate the behavior of Node component where toggle is sent without
433+
// context
434+
const {toggle} = foo1;
435+
await toggle();
432436

433437
expect(treeWalkerSpy).toHaveBeenCalledWith(false);
434438
expect(foo1.isOpen).toBeFalsy();

__tests__/VariableSizeTree.spec.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,11 @@ describe('VariableSizeTree', () => {
478478
foo1.height = 50;
479479

480480
treeWalkerSpy.mockClear();
481-
await foo1.toggle();
481+
482+
// Imitate the behavior of Node component where toggle is sent without
483+
// context
484+
const {toggle} = foo1;
485+
await toggle();
482486

483487
expect(treeWalkerSpy).toHaveBeenCalledWith(false);
484488
expect(foo1.height).toBe(defaultHeight);

src/FixedSizeTree.tsx

+7-5
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,19 @@ const computeTree = <T extends {}>(
7070
} else {
7171
({id} = value);
7272
const {isOpenByDefault} = value;
73-
const record = records[id as string];
73+
let record = records[id as string];
7474

7575
if (!record) {
76-
records[id as string] = {
76+
record = {
7777
data: value,
7878
isOpen: isOpenByDefault,
79-
async toggle(this: FixedSizeNodeRecord<T>): Promise<void> {
80-
this.isOpen = !this.isOpen;
81-
await recomputeTree({refreshNodes: this.isOpen});
79+
async toggle(): Promise<void> {
80+
record.isOpen = !record.isOpen;
81+
await recomputeTree({refreshNodes: record.isOpen});
8282
},
8383
};
84+
85+
records[id as string] = record;
8486
} else {
8587
record.data = value;
8688

src/VariableSizeTree.tsx

+10-12
Original file line numberDiff line numberDiff line change
@@ -105,29 +105,27 @@ const computeTree = <T extends {}>(
105105
} else {
106106
({id} = value);
107107
const {defaultHeight, isOpenByDefault} = value;
108-
const record = records[id as string];
108+
let record = records[id as string];
109109

110110
if (!record) {
111-
records[id as string] = {
111+
record = {
112112
data: value,
113113
height: defaultHeight,
114114
isOpen: isOpenByDefault,
115-
resize(
116-
this: VariableSizeNodeRecord<T>,
117-
height: number,
118-
shouldForceUpdate?: boolean,
119-
): void {
120-
this.height = height;
121-
resetAfterId(this.data.id, shouldForceUpdate);
115+
resize(height: number, shouldForceUpdate?: boolean): void {
116+
record.height = height;
117+
resetAfterId(record.data.id, shouldForceUpdate);
122118
},
123-
async toggle(this: VariableSizeNodeRecord<T>): Promise<void> {
124-
this.isOpen = !this.isOpen;
119+
async toggle(): Promise<void> {
120+
record.isOpen = !record.isOpen;
125121
await recomputeTree({
126-
refreshNodes: this.isOpen,
122+
refreshNodes: record.isOpen,
127123
useDefaultHeight: true,
128124
});
129125
},
130126
};
127+
128+
records[id as string] = record;
131129
} else {
132130
record.data = value;
133131

0 commit comments

Comments
 (0)