Code Monkey home page Code Monkey logo

react-infinity-menu's Introduction

react-infinity-menu

Circle CI

Demo

Awesome demo

Installation

npm install react-infinity-menu

How to use

import InfinityMenu from "react-infinity-menu";
import "react-infinity-menu/src/infinity-menu.css";

class Example extends React.Component {
	onNodeMouseClick(event, tree, node, level, keyPath) {
		this.setState({
			tree: tree
		});
	}

	onLeafMouseClick(event, leaf) {
		console.log( leaf.id ); // Prints the leaf id
		console.log( leaf.name ); // Prints the leaf name
	}

	onLeafMouseUp(event, leaf) {
		console.log( leaf.id ); // Prints the leaf id
		console.log( leaf.name ); // Prints the leaf name
	}

	onLeafMouseDown(event, leaf) {
		console.log( leaf.id ); // Prints the leaf id
		console.log( leaf.name ); // Prints the leaf name
	}

	render() {
		return (
			<InfinityMenu
				tree={this.state.tree}
				onNodeMouseClick={this.onNodeMouseClick}
				onLeafMouseClick={this.onLeafMouseClick}/*optional*/
				onLeafMouseDown={this.onLeafMouseDown}/*optional*/
				onLeafMouseUp={this.onLeafMouseUp}/*optional*/
				maxLeaves={5}/*optional*/
			/>
		);
	}
}

Properties

tree(Array)

A tree to represent the folder structure. Here is the example data you could pass into the tree props.

[
	{
		name: "menu1", /*require*/
		id: 1, /*require*/
		isOpen: true, /*require*/
		customComponent: YOUR_OWN_COMPONENT_FOR_MENU,
		children: [
			{
				name: "submenu1",
				id: 1,
				isOpen: true,
				customComponent: YOUR_OWN_COMPONENT_FOR_SUB_MENU,
				children: [
					{
						name: "item1-1",
						id: 1
					},
					{
						name: "item1-2",
						id: 2
					}
				]
			},
			{
				name: "submenu2",
				id: 2,
				isOpen: true,
				customComponent: YOUR_OWN_COMPONENT_FOR_SUB_MENU,
				children: [
					{
						name: "item2-1",
						id: 1
					}
				]
			}
		]
	},
	{
		name: "menu2", /*require*/
		id: 2, /*require*/
		isOpen: true, /*require*/
		customComponent: YOUR_OWN_COMPONENT_FOR_MENU,
		children: [
			{
				name: "item3-1",
				id: 1
			}
		]
	}
];

disableDefaultHeaderContent (boolean)

A boolean to set if user want to display the default header search input. If user pass in headerContent, the default header will be disabled.

headerContent(React Component)

Subcomponent rendered above the tree.

headerContent is passed in to InfinityMenu. It is rendered above the tree subcomponent.

headerProps(object)

Additional props for headerContent.

  • headerProps is an optional prop of InfinityMenu. The props in this object are passed as props to a headerContent component. This is useful when extending InfinityMenu.

Passing the following into InfinityMenu as the headerProps prop sets the title prop on the headerContent component.

{
	title: "my great title"
}

customComponentMappings (object)

A mapping that let user to pass in customComponent as string.

var myComponentMappings = {
    "MyComponent1": MyComponent1,
    "MyComponent2": MyComponent2
}

customComponent(React Component)

A custom React component the user can pass in.

  • As the customComponent at the node level, you will receive props key, onClick, name, isOpen, data and isSearching.

  • As the customComponent at the leaf level, you will receive props key, onMouseDown, onMouseUp, onClick, name, icon and data.

    Example of customComponent

    class Node extends React.Component {
        render() {
            return (
                <div key={this.props.key} onClick={this.props.onClick}>
                    <label>{this.props.name}</label>
                </div>
            );
        }
    }

filter(function)[node, searchInput]

By default, when the menu is in searching mode, it will filter all nodes by whether their name is equal to the current searchInput. If the node name is equal to the searchInput, then the node will pass the filter and be displayed in tree (if it fails the filter, it will not be displayed in the tree).

This allows the user to specify their own filtering criteria. When the menu is in search mode, every node will be run against the filter() function:

  • If the function returns true, the node will pass the filter, and be displayed in the tree.
  • If the function returns false, the node will fail the filter, and will not be displayed in the tree.

The function takes the following arguments:

  • node (object) is the folder(node) the user clicked on. Includes the following properties: id, name, isOpen and children.
  • searchInput (string) The current search term

emptyTreeComponent (React Component)

If the tree prop is an empty array or if the menu is in searching mode and no nodes match the filter, then the tree is considered "empty".

By default, nothing will be displayed in an empty tree.

However, if this prop is passed in, the specified component will be rendered when the tree is empty.

This allows you have a very customized "empty tree" message/image.

emptyTreeComponentProps (object)

Allows you to specify props to pass to the emptyTreeComponent.

By default, this is an empty object.

onNodeMouseClick(function)[event, tree, node, level, keyPath]

This function will get call when user click on the folder(node). The function arguments include event, tree, node and level.

  • event is the mouse click event.
  • tree is the updated tree, you should update your own tree accordingly.
  • node is the folder(node) the user clicked on. Including the id, name, isOpen and children.
  • level is the distance from the root.
  • keyPath is the path from root to current node

onLeafMouseClick(function)[event, leaf]

Bind to the onClick on the leaf. This function will get call when user click on the item(leaf). The function arguments include event, leaf.

  • event is the click event.
  • leaf is the item user clicked on. Includes name, id, keyPath and all data the user inputs when they pass in the tree.

onLeafMouseDown(function)[event, leaf]

Bind to the onMouseDown on the leaf. This function will get call when user mouse down on the item(leaf). The function arguments include event, leaf.

  • event is the click event.
  • leaf is the item user clicked on. Includes name, id, keyPath and all data the user inputs when they pass in the tree.

onLeafMouseUp(function)[event, leaf]

Bind to the onMouseUp on the leaf. This function will get call when user mouse up on the item(leaf). The function arguments include event, leaf.

  • event is the click event.
  • leaf is the item user clicked on. Includes name, id, keyPath and all data the user inputs when they pass in the tree.

maxLeaves(integer)

Sets the maximum number of leaf to show initially. Also used as an increment when then load more is pressed.

shouldComponentUpdate(function) [currProps, currState, nextProps, nextState]

A function that will be called inside shouldComponentUpdate. It's a good place to optimize update.

Styles

There is a default style sheet you can use if you so desire. /src/infinity-menu.css

License

Copyright (C) 2017 Social Tables, Inc. (https://www.socialtables.com) All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

react-infinity-menu's People

Contributors

conorhastings avatar danmactough avatar ddombrow avatar dianedlee17 avatar jjow avatar joh-truecar avatar johnelliott avatar kylehill avatar rovolution avatar skoschik avatar tiansijie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-infinity-menu's Issues

No newest version (V 3.2.0) found in NPM

I tried to update package to newest version V3.2.0 via NPM, but npm said there is no matched version of 3.2.0, and currently, newest version is V3.1.0. (02/23/2017)

Is there a way to add animation?

Hi, I am currently using this component for a project I am working on and I loved it.

Is there any way to add an animation when the submenu opens, collapse animation perhaps?

Thanks in advance.

Having trouble with search

Hey there,

I really like what you've built here. I am currently using this to create a tree-view of data for an internal application. I am trying to pass "input" as my headerContent, which renders but i'm not sure how to get the search functionality going for this.

This is my render function right now:
render () { return ( <InfinityMenu tree={this.state.tree} headerContent={"input"} headerProps={{className: 'form-control'}} onNodeMouseClick={this.onNodeMouseClick.bind(this)} onLeafMouseClick={this.onLeafMouseClick}/*optional*/ onLeafMouseDown={this.onLeafMouseDown}/*optional*/ onLeafMouseUp={this.onLeafMouseUp}/*optional*/ /> ); }

Thanks in advance for taking a look!

remove full lodash dependency

should be able to only import what's needed, also seems like _.some and _.filter not neccesary, can use native methods

in "onLeafMouseClick", an "data-reactid" of the leaf is passed in instead of the "leaf" object when using customComponent for leaf

Background:

Leaf is using customized component, and in customized component, onClick = { onClick } just do exactly like the doc. (Everything is normal for Node component even if I use customComponent for nodes. )

And in InfinityMenu, this prop is set :

onLeafMouseClick={this.onLeafMouseClick.bind(this)},

However,


onLeafMouseClick(event, leaf) {
  console.log(leaf); // it is a string which is the leaf's data-reactid, not leaf object, but everything works well if I don't use customzied component
}

Is that a bug I guess ?

Custom component as leaf in main menu level

hi,

i am trying to set custom "onClick" function to custom component on first menu level. (He has no parent and no child)

tree: [ { name: Translation.t('sideMenu.sendRecommendation'), id: 0, isOpen: true, customComponent: SideMenuNode, onLeafMouseClick :this.onClick, link: 'forms/recommendation' },]

when i add to this tree object a children:[] (empty array) there is a way to pass onclick function via props?
i check the code and i think there isnt.

when i pass this tree object as leaf at main level he failed when try to search his parent.

from your code:

/*the leaves*/ if (!curr.children) { var keyPathArray = keyPath.split("."); var parentPath = Object.assign([], keyPathArray).splice(0, keyPathArray.length - 2); var parentNode = (0, _get3.default)(this.props.tree, parentPath); var filteredChildren = parentNode.children.some(function (child) { return child.isSearchDisplay === true; }) ? parentNode.children.filter(function (child) { return child.isSearchDisplay === true; }) : parentNode.children; var itemKey = "infinity-menu-leaf-" + curr.id; var visIds = filteredChildren.map(function (e) { return e.id; });

failed here:

var parentNode = (0, _get3.default)(this.props.tree, parentPath); var filteredChildren = parentNode.children.some(function (child)

there is any option to do it?

IE11 compatibility

When using the search function in the tree in IE11, I get an error

return e.name.toLowerCase().includes(t.toLowerCase())
Object doesn't support property or method 'includes'

.includes() should be changed to .indexOf() > 0.

Search functionality not working as expected with numeric values

When I enter a numeric value in the search box, the tree filters and shows correct results but when I erase the search input(numeric value) from search box then it's not showing the original tree instead it's still showing filtered results. Interestingly it's working as expected with alphabets.

Initially, I was using version 3.0.1 later I upgraded to version 3.2.0 but the issue still exists.

Any help greatly appreciated.

too many leaves

Would love a way to set a limit for the maximum number of leaves that should be rendered initially. With the ability to 'load more' or something, nothing fancy haha. Not sure if something like that fits into the scope here but i thought it would be a nice add, really helps if you have a lot of child nodes. Not sure how or if i could tap into doing something like this.

...i guess i could do something with the data i feed into react-infinity-menu through props... but that seems to defeat the purpose.

Any thoughts?

Open Source this

What would it take?

  • rename references to floor elements/objects/etc...
  • probably remove immutable
  • read me
  • demo page.
  • transpile less to a css build.

anything else I might be missing cc @tiansijie

Too much recursion

Passing any react component instances in the tree results in a "Too much recursion" error on Firefox. Similar error on Google Chrome.

        this.state = {
            tree: [
                {
                    name: "controls",
                    id: 0,
                    isOpen: false,
                    customComponent: ControlManage,
                    children: [],
                    controlItems: controlItems
                } ....

This seems to be a problem of the deep-copy library. I tried switching to deepcopy however it seems to somehow change the state of InfinityMenu.

In the render method of InfinityMenu.

assert(this.state == {searching: {...}}
const tree = deepcopy(this.props.tree);
assert(this.state == {}}

Update component while searching

First of all, awesome component! I'm customizing this component to show a leaf as "selected" when it is clicked. I do this by setting an "onClick" function to the leaf which goes and updates the leaf with a customComponent. This work just fine when the component is not in searching mode. When it is in searching mode, my function updates the particular leaf with the customComponent but the changes are not reflected until I clear out my search. I'm not sure how to force the component to update even when it's in searching mode.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.