Skip to content

Consider opennessState on record creation #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions __tests__/FixedSizeTree.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {FixedSizeList} from 'react-window';
import {
FixedSizeNodeComponentProps,
FixedSizeNodeData,
FixedSizeNodeRecord,
FixedSizeTree,
FixedSizeTreeProps,
FixedSizeTreeState,
Expand Down Expand Up @@ -75,6 +76,18 @@ describe('FixedSizeTree', () => {
}
}

const mountComponent = (): typeof component =>
mount(
<FixedSizeTree<ExtendedData>
itemSize={30}
treeWalker={treeWalkerSpy}
height={500}
width={500}
>
{Node}
</FixedSizeTree>,
);

beforeEach(() => {
tree = {
children: [
Expand All @@ -89,16 +102,7 @@ describe('FixedSizeTree', () => {

treeWalkerSpy = jest.fn(treeWalker);

component = mount(
<FixedSizeTree<ExtendedData>
itemSize={30}
treeWalker={treeWalkerSpy}
height={500}
width={500}
>
{Node}
</FixedSizeTree>,
);
component = mountComponent();
});

it('renders a component', () => {
Expand Down Expand Up @@ -479,6 +483,33 @@ describe('FixedSizeTree', () => {
expect(foo2!.isOpen).toBeTruthy();
expect(foo3!.isOpen).toBeTruthy();
});

it('opennessState works when node is created during update', async () => {
component.unmount();
isOpenByDefault = false;
component = mountComponent();
treeInstance = component.instance();

await treeInstance.recomputeTree({
opennessState: {
'foo-1': true,
'foo-2': true,
'foo-3': true,
},
refreshNodes: true,
});
component.update();

const {records} = component.find(FixedSizeList).prop('itemData') as {
records: Record<string, FixedSizeNodeRecord<ExtendedData>>;
};

expect(Object.keys(records).map((key) => records[key].isOpen)).toEqual([
true,
true,
true,
]);
});
});

it('provides a toggle function that changes openness state of the specific node', async () => {
Expand Down
49 changes: 40 additions & 9 deletions __tests__/VariableSizeTree.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Row,
VariableSizeNodeComponentProps,
VariableSizeNodeData,
VariableSizeNodeRecord,
VariableSizeTree,
VariableSizeTreeProps,
VariableSizeTreeState,
Expand Down Expand Up @@ -77,6 +78,17 @@ describe('VariableSizeTree', () => {
}
}

const mountComponent = (): typeof component =>
mount(
<VariableSizeTree<ExtendedData>
treeWalker={treeWalkerSpy}
height={500}
width={500}
>
{Node}
</VariableSizeTree>,
);

beforeEach(() => {
tree = {
children: [
Expand All @@ -92,15 +104,7 @@ describe('VariableSizeTree', () => {

treeWalkerSpy = jest.fn(treeWalker);

component = mount(
<VariableSizeTree<ExtendedData>
treeWalker={treeWalkerSpy}
height={500}
width={500}
>
{Node}
</VariableSizeTree>,
);
component = mountComponent();
});

it('renders a component', () => {
Expand Down Expand Up @@ -692,6 +696,33 @@ describe('VariableSizeTree', () => {
expect(foo2!.isOpen).toBeTruthy();
expect(foo3!.isOpen).toBeTruthy();
});

it('opennessState works when node is created during update', async () => {
component.unmount();
isOpenByDefault = false;
component = mountComponent();
treeInstance = component.instance();

await treeInstance.recomputeTree({
opennessState: {
'foo-1': true,
'foo-2': true,
'foo-3': true,
},
refreshNodes: true,
});
component.update();

const {records} = component.find(VariableSizeList).prop('itemData') as {
records: Record<string, VariableSizeNodeRecord<ExtendedData>>;
};

expect(Object.keys(records).map((key) => records[key].isOpen)).toEqual([
true,
true,
true,
]);
});
});

it('provides a resize function that changes height of the specific node', () => {
Expand Down
8 changes: 6 additions & 2 deletions src/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ export type TreeCreatorOptions<
TData
>
> = Readonly<{
createRecord: (data: TData, state: TState) => TNodeRecord;
createRecord: (
data: TData,
options: TUpdateOptions,
state: TState,
) => TNodeRecord;
shouldUpdateRecords: (options: TUpdateOptions) => boolean;
updateRecord: (
record: TNodeRecord,
Expand Down Expand Up @@ -226,7 +230,7 @@ export const createTreeComputer = <
const record = records[id as string];

if (!record) {
records[id as string] = createRecord(value, state);
records[id as string] = createRecord(value, options, state);
} else {
record.data = value;
updateRecordOnNewData(record, options);
Expand Down
4 changes: 2 additions & 2 deletions src/VariableSizeTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ const computeTree = createTreeComputer<
VariableSizeTreeProps<VariableSizeNodeData>,
VariableSizeTreeState<VariableSizeNodeData>
>({
createRecord: (data, {recomputeTree, resetAfterId}) => {
createRecord: (data, {opennessState}, {recomputeTree, resetAfterId}) => {
const record = {
data,
height: data.defaultHeight,
isOpen: data.isOpenByDefault,
isOpen: opennessState?.[data.id as string] ?? data.isOpenByDefault,
resize(height: number, shouldForceUpdate?: boolean): void {
record.height = height;
resetAfterId(record.data.id, shouldForceUpdate);
Expand Down
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ export const identity = <T>(value: T): T => value;

export const createRecord: DefaultTreeCreatorOptions['createRecord'] = (
data,
{opennessState},
{recomputeTree},
) => {
const record = {
data,
isOpen: data.isOpenByDefault,
isOpen: opennessState?.[data.id as string] ?? data.isOpenByDefault,
toggle(): Promise<void> {
record.isOpen = !record.isOpen;

Expand Down