Skip to content

Commit

Permalink
Merge pull request #180 from iannbing/development
Browse files Browse the repository at this point in the history
v1.1.18
  • Loading branch information
iannbing authored Aug 4, 2020
2 parents 2306bed + 45ebacf commit 2d9990a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ Note the difference between the state `active` and `focused`. ENTER is equivalen
| initialOpenNodes | you can pass an array of node names to set some branches open as initial state | string[] | - |
| locale | you can provide a function that converts `label` into `string` | ({label, ...other}) => string | ({label}) => label |
| hasSearch | Set to `false` then `children` will not have the prop `search` | boolean | true |
| cacheSearch | Enable/Disable cache on search | boolean | true |
| matchSearch | you can define your own search function | ({label, searchTerm, ...other}) => boolean | ({label, searchTerm}) => isVisible |
| disableKeyboard | Disable keyboard navigation | boolean | false |
| children | a render props that provdes two props: `search`, `items` and `resetOpenNodes` | (ChildrenProps) => React.ReactNode | - |
Expand Down
20 changes: 13 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-simple-tree-menu",
"version": "1.1.17",
"version": "1.1.18",
"description": "A simple React tree menu component",
"keywords": [
"react",
Expand Down
3 changes: 3 additions & 0 deletions src/TreeMenu/__tests__/__snapshots__/TreeMenu.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
exports[`TreeMenu should highlight the active node 1`] = `
<TreeMenu
activeKey="releasenotes/desktop-modeler/7"
cacheSearch={true}
data={
Object {
"atd": Object {
Expand Down Expand Up @@ -268,6 +269,7 @@ exports[`TreeMenu should highlight the active node 1`] = `

exports[`TreeMenu should open specified nodes 1`] = `
<TreeMenu
cacheSearch={true}
data={
Object {
"atd": Object {
Expand Down Expand Up @@ -533,6 +535,7 @@ exports[`TreeMenu should open specified nodes 1`] = `

exports[`TreeMenu should render the level-1 nodes by default 1`] = `
<TreeMenu
cacheSearch={true}
data={
Object {
"atd": Object {
Expand Down
19 changes: 15 additions & 4 deletions src/TreeMenu/__tests__/walk.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import walk, { TreeNode, TreeNodeObject, TreeNodeInArray } from '../walk';
import {slowWalk,fastWalk, TreeNode, TreeNodeObject, TreeNodeInArray } from '../walk';

const mockDataInObject: TreeNodeObject = {
atd: {
Expand Down Expand Up @@ -97,13 +97,24 @@ const expectedOutcome = [
},
];

describe('walk', () => {
describe('slowWalk', () => {
it('should transpose the data object to a desired shape', () => {
const result = walk({ data: mockDataInObject, openNodes: [], searchTerm: '7' });
const result = slowWalk({ data: mockDataInObject, openNodes: [], searchTerm: '7' });
expect(result).toEqual(expectedOutcome);
});
it('should transpose the data array to a desired shape', () => {
const result = walk({ data: mockDataInArray, openNodes: [], searchTerm: '7' });
const result = slowWalk({ data: mockDataInArray, openNodes: [], searchTerm: '7' });
expect(result).toEqual(expectedOutcome);
});
});

describe('fastWalk', () => {
it('should transpose the data object to a desired shape', () => {
const result = fastWalk({ data: mockDataInObject, openNodes: [], searchTerm: '7' });
expect(result).toEqual(expectedOutcome);
});
it('should transpose the data array to a desired shape', () => {
const result = fastWalk({ data: mockDataInArray, openNodes: [], searchTerm: '7' });
expect(result).toEqual(expectedOutcome);
});
});
10 changes: 7 additions & 3 deletions src/TreeMenu/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import debounce from 'tiny-debounce';

import walk, {
import {
fastWalk,
slowWalk,
TreeNode,
Item,
TreeNodeInArray,
Expand All @@ -21,6 +23,7 @@ export type TreeMenuProps = {
openNodes?: string[];
resetOpenNodesOnDataUpdate?: boolean;
hasSearch?: boolean;
cacheSearch?: boolean;
onClickItem?: (props: Item) => void;
debounceTime?: number;
children?: TreeMenuChildren;
Expand All @@ -45,6 +48,7 @@ class TreeMenu extends React.Component<TreeMenuProps, TreeMenuState> {
debounceTime: 125,
children: defaultChildren,
hasSearch: true,
cacheSearch:true,
resetOpenNodesOnDataUpdate: false,
disableKeyboard: false,
};
Expand Down Expand Up @@ -95,9 +99,9 @@ class TreeMenu extends React.Component<TreeMenuProps, TreeMenuState> {
const openNodes = this.props.openNodes || this.state.openNodes;
const activeKey = this.props.activeKey || this.state.activeKey;
const focusKey = this.props.focusKey || this.state.focusKey;

const defaultSearch = this.props.cacheSearch ? fastWalk : slowWalk;
const items: Item[] = data
? walk({ data, openNodes, searchTerm, locale, matchSearch })
? defaultSearch({ data, openNodes, searchTerm, locale, matchSearch })
: [];

return items.map(item => {
Expand Down
3 changes: 2 additions & 1 deletion src/TreeMenu/walk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,5 @@ const generateBranch = ({
return isVisible ? [currentItem, ...nextLevelItems] : nextLevelItems;
};

export default memoize(walk);
export const fastWalk = memoize(walk);
export const slowWalk = walk;

0 comments on commit 2d9990a

Please sign in to comment.