Skip to content

Commit cb95775

Browse files
authored
Merge pull request #36 from Lodin/fix/openness-state-on-creation
Consider opennessState on record creation
2 parents 3502201 + ab311db commit cb95775

File tree

5 files changed

+91
-24
lines changed

5 files changed

+91
-24
lines changed

__tests__/FixedSizeTree.spec.tsx

+41-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {FixedSizeList} from 'react-window';
44
import {
55
FixedSizeNodeComponentProps,
66
FixedSizeNodeData,
7+
FixedSizeNodeRecord,
78
FixedSizeTree,
89
FixedSizeTreeProps,
910
FixedSizeTreeState,
@@ -75,6 +76,18 @@ describe('FixedSizeTree', () => {
7576
}
7677
}
7778

79+
const mountComponent = (): typeof component =>
80+
mount(
81+
<FixedSizeTree<ExtendedData>
82+
itemSize={30}
83+
treeWalker={treeWalkerSpy}
84+
height={500}
85+
width={500}
86+
>
87+
{Node}
88+
</FixedSizeTree>,
89+
);
90+
7891
beforeEach(() => {
7992
tree = {
8093
children: [
@@ -89,16 +102,7 @@ describe('FixedSizeTree', () => {
89102

90103
treeWalkerSpy = jest.fn(treeWalker);
91104

92-
component = mount(
93-
<FixedSizeTree<ExtendedData>
94-
itemSize={30}
95-
treeWalker={treeWalkerSpy}
96-
height={500}
97-
width={500}
98-
>
99-
{Node}
100-
</FixedSizeTree>,
101-
);
105+
component = mountComponent();
102106
});
103107

104108
it('renders a component', () => {
@@ -479,6 +483,33 @@ describe('FixedSizeTree', () => {
479483
expect(foo2!.isOpen).toBeTruthy();
480484
expect(foo3!.isOpen).toBeTruthy();
481485
});
486+
487+
it('opennessState works when node is created during update', async () => {
488+
component.unmount();
489+
isOpenByDefault = false;
490+
component = mountComponent();
491+
treeInstance = component.instance();
492+
493+
await treeInstance.recomputeTree({
494+
opennessState: {
495+
'foo-1': true,
496+
'foo-2': true,
497+
'foo-3': true,
498+
},
499+
refreshNodes: true,
500+
});
501+
component.update();
502+
503+
const {records} = component.find(FixedSizeList).prop('itemData') as {
504+
records: Record<string, FixedSizeNodeRecord<ExtendedData>>;
505+
};
506+
507+
expect(Object.keys(records).map((key) => records[key].isOpen)).toEqual([
508+
true,
509+
true,
510+
true,
511+
]);
512+
});
482513
});
483514

484515
it('provides a toggle function that changes openness state of the specific node', async () => {

__tests__/VariableSizeTree.spec.tsx

+40-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Row,
66
VariableSizeNodeComponentProps,
77
VariableSizeNodeData,
8+
VariableSizeNodeRecord,
89
VariableSizeTree,
910
VariableSizeTreeProps,
1011
VariableSizeTreeState,
@@ -77,6 +78,17 @@ describe('VariableSizeTree', () => {
7778
}
7879
}
7980

81+
const mountComponent = (): typeof component =>
82+
mount(
83+
<VariableSizeTree<ExtendedData>
84+
treeWalker={treeWalkerSpy}
85+
height={500}
86+
width={500}
87+
>
88+
{Node}
89+
</VariableSizeTree>,
90+
);
91+
8092
beforeEach(() => {
8193
tree = {
8294
children: [
@@ -92,15 +104,7 @@ describe('VariableSizeTree', () => {
92104

93105
treeWalkerSpy = jest.fn(treeWalker);
94106

95-
component = mount(
96-
<VariableSizeTree<ExtendedData>
97-
treeWalker={treeWalkerSpy}
98-
height={500}
99-
width={500}
100-
>
101-
{Node}
102-
</VariableSizeTree>,
103-
);
107+
component = mountComponent();
104108
});
105109

106110
it('renders a component', () => {
@@ -692,6 +696,33 @@ describe('VariableSizeTree', () => {
692696
expect(foo2!.isOpen).toBeTruthy();
693697
expect(foo3!.isOpen).toBeTruthy();
694698
});
699+
700+
it('opennessState works when node is created during update', async () => {
701+
component.unmount();
702+
isOpenByDefault = false;
703+
component = mountComponent();
704+
treeInstance = component.instance();
705+
706+
await treeInstance.recomputeTree({
707+
opennessState: {
708+
'foo-1': true,
709+
'foo-2': true,
710+
'foo-3': true,
711+
},
712+
refreshNodes: true,
713+
});
714+
component.update();
715+
716+
const {records} = component.find(VariableSizeList).prop('itemData') as {
717+
records: Record<string, VariableSizeNodeRecord<ExtendedData>>;
718+
};
719+
720+
expect(Object.keys(records).map((key) => records[key].isOpen)).toEqual([
721+
true,
722+
true,
723+
true,
724+
]);
725+
});
695726
});
696727

697728
it('provides a resize function that changes height of the specific node', () => {

src/Tree.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ export type TreeCreatorOptions<
130130
TData
131131
>
132132
> = Readonly<{
133-
createRecord: (data: TData, state: TState) => TNodeRecord;
133+
createRecord: (
134+
data: TData,
135+
options: TUpdateOptions,
136+
state: TState,
137+
) => TNodeRecord;
134138
shouldUpdateRecords: (options: TUpdateOptions) => boolean;
135139
updateRecord: (
136140
record: TNodeRecord,
@@ -226,7 +230,7 @@ export const createTreeComputer = <
226230
const record = records[id as string];
227231

228232
if (!record) {
229-
records[id as string] = createRecord(value, state);
233+
records[id as string] = createRecord(value, options, state);
230234
} else {
231235
record.data = value;
232236
updateRecordOnNewData(record, options);

src/VariableSizeTree.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ const computeTree = createTreeComputer<
6767
VariableSizeTreeProps<VariableSizeNodeData>,
6868
VariableSizeTreeState<VariableSizeNodeData>
6969
>({
70-
createRecord: (data, {recomputeTree, resetAfterId}) => {
70+
createRecord: (data, {opennessState}, {recomputeTree, resetAfterId}) => {
7171
const record = {
7272
data,
7373
height: data.defaultHeight,
74-
isOpen: data.isOpenByDefault,
74+
isOpen: opennessState?.[data.id as string] ?? data.isOpenByDefault,
7575
resize(height: number, shouldForceUpdate?: boolean): void {
7676
record.height = height;
7777
resetAfterId(record.data.id, shouldForceUpdate);

src/utils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ export const identity = <T>(value: T): T => value;
3232

3333
export const createRecord: DefaultTreeCreatorOptions['createRecord'] = (
3434
data,
35+
{opennessState},
3536
{recomputeTree},
3637
) => {
3738
const record = {
3839
data,
39-
isOpen: data.isOpenByDefault,
40+
isOpen: opennessState?.[data.id as string] ?? data.isOpenByDefault,
4041
toggle(): Promise<void> {
4142
record.isOpen = !record.isOpen;
4243

0 commit comments

Comments
 (0)