From 0a3d9ec27f9750d2783b139c4678a9c4c0aed4f1 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 6 Feb 2019 22:17:03 +0200 Subject: [PATCH] Create TreeView.md --- snippets/TreeView.md | 126 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 snippets/TreeView.md diff --git a/snippets/TreeView.md b/snippets/TreeView.md new file mode 100644 index 000000000..de2e1bf52 --- /dev/null +++ b/snippets/TreeView.md @@ -0,0 +1,126 @@ +### TreeView + +Renders a tree view of a JSON object or array with collapsible content. + +Use `defaultProps` to set the default values of certain props. +Use the value of the `toggled` prop to determine the initial state of the content (collapsed/expanded). +Set the `state` of the component to the value of the `toggled` prop and bind the `toggle` method to the component's context. +Create a method, `toggle`, which uses `Component.prototype.setState` to change the component's `state` from collapsed to expanded and vice versa. +In the `render()` method, use a `
` to wrap the contents of the component and the `` element, used to alter the component's `state`. +Determine the appearance of the component, based on `this.props.isParentToggled`, `this.state.toggled`, `this.props.name` and `Array.isArray()` on `this.props.data`. +For each child in `this.props.data`, determine if it is an object or array and recursively render a sub-tree. +Otherwise, render a `

` element with the appropriate style. + +```css +.tree-element { + margin: 0; + position: relative; +} + +div.tree-element:before { + content: ''; + position: absolute; + top: 24px; + left: 1px; + height: calc(100% - 48px); + border-left: 1px solid gray; +} + +.toggler { + position: absolute; + top: 10px; + left: 0px; + width: 0; + height: 0; + border-top: 4px solid transparent; + border-bottom: 4px solid transparent; + border-left: 5px solid gray; + cursor: pointer; +} + +.toggler.closed { + transform: rotate(90deg); +} + +.collapsed { + display: none; +} +``` + +```jsx +class TreeView extends React.Component { + constructor(props) { + super(props); + this.state = { + toggled: props.toggled + } + this.toggle = this.toggle.bind(this); + } + + toggle() { + this.setState(state => ({ toggled: !state.toggled })); + } + + render() { + return ( +

+ + {this.props.name ?   {this.props.name}: :   } + {Array.isArray(this.props.data) ? '[' : '{'} + {!this.state.toggled && '...'} + {Object.keys(this.props.data).map( + (v, i, a) => + typeof this.props.data[v] == "object" ? ( + + ) : ( +

+ {Array.isArray(this.props.data) ? '' : {v}: } + {this.props.data[v]}{i === a.length - 1 ? '' : ','} +

+ ) + )} + {Array.isArray(this.props.data) ? ']' : '}'} + {!this.props.isLast ? ',' : ''} +
+ ) + } +} + +TreeView.defaultProps = { + isLast: true, + toggled: true, + name: null, + isChildElement: false, + isParentToggled: true +} +``` + +```jsx +let data = { + lorem: { + ipsum: "dolor sit", + amet: { + consectetur: "adipiscing", + elit: [ + "duis", + "vitae", + { + semper: "orci" + }, + { + est: "sed ornare" + }, + "etiam", + ["laoreet", "tincidunt"], + ["vestibulum", "ante"] + ] + }, + ipsum: "primis" + } +}; +ReactDOM.render(, document.getElementById("root")); +``` + + + +