Skip to content

Commit

Permalink
Merge pull request #1 from iLoveCodingOrg/chore/examples
Browse files Browse the repository at this point in the history
Examples added, readme improved
  • Loading branch information
azizali committed Nov 15, 2017
2 parents 6a2f366 + b9696f3 commit 30e1376
Show file tree
Hide file tree
Showing 14 changed files with 1,037 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -43,4 +43,5 @@ cfg/secrets.js
.DS_Store
*.tgz

dist/
dist
dist-examples
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Aziz Ali

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
135 changes: 135 additions & 0 deletions examples/Async.js
@@ -0,0 +1,135 @@
import React, { Component } from 'react';
import ExpandableTree from '../dist/main.js';
import { cloneDeep, find } from 'lodash';
import '../dist/style.css';

export default class extends Component {
constructor (){
super();

this.state = {
data: [
{
id: 1,
name: 'Parent A'
}
]
}
}

render(){
const { state, setState } = this;
return (
<div>
<h2>Load Data Asynchronously</h2>
<p>You can incrementally and asynchronously load data for child nodes.</p>
<p>You can trigger an API call on any event (expand, check, delete), and update the <code>data</code> prop when your API resolves.</p>
<p>To show the loading sign, set the <code>isChildrenLoading</code> value to <code>true</code> of the parent node whose chilren are being loaded</p>

<p>These are the options:</p>
<ol>
<li><code>loadingElement</code> Provide your own loading indicator. Default <code>&lt;div&gt;loading...&lt;/div&gt;</code></li>
</ol>

<hr/>
<div className="row">
<div className="col-xs-12 col-lg-8 col-md-8">
<div className="panel panel-default">
<div className="panel-body">
<ExpandableTree
data={ this.state.data }
onUpdateCb={(updatedData)=>{
this.setState({data: updatedData});
}}
isExpandable={(node, depth)=>{ return (depth===0)? true : false; }}
onExpandToggleCb={(node, depth)=>{
// This will show the loading sign
node.isChildrenLoading = true;

setTimeout(()=>{
const updatedData = cloneDeep(state.data);

// Remove loading sign
updatedData[0].isChildrenLoading = false;

// Make sure node is expanded
updatedData[0].isExpanded = true;

// Set Children data that you potentially
// got from an API response
updatedData[0].children = [
{
id: 22,
name: 'Child 1'
},
{
id: 23,
name: 'Child 2'
}
];

// Update state
this.setState({data: updatedData})

}, 1700);
}}
/>
</div>
</div>
</div>
</div>

<h5>Source code:</h5>
<pre>
{`this.state = {
data: [
{
id: 1,
name: 'Parent A'
}
]
}
<ExpandableTree
data={ this.state.data }
onUpdateCb={(updatedData)=>{
this.setState({data: updatedData});
}}
isExpandable={(node, depth)=>{ return (depth===0)? true : false; }}
onExpandToggleCb={(node, depth)=>{
// This will show the loading sign
node.isChildrenLoading = true;
setTimeout(()=>{
const updatedData = cloneDeep(state.data);
// Remove loading sign
updatedData[0].isChildrenLoading = false;
// Make sure node is expanded
updatedData[0].isExpanded = true;
// Set Children data that you potentially
// got from an API response
updatedData[0].children = [
{
id: 22,
name: 'Child 1'
},
{
id: 23,
name: 'Child 2'
}
];
// Update state
this.setState({data: updatedData})
}, 1700);
}}
/>`}
</pre>
</div>
)
}
}
120 changes: 120 additions & 0 deletions examples/Basic.js
@@ -0,0 +1,120 @@
import React, { Component } from 'react';
import ExpandableTree from '../dist/main.js';
import '../dist/style.css';

export default class extends Component {
constructor (){
super();

this.state = {
data: [
{
id: 1,
name: 'Parent A'
},
{
id: 2,
name: 'Parent B',
isExpanded: true,
isChecked: true,
children: [
{
id: 21,
name: 'Child 1',
children: [
{
id: 5,
name: "Grand Child"
}
]
},
{
id: 22,
name: 'Child 2'
},
{
id: 23,
name: 'Child 3'
},
{
id: 24,
name: 'Child 4'
}
]
}
]
}
}

render(){
return (
<div>
<h2>Basic</h2>
<hr/>
<div className="row">
<div className="col-xs-12 col-lg-8 col-md-8">
<div className="panel panel-default">
<div className="panel-body">
<ExpandableTree
data={ this.state.data }
onUpdateCb={(updatedData)=>{
this.setState({data: updatedData})
}}
/>
</div>
</div>
</div>
</div>

<h5>Source code:</h5>
<pre>
{`this.state = {
data: [
{
id: 1,
name: 'Parent A'
},
{
id: 2,
name: 'Parent B',
isExpanded: true,
isChecked: true,
children: [
{
id: 21,
name: 'Child 1',
children: [
{
id: 5,
name: "Grand Child"
}
]
},
{
id: 22,
name: 'Child 2'
},
{
id: 23,
name: 'Child 3'
},
{
id: 24,
name: 'Child 4'
}
]
}
]
}
<ExpandableTree
data={ this.state.data }
onUpdateCb={(updatedData)=>{
this.setState({data: updatedData})
}}
/>`}
</pre>
</div>
)
}
}

0 comments on commit 30e1376

Please sign in to comment.