Skip to content

Update README file #147

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 3 commits into from
Jan 12, 2021
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
225 changes: 216 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
# React Tress
# React Tree

[![Build Status](https://travis-ci.org/kenshoo/react-tree.svg?branch=master)](https://travis-ci.org/kenshoo/react-tree)
[![Test Coverage](https://api.codeclimate.com/v1/badges/7b44acc9042c5ee410a8/test_coverage)](https://codeclimate.com/github/kenshoo/react-tree/test_coverage)

Code usage:
React Tree is a straight forward component that allows a user to display and manage a hierarchical structure of items in a clear and comfortable way.

## Optional Themes

Two optional themes are supported when using React Tree:
- Basic tree: using @emotion/core.
- Material tree: based on the basic tree logic, using Material-UI components - https://github.com/kenshoo/react-tree/blob/master/packages/material_tree/README.md

Both options support component customization.

Examples - https://github.com/kenshoo/react-tree/blob/master/packages/docs/stories/core.stories.js

## Basic tree

<p align="center">
<img src="core-tree-demo.gif?raw=true" width="400" />
</p>

### Installation

**Installation using npm:**
```
npm install @kenshooui/react-tree --save
```
**Installation using Yarn:**

```
yarn add @kenshooui/react-tree
```

### How to use
<!-- example -->

```jsx
Expand All @@ -31,12 +59,191 @@ const structure = [
/>;
```

## Props
### Props

| Name | Type | Default | Description |
| :------------------ | :---------- | :--------------------- | :-------------------------------------------------------------- |
| `structure` | `Array` | `[]` | `Component input - array of leaves along with their ancestors` |
| `title` | `String` | `""` | `Title to be displayed on root mode` |
| `onSelect` | `Func` | `() => {}` | `callback when clicking a leaf` |
| `styles` | `Object` | | `Optional - enables customized styles` |
| `width` | `number` | `230 ` | `The width of the tree component` |
| `height` | `number` | `300 ` | `The height of the tree component` |
| `noResultsText` | `String` | `No matching results` | `The message to be displayed when having no results on searching` |
| `noResultsRenderer` | `Component` | `no_matching_items.js` | `Component to replace the default NoResults component. ` |
| `noResultsIconRenderer`| `Component` | | `Component to replace the default NoResultsIcon component.` |
| `headerRenderer` | `Component` | `header.js` | `Component to replace the default Header component.` |
| `backIconRenderer` | `Component` | | `Component to replace the default BackIcon component. ` |
| `inputRenderer` | `Component` | `input.js️` | `Component to replace the default Input component. ` |
| `inputIconRenderer` | `Component` | | `Component to replace the default InputIcon component. ` |
| `clearIconRenderer` | `Component` | | `Component to replace the default CleaseIcon component. ` |
| `itemRenderer` | `Component` | `item.js️` | `Component to replace the default Item component. ` |
| `itemsRenderer` | `Component` | `items.js` | `Component to replace the default Items component. ` |
| `forwardIconRenderer` | `Component` | | `Component to replace the default ForwardIcon component. ` |
| `treeContainerRenderer`| `Component` | `tree_container.js` | `Component to replace the default TreeContainer component. ` |
| `markSelectedItem` | `boolean` | `false` | `Toggle to mark selected item. ` |

<br/>

### Customization

#### Styling

The basic tree gets "styles" object property.
<br/>
If the "styles" object is empty, the basic tree will use the default styles.
<br/>
The "styles" object can override any of the following:
- container - tree container
- header - tree header. Displays tree title or item's parents
- headerBackIcon - back icon
- item - a single item from the hierarchical tree.
- highlight - the style of the searched (highlighted) letters of an item
- searchItemInitial: the style of the basic ("not searched") letters of an item
- parents - the style of parents sub-title on search
- items - items list container
- noResults - displayed when there are no found items
- noResultsIcon - the icon displayed when there are no found items
- noResultsText - the massage displayed when there are no found items
- input - search input
- searchInput - the icon of the search input
- clearInput - the icon of the search input "clear" button
- forwardIcon - the icon that is part of the item component. Displayed when the item has children.
- selectedItem - the style of the selectedItem item
- inputWrapper - the style of input wrapper

<br/>

#### Renderers

You can replace the renderers of the following components:

<br/>

**Container**

Use the `treeContainerRenderer` to replace the default component.

Each treeContainer receives the following props:

`containerRef` - Holds a reference to the tree container component

`children` - Holds all sub components (like header, input, items, etc..)

`getStyles` - Gets relevant styles

`styles` - Enables using customized styles

<br/>

**Header**

Use the `headerRenderer` to replace the default component.

Each header receives the following props:

`headerRef` - Holds a reference to the header component

`parents` - Holds the parents of the current level.
<br/>
For example for the following structure: ["Profiles", "Performance", "Clicks"]
- In the first level the parents are: [""]
- In the second level the parents are: ["Profile"]
- In the third level the parents are: ["Profile, "Performance"]


`onClick` - Triggers the back event on click

`title` - The title of the header. Displayed on the first level.

`getStyles` - Gets relevant styles

`styles` - Enables using customized styles

`backIconRenderer` - Use the `backIconRenderer` to replace the default back button component.

<br/>

**Input**

Use the `inputRenderer` to replace the default component.

Each header receives the following props:

`inputRef` - Holds a reference to the input component

`searchTerm` - Holds the searched value

`onInputChange` - Triggers set searchTerm event on change

`getStyles` - Gets relevant styles

`styles` - Enables using customized styles

`inputIconRenderer` - Use the `inputIconRenderer` to replace the default input icon component.

`clearIconRenderer` - Use the `clearIconRenderer` to replace the default clear input icon component.

<br/>

**Items**

Use the `itemsRenderer` to replace the default component.

Each header receives the following props:

`getStyles` - Gets relevant styles

`styles` - Enables using customized styles

`children` - All items

<br/>

**Item**

Use the `itemRenderer` to replace the default component

Each header receives the following props:

`getStyles` - Gets relevant styles

`styles` - Enables using customized styles

`searchTerm` - Holds the searched value

`item` - Represents an item from the given structure

`onClick` - Is called when clicking on an item

`forwardIconRenderer` - Use the `forwardIconRenderer` to replace the default forward icon component

`selectedItem` - Represents the current selected item. Is relevant when markSelectedItem = true

<br/>

**No Results**

Use the `noResultsRenderer` to replace the default component.

`getStyles` - Gets relevant styles

`styles` - Enables using customized styles

`text` - Displayed when there are no results

`noResultsIconRenderer` - Use the `noResultsIconRenderer` to replace the default no results warning icon component.


## How to Contribute

#### Setting up development environment

1. Fork the repository and create your branch from `master`.
2. Install the project: `yarn install`
3. Run tests: `yarn test` or `yarn test:watch`
4. Run dev environment: `yarn storybook` and head to [https://localhost:6006](https://localhost:6006)

| Name | Type | Default | Description |
| :------------------ | :-------- | :--------------------- | :-------------------------------------------------------------- |
| `structure` | `Array` | `[]` | `Component input - array of leaves along with their ancestors` |
| `title` | `String` | `` | `Title to be displayed on root mode` |
| `onSelect` | `Func` | `` | `callback when clicking a leaf` |
| `NoResultsRenderer` | `` | `no_matching_items.js` | `renderer when having no results on searching` |
## Credit

Styling patterns were taken from react-select - https://github.com/JedWatson/react-select/blob/master/README.md
Binary file added core-tree-demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion packages/core/src/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const Tree = props => {
onSelect,
width,
height,
styles,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liorheber In the previous PRs I removed "styles" and "classname" props bacuse I thought They are not in use.
Noe when writing the README I understood the "styles" is needed for customized styles.
What about calssname? Do we need this property?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which classNames? Can you link the change?
You would usually need the className prop to customize using almost any styling library - css modules, styled-components etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are for customization, however, I'm not sure it was fully implemented.
I suggest returning it and we can have a look at it at a later task when we decide to support that type of customization.

noResultsText = "No matching results",
headerRenderer: Header = HeaderDefault,
backIconRenderer,
Expand Down Expand Up @@ -66,6 +67,7 @@ const Tree = props => {
<TreeContainer
containerRef={containerRef}
getStyles={getStyles}
styles={styles}
width={(width || DEFAULT_WIDTH) + PIXEL_SUFFIX}
height={(height || DEFAULT_HEIGHT) + PIXEL_SUFFIX}
>
Expand All @@ -75,18 +77,20 @@ const Tree = props => {
title={title}
onClick={onBackClick}
getStyles={getStyles}
styles={styles}
backIconRenderer={backIconRenderer}
>
{title}
</Header>
<Input
inputRef={inputRef}
getStyles={getStyles}
styles={styles}
searchTerm={searchTerm}
onInputChange={onInputChange}
inputIconRenderer={inputIconRenderer}
/>
<Items getStyles={getStyles} height={itemsHeight}>
<Items styles={styles} getStyles={getStyles} height={itemsHeight}>
{leaves &&
leaves.length > 0 &&
leaves.map(item => (
Expand All @@ -104,6 +108,7 @@ const Tree = props => {
height={itemsHeight}
text={noResultsText}
getStyles={getStyles}
styles={styles}
noResultsIconRenderer={noResultsIconRenderer}
/>
)}
Expand Down
41 changes: 39 additions & 2 deletions packages/docs/stories/core.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import React from "react";
import Tree from "@kenshooui/react-tree";
import MaterialTree from "@kenshooui/material-tree";
import makeStyles from "@material-ui/core/styles/makeStyles";
import {
CustomForwardIcon,
CustomHeader,
CustomSearchIcon,
customStyles
} from "./custom_renderers";

const useStyles = makeStyles({
wrapper: {
Expand Down Expand Up @@ -57,13 +63,33 @@ export const Basic = () => {
return (
<div className={classes.wrapper}>
<div className={classes.item}>
<div className={classes.title}>Default Dimensions</div>
<div className={classes.title}>Default Tree</div>
<Tree
structure={structure}
title={"Choose an item"}
onSelect={item => alert(item)}
/>
</div>
<div className={classes.item}>
<div className={classes.title}>Custom Renderers</div>
<Tree
structure={structure}
title={"Choose an item"}
onSelect={item => alert(item)}
headerRenderer={CustomHeader}
forwardIconRenderer={CustomForwardIcon}
inputIconRenderer={CustomSearchIcon}
/>
</div>
<div className={classes.item}>
<div className={classes.title}>Custom Styles</div>
<Tree
structure={structure}
title={"Choose an item"}
onSelect={item => alert(item)}
styles={customStyles}
/>
</div>
<div className={classes.item}>
<div className={classes.title}>Custom Dimensions</div>
<Tree
Expand All @@ -83,11 +109,22 @@ export const MaterialTheme = () => {
return (
<div className={classes.wrapper}>
<div className={classes.item}>
<div className={classes.title}>Default Dimensions</div>
<div className={classes.title}>Default Tree</div>
<MaterialTree
structure={structure}
title={"Choose an item"}
onSelect={item => alert(item)}
/>
</div>
<div className={classes.item}>
<div className={classes.title}>Custom Renderers</div>
<MaterialTree
structure={structure}
title={"Choose an item"}
onSelect={item => alert(item)}
headerRenderer={CustomHeader}
forwardIconRenderer={CustomForwardIcon}
inputIconRenderer={CustomSearchIcon}
/>
</div>
<div className={classes.item}>
Expand Down
Loading