forked from dnnsoftware/Dnn.Platform
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFolders.jsx
More file actions
87 lines (74 loc) · 2.57 KB
/
Copy pathFolders.jsx
File metadata and controls
87 lines (74 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React, { Component } from "react";
import PropTypes from "prop-types";
import FolderIcon from "./img/folder.svg";
export default class Folders extends Component {
constructor() {
super();
this.state = {
openFolders: []
};
}
componentDidUpdate(prevProps) {
const { props } = this;
if (!props.folders || !props.folders.children || !props.folders.children[0] || !props.folders.children[0].data || !props.folders.children[0].data.key) {
return;
}
const rootKey = props.folders.children[0].data.key;
let {openFolders} = this.state;
openFolders.push(rootKey);
if (this.state.openFolders !== openFolders) {
this.setState({ openFolders });
}
}
toggleFolder(key) {
let {openFolders} = this.state;
if (openFolders.some(id => id === key)) {
openFolders = openFolders.filter(id => id !== key);
} else {
openFolders.push(key);
}
this.setState({ openFolders });
}
onParentClick(item) {
if (!item.data.hasChildren) {
return;
}
this.toggleFolder(item.data.key);
if (item.children && item.children.length) {
return;
}
this.props.getChildren(item.data.key);
}
onFolderNameClick(folder) {
this.props.onFolderClick(folder.data);
}
getFolders(folder) {
if (!folder) {
return false;
}
const children = folder.children.map((child) => {
const isOpen = this.state.openFolders.some(id => id === child.data.key);
const className = isOpen ? "open" : "";
return <li className={className} key={child.data.key}>
{child.data.hasChildren && <div className="has-children" onClick={this.onParentClick.bind(this, child) }></div>}
<div onClick={this.onFolderNameClick.bind(this, child)}>
<div className="icon"><FolderIcon /></div>
<div className="item-name">{child.data.value}</div>
</div>
{child.data.hasChildren && this.getFolders(child) }
</li>;
});
return <ul>{children}</ul>;
}
render() {
const folders = this.getFolders(this.props.folders);
return <div className="item-picker">
{folders}
</div>;
}
}
Folders.propTypes = {
folders: PropTypes.object.isRequired,
onFolderClick: PropTypes.func.isRequired,
getChildren: PropTypes.func.isRequired
};