diff --git a/readme.md b/readme.md index 7f255e9..7b21c43 100644 --- a/readme.md +++ b/readme.md @@ -10,18 +10,21 @@ A simple, configurable tree view control for React. Demo: https://codesandbox.io/embed/rrw7mrknyp -* `content`, Name of the node (string or React-component) -* `type`, optional description, good for displaying icons, too (string or React-component) -* `open`, optional: default open state -* `canHide`, optional: when set true displays an eye icon -* `visible`, optional: default visible state -* `onClick`, optional: click events on the eye -* `springConfig`, optional: react-spring animation config +- `content`, Name of the node (string or React-component) +- `type`, optional description, good for displaying icons, too (string or React-component) +- `open`, optional: default open state +- `canHide`, optional: when set true displays an eye icon +- `visible`, optional: default visible state +- `onClick`, optional: click events on the eye +- `springConfig`, optional: react-spring animation config +- `onItemClick`, optional: click events on the tree span item (pass itemId props as parameter) +- `onItemToggle`, optional: click events on the toggle icon, parameters: itemId, isOpen. +- `itemId`, optional: custom identifier of tree item ```jsx import Tree from 'react-animated-tree' - + console.log(itemId)}> diff --git a/src/index.js b/src/index.js index dfd08cf..6e6e1c1 100644 --- a/src/index.js +++ b/src/index.js @@ -9,7 +9,7 @@ const styles = { padding: '4px 0px 0px 0px', textOverflow: 'ellipsis', whiteSpace: 'nowrap', - overflowX: 'hidden', + overflow: 'hidden', verticalAlign: 'middle', }, toggle: { @@ -46,17 +46,29 @@ export default class Tree extends React.PureComponent { visible: PropTypes.bool, canHide: PropTypes.bool, content: PropTypes.node, + itemId: PropTypes.string, springConfig: PropTypes.func, + onItemClick: PropTypes.func, + onItemToggle: PropTypes.func, } constructor(props) { super() - this.state = { open: props.open, visible: props.visible, immediate: false } + this.state = { + open: props.open, + visible: props.visible, + immediate: false, + id: props.itemId, + } } - toggle = () => - this.props.children && - this.setState(state => ({ open: !state.open, immediate: false })) + toggle = () => { + if (typeof this.props.children !== 'undefined') { + this.props.onItemToggle && + this.props.onItemToggle(this.state.id, !this.state.open) + this.setState(state => ({ open: !state.open, immediate: false })) + } + } toggleVisibility = () => { this.setState( @@ -65,6 +77,10 @@ export default class Tree extends React.PureComponent { ) } + onItemClick = () => { + this.props.onItemClick && this.props.onItemClick(this.state.id) + } + componentWillReceiveProps(props) { this.setState(state => { return ['open', 'visible'].reduce( @@ -97,7 +113,9 @@ export default class Tree extends React.PureComponent { onClick={this.toggleVisibility} /> )} - {content} + + {content} + {children}