-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNestedListItem.js
More file actions
43 lines (40 loc) · 1.24 KB
/
NestedListItem.js
File metadata and controls
43 lines (40 loc) · 1.24 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
import React, { Component } from 'react';
export default class NestedListItem extends Component {
constructor(props) {
super(props)
this.state = {
collapsed: true
}
}
clickHandler = () => this.setState({ collapsed: !this.state.collapsed })
render() {
const { data, styles, leftOffset, isFirstRender, createChildrenNodes } = this.props;
const paddingLeft = isFirstRender ? '0px' : `${leftOffset}px`;
// we merge the base styles with increased left padding
let newStyles = Object.assign({}, styles.listItem, { paddingLeft });
return (
<React.Fragment>
<div style={newStyles || {}} onClick={() => this.clickHandler()}>
<div>{data.title ? data.title : `No Title`}</div>
{
data.data &&
<span style={{cursor: 'pointer'}}>
{
this.state.collapsed
? '+'
: '-'
}
</span>
}
</div>
<div>
{
!this.state.collapsed && data.data
? createChildrenNodes(data.data, 2 * leftOffset) // we double left padding on every recursion/depth
: null
}
</div>
</React.Fragment>
)
}
}