Skip to content

Commit 4e8ec91

Browse files
committed
✔✔Add element to tree
1 parent 5817ff7 commit 4e8ec91

File tree

9 files changed

+777
-45
lines changed

9 files changed

+777
-45
lines changed

package-lock.json

Lines changed: 35 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"@fortawesome/fontawesome-svg-core": "^1.2.19",
7+
"@fortawesome/free-solid-svg-icons": "^5.9.0",
8+
"@fortawesome/react-fontawesome": "^0.1.4",
69
"bootstrap": "^4.3.1",
710
"codemirror": "^5.48.0",
811
"react": "^16.8.6",
912
"react-bootstrap": "^1.0.0-beta.9",
1013
"react-codemirror": "^1.0.0",
1114
"react-codemirror2": "^6.0.0",
1215
"react-dom": "^16.8.6",
16+
"react-modal": "^3.8.2",
1317
"react-router-dom": "^5.0.1",
1418
"react-scripts": "3.0.1",
1519
"react-treebeard": "^3.2.4"

public/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
1111
-->
1212
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
13+
14+
<!--Fontawesome CDN-->
15+
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
1316
<!--
1417
Notice the use of %PUBLIC_URL% in the tags above.
1518
It will be replaced with the URL of the `public` folder during the build.

src/App.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ export default class App extends Component {
2424
</React.Fragment>
2525
);
2626
}
27-
}
28-
27+
}

src/NodeViewer.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
5+
const HELP_MSG = 'Select A Node To See Its Data Structure Here...';
6+
7+
const NodeViewer = ({node}) => {
8+
let json = JSON.stringify(node, null, 4);
9+
10+
if (!json) {
11+
json = HELP_MSG;
12+
}
13+
14+
return <div>{json}</div>;
15+
};
16+
17+
NodeViewer.propTypes = {
18+
node: PropTypes.object
19+
};
20+
21+
export default NodeViewer;

src/components/File.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
3+
const File = (props) => {
4+
return <div>HI {props.name} <span onClick={props.openModal}><i className="far fa-plus-square"/></span></div>;
5+
}
6+
7+
export default File;

src/components/Folder.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
3+
const Folder = (props) => {
4+
return <div>HI {props.name} <span onClick={props.openModal}><i className="far fa-plus-square"/></span></div>;
5+
}
6+
7+
export default Folder;

src/components/Tree.js

Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,49 @@
11
import React, { Component } from 'react'
22
import {Treebeard} from 'react-treebeard';
3+
import Modal from 'react-modal';
4+
import Folder from './Folder'
5+
import File from './File'
6+
7+
const customStyles = {
8+
content : {
9+
top : '50%',
10+
left : '50%',
11+
right : 'auto',
12+
bottom : 'auto',
13+
marginRight : '-50%',
14+
transform : 'translate(-50%, -50%)',
15+
width : '300px',
16+
height : '300px',
17+
}
18+
};
19+
320

421
const data = {
522
name: 'root',
623
toggled: true,
724
children: [
825
{
9-
name: 'parent1',
26+
name: 'App',
1027
children: [
11-
{ name: 'child1' },
12-
{ name: 'child2' }
28+
{ name: 'server.js' },
29+
{ name: 'data.js' }
1330
]
1431
},
1532
{
16-
name: 'teacher1',
17-
// loading: true,
33+
name: 'styles',
1834
children: [
19-
{name: 'student1'},
20-
{name: 'student2'},
35+
{name: 'header.css'},
36+
{name: 'footer.css'},
2137
]
2238
},
2339
{
24-
name: 'parent',
40+
name: 'client',
2541
children: [
2642
{
27-
name: 'nested parent',
43+
name: 'App',
2844
children: [
29-
{ name: 'nested child 1' },
30-
{ name: 'nested child 2' }
45+
{ name: 'index.js' },
46+
{ name: 'context.js' }
3147
]
3248
}
3349
]
@@ -37,9 +53,44 @@ const data = {
3753
export default class Tree extends Component {
3854
constructor(props){
3955
super(props);
40-
this.state = {data};
56+
this.state = {
57+
data,
58+
modalIsOpen: false,
59+
value: "Enter",
60+
activeNode:{}
61+
};
4162
}
42-
63+
64+
openModal = (node)=> {
65+
console.log(node);
66+
this.setState({modalIsOpen: true,activeNode:node});
67+
}
68+
69+
70+
closeModal = ()=> {
71+
this.setState({modalIsOpen: false});
72+
}
73+
74+
75+
componentWillMount = () => {
76+
this.state.data.children.map((child, index)=> {
77+
const name1 = child.name;
78+
child.name = <div>HI {name1} <span onClick={(e)=>this.openModal(child,e)}><i className="far fa-plus-square"/></span></div>;
79+
})
80+
}
81+
82+
handleSubmit = (e) =>{
83+
e.preventDefault();
84+
const name = e.target.value;
85+
this.state.activeNode.children.push({name:this.state.value});
86+
const newData = this.state.data;
87+
this.setState({data: newData});
88+
this.closeModal()
89+
}
90+
handleChange(event) {
91+
this.setState({value: event.target.value});
92+
}
93+
4394
onToggle = (node, toggled) => {
4495
const {cursor, data} = this.state;
4596
if (cursor) {
@@ -52,13 +103,29 @@ export default class Tree extends Component {
52103
this.setState(() => ({cursor: node, data: Object.assign({}, data)}));
53104
}
54105
render() {
55-
const {data} = this.state;
106+
// const {data} = this.state;
56107
return (
57-
<Treebeard
58-
data={data}
59-
onToggle={this.onToggle}
60-
style = {style}
61-
/>
108+
<React.Fragment>
109+
<Modal
110+
isOpen={this.state.modalIsOpen}
111+
onAfterOpen={this.afterOpenModal}
112+
onRequestClose={this.closeModal}
113+
style={customStyles}
114+
contentLabel="Example Modal"
115+
>
116+
<form onSubmit={(e)=>{this.handleSubmit(e)}}>
117+
<input type="text" value={this.state.value} onChange={(e)=>{this.handleChange(e)}} />
118+
<input type="submit" value="ENTER"/>
119+
</form>
120+
121+
</Modal>
122+
<Treebeard
123+
data={this.state.data}
124+
onToggle={this.onToggle}
125+
style = {style}
126+
/>
127+
</React.Fragment>
128+
62129
)
63130
}
64131
}

0 commit comments

Comments
 (0)