Skip to content
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

Fix resetting height after re-computation (second attempt) #28

Merged
merged 2 commits into from
Aug 10, 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
25 changes: 25 additions & 0 deletions __tests__/VariableSizeTree.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ describe('VariableSizeTree', () => {
});

describe('recomputeTree', () => {
let resetAfterIndexSpy: jest.SpyInstance;

beforeEach(() => {
const listInstance = component
.find(VariableSizeList)
.instance() as VariableSizeList;

resetAfterIndexSpy = jest.spyOn(listInstance, 'resetAfterIndex');
});

it('updates tree order', async () => {
tree = {
children: [
Expand Down Expand Up @@ -284,6 +294,8 @@ describe('VariableSizeTree', () => {
treeData: undefined,
},
);

expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
});

it('updates tree nodes metadata', async () => {
Expand Down Expand Up @@ -344,6 +356,8 @@ describe('VariableSizeTree', () => {
treeData: undefined,
},
);

expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
});

it('resets current openness to default', async () => {
Expand Down Expand Up @@ -408,6 +422,8 @@ describe('VariableSizeTree', () => {
treeData: undefined,
},
);

expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
});

it('resets current openness to the new default provided by the node refreshing', async () => {
Expand Down Expand Up @@ -465,6 +481,8 @@ describe('VariableSizeTree', () => {
treeData: undefined,
},
);

expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
});

it('provides a toggle function that changes openness state of the specific node', async () => {
Expand All @@ -482,6 +500,8 @@ describe('VariableSizeTree', () => {
expect(treeWalkerSpy).toHaveBeenCalledWith(false);
expect(foo1.height).toBe(defaultHeight);
expect(foo1.isOpen).toBeFalsy();

expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
});

it('resets current height to default', async () => {
Expand Down Expand Up @@ -548,6 +568,8 @@ describe('VariableSizeTree', () => {
treeData: undefined,
},
);

expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
});

it('resets current height to the new default provided by the node refreshing', async () => {
Expand Down Expand Up @@ -605,6 +627,8 @@ describe('VariableSizeTree', () => {
treeData: undefined,
},
);

expect(resetAfterIndexSpy).toHaveBeenCalledWith(0, true);
});

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

it('opennessState is overridden by useDefaultOpenness', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/FixedSizeTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export class FixedSizeTree<T extends FixedSizeNodeData = NodeData> extends Tree<
return (
<FixedSizeList
{...rest}
itemData={this.state}
itemCount={this.state.order!.length}
itemData={this.state}
ref={this.list}
>
{rowComponent!}
Expand Down
9 changes: 5 additions & 4 deletions src/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ListProps,
VariableSizeList,
} from 'react-window';
import {DefaultTreeProps, DefaultTreeState} from './utils';

export type NodeData = Readonly<{
/**
Expand Down Expand Up @@ -261,14 +262,14 @@ class Tree<
>,
TListComponent extends FixedSizeList | VariableSizeList
> extends PureComponent<TProps, TState> {
public static defaultProps: Partial<TreeProps<any, any>> = {
public static defaultProps: Partial<DefaultTreeProps> = {
rowComponent: Row,
};

public static getDerivedStateFromProps(
props: TreeProps<any, any>,
state: TreeState<any, any, any, any>,
): Partial<TreeState<any, any, any, any>> {
props: DefaultTreeProps,
state: DefaultTreeState,
): Partial<DefaultTreeState> {
const {children: component, itemData: treeData, treeWalker} = props;
const {computeTree, order, treeWalker: oldTreeWalker} = state;

Expand Down
6 changes: 2 additions & 4 deletions src/VariableSizeTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ export class VariableSizeTree<T extends VariableSizeNodeData> extends Tree<

public recomputeTree(options?: VariableSizeUpdateOptions): Promise<void> {
return super.recomputeTree(options).then(() => {
if (options?.useDefaultHeight) {
this.list.current?.resetAfterIndex(0, true);
}
this.list.current?.resetAfterIndex(0, true);
});
}

Expand All @@ -150,8 +148,8 @@ export class VariableSizeTree<T extends VariableSizeNodeData> extends Tree<
return (
<VariableSizeList
{...rest}
itemData={this.state}
itemCount={this.state.order!.length}
itemData={this.state}
// eslint-disable-next-line @typescript-eslint/unbound-method
itemSize={itemSize ?? this.getItemSize}
ref={this.list}
Expand Down
20 changes: 14 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,29 @@ import {
NodeData,
NodeRecord,
TreeCreatorOptions,
TreeProps,
TreeState,
UpdateOptions,
} from './Tree';

export type DefaultTreeProps = TreeProps<
NodeComponentProps<NodeData>,
NodeData
>;

export type DefaultTreeState = TreeState<
NodeComponentProps<NodeData>,
NodeRecord<NodeData>,
UpdateOptions,
NodeData
>;

export type DefaultTreeCreatorOptions = TreeCreatorOptions<
NodeComponentProps<NodeData>,
NodeRecord<NodeData>,
UpdateOptions,
NodeData,
TreeState<
NodeComponentProps<NodeData>,
NodeRecord<NodeData>,
UpdateOptions,
NodeData
>
DefaultTreeState
>;

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