Skip to content
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,5 @@ Here are the props you can pass to `TreeView`.
| expandButtonColor | `string`| optional | `'#4B6DAA'` | The color of the expand button |
| nodeSize| `number`| optional | 20 | The size of the tree leaf icons |
| nodeIcon| `string`| optional | [blue dot] | The node icon. Must be a base64 imported image |
| collapsedIcon| `string`| optional | [+ sign] | The collapsed icon. Must be a base64 imported image. Require to set also the `expandedIcon`. |
| expandedIcon| `string`| optional | [- sign] | The expanded icon. Must be a base64 imported image. Require to set also the `collapsedIcon`. |
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<title>React Expandable TreeView</title>
</head>
<body>
<noscript>
Expand Down
36 changes: 30 additions & 6 deletions src/demo/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React from 'react';
import TreeView from '../lib';
import testData from './testData';
import collapsed from './images/right.png';
import expanded from './images/down.png';
import nodeIcon from './images/github.png';

export default class App extends React.Component {
renderNode(node) {
Expand All @@ -10,12 +13,33 @@ export default class App extends React.Component {
render() {
return (
<div style={{ padding: 20 }}>
<h1>The <i>Felidae</i> family</h1>
<TreeView
data={testData}
renderNode={this.renderNode}
/>
<h1>The <i>Felidae</i> family</h1>
<div style={{ display: 'flex' }}>
<div style={{ width: '50%' }}>
<h3>Basic sample</h3>
<TreeView
data={testData}
renderNode={this.renderNode}
/>
</div>
<div style={{ width: '50%' }}>
<h3>Custom sample</h3>
<TreeView
data={testData}
collapsedIcon={collapsed}
expandedIcon={expanded}
nodeIcon={nodeIcon}
nodeSize={20}
lineColor="crimson"
lineWidth={1}
lineAlpha={1}
expandButtonColor="transparent"
renderNode={this.renderNode}
/>
</div>

</div>
</div>
)
);
}
}
Binary file added src/demo/images/down.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/demo/images/github.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/demo/images/right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/demo/testData.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ const testData = [
}
]
},
]
];

export default testData;
25 changes: 22 additions & 3 deletions src/lib/components/Row.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ const Square = styled.div`
font-weight: bold;
`;

const ExpandCollapse = styled.img`
width: ${(props) => props.nodeSize}px;
height: ${(props) => props.nodeSize}px;
cursor: pointer;
`;

const NodeIcon = styled.img`
width: ${(props) => props.nodeSize}px;
`;
Expand Down Expand Up @@ -85,7 +91,7 @@ const HorizontalLine = styled.div`
const Junction = styled.div`
width: ${(props) => ((props.nodeSize / 2) + (props.lineWidth * 2))}px;
height: ${(props) => (props.nodeSize / 2)}px;
margin-left: ${(props) => ((props.nodeSize / 2) - props.lineWidth)}px;
margin-left: ${(props) => (props.nodeSize / 2)}px;
border-width: 0 0 ${((props) => props.lineWidth)}px ${(props) => props.lineWidth}px;
border-color: ${(props) => props.lineColor};
border-style: ${(props) => props.lineStyle};
Expand All @@ -110,9 +116,18 @@ export default class Row extends React.Component {
lineWidth,
lineColor,
lineStyle,
lineAlpha
lineAlpha,
collapsedIcon,
expandedIcon,
} = this.props;
if (hasChildren) {
if (hasChildren && collapsedIcon && expandedIcon) {
return <ExpandCollapse
alt={isExpanded ? 'collapse' : 'expand'}
src={isExpanded ? expandedIcon : collapsedIcon}
onClick={() => onNodeClick(element.id)}
nodeSize={nodeSize}
/>;
} else if (hasChildren) {
return (
<Square
expandButtonColor={expandButtonColor}
Expand Down Expand Up @@ -154,6 +169,8 @@ export default class Row extends React.Component {
expandButtonColor,
nodeSize,
nodeIcon,
collapsedIcon,
expandedIcon,
} = this.props;
const hasChildren = element.children && element.children.length > 0;
return (
Expand Down Expand Up @@ -218,6 +235,8 @@ export default class Row extends React.Component {
expandButtonColor={expandButtonColor}
nodeSize={nodeSize}
nodeIcon={nodeIcon}
collapsedIcon={collapsedIcon}
expandedIcon={expandedIcon}
/>
)
)}
Expand Down
6 changes: 6 additions & 0 deletions src/lib/components/TreeView.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class TreeView extends React.Component {
expandButtonColor,
nodeSize,
nodeIcon,
collapsedIcon,
expandedIcon,
} = this.props;
const { expandedElements } = this.state;
return (
Expand All @@ -69,6 +71,8 @@ class TreeView extends React.Component {
expandButtonColor={expandButtonColor}
nodeSize={nodeSize}
nodeIcon={nodeIcon}
collapsedIcon={collapsedIcon}
expandedIcon={expandedIcon}
/>
))}
</div>
Expand All @@ -86,6 +90,8 @@ TreeView.propTypes = {
expandButtonColor: PropTypes.string,
nodeSize: PropTypes.number,
nodeIcon: PropTypes.string,
collapsedIcon: PropTypes.string,
expandedIcon: PropTypes.string,
};

TreeView.defaultProps = {
Expand Down