-
Notifications
You must be signed in to change notification settings - Fork 295
feat: Separating header click action from toggle click action. #229
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,13 @@ import PropTypes from 'prop-types'; | |
import {Div} from '../src/components/common'; | ||
|
||
// Example: Customising The Header Decorator To Include Icons | ||
const Header = ({style, node}) => { | ||
const Header = ({style, node, onClick}) => { | ||
const iconType = node.children ? 'folder' : 'file-text'; | ||
const iconClass = `fa fa-${iconType}`; | ||
const iconStyle = {marginRight: '5px'}; | ||
|
||
return ( | ||
<Div style={style.base}> | ||
<Div style={style.base} onClick={() => onClick()}> | ||
<Div style={style.title}> | ||
<i className={iconClass} style={iconStyle}/> | ||
|
||
|
@@ -23,6 +23,7 @@ const Header = ({style, node}) => { | |
Header.propTypes = { | ||
node: PropTypes.object, | ||
style: PropTypes.object, | ||
onClick: PropTypes.func, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comma |
||
}; | ||
|
||
export default Header; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,19 +21,22 @@ class Container extends PureComponent { | |
} | ||
|
||
renderToggleDecorator() { | ||
const {style, decorators} = this.props; | ||
return <decorators.Toggle style={style.toggle}/>; | ||
const {style, decorators, onClickToggle} = this.props; | ||
return <decorators.Toggle style={style.toggle} onClick={onClickToggle ? () => onClickToggle() : null}/>; | ||
} | ||
|
||
render() { | ||
const {style, decorators, terminal, onClick, node} = this.props; | ||
const {style, decorators, terminal, onClick, onClickHeader, node} = this.props; | ||
return ( | ||
<div | ||
onClick={onClick} | ||
onClick={onClick ? () => onClick() : null} | ||
style={node.active ? {...style.container} : {...style.link}} | ||
> | ||
{!terminal ? this.renderToggle() : null} | ||
<decorators.Header node={node} style={style.header}/> | ||
<decorators.Header | ||
node={node} | ||
style={style.header} | ||
onClick={onClickHeader} /> | ||
</div> | ||
); | ||
} | ||
|
@@ -43,12 +46,14 @@ Container.propTypes = { | |
style: PropTypes.object.isRequired, | ||
decorators: PropTypes.object.isRequired, | ||
terminal: PropTypes.bool.isRequired, | ||
onClick: PropTypes.func.isRequired, | ||
onClick: PropTypes.func, | ||
onClickToggle: PropTypes.func, | ||
onClickHeader: PropTypes.func, | ||
animations: PropTypes.oneOfType([ | ||
PropTypes.object, | ||
PropTypes.bool | ||
PropTypes.bool, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comma |
||
]).isRequired, | ||
node: PropTypes.object.isRequired | ||
node: PropTypes.object.isRequired, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comma |
||
}; | ||
|
||
export default Container; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,8 @@ import PropTypes from 'prop-types'; | |
|
||
import {Div} from '../common'; | ||
|
||
const Header = ({node, style}) => ( | ||
<Div style={style.base}> | ||
const Header = ({node, style, onClick}) => ( | ||
<Div style={style.base} onClick={onClick ? () => onClick() : null}> | ||
<Div style={style.title}> | ||
{node.name} | ||
</Div> | ||
|
@@ -13,7 +13,8 @@ const Header = ({node, style}) => ( | |
|
||
Header.propTypes = { | ||
style: PropTypes.object, | ||
node: PropTypes.object.isRequired | ||
node: PropTypes.object.isRequired, | ||
onClick: PropTypes.func, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comma |
||
}; | ||
|
||
export default Header; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ class NodeHeader extends Component { | |
} | ||
|
||
render() { | ||
const {animations, decorators, node, onClick, style} = this.props; | ||
const {animations, decorators, node, onClick, onClickToggle, onClickHeader, style} = this.props; | ||
const {active, children} = node; | ||
const terminal = !children; | ||
let styles; | ||
|
@@ -35,7 +35,7 @@ class NodeHeader extends Component { | |
} | ||
return ( | ||
<decorators.Container | ||
{...{animations, decorators, node, onClick, terminal}} | ||
{...{animations, decorators, node, onClick, onClickToggle, onClickHeader, terminal}} | ||
style={styles} | ||
/> | ||
); | ||
|
@@ -50,7 +50,9 @@ NodeHeader.propTypes = { | |
PropTypes.bool | ||
]).isRequired, | ||
node: PropTypes.object.isRequired, | ||
onClick: PropTypes.func | ||
onClick: PropTypes.func, | ||
onClickToggle: PropTypes.func, | ||
onClickHeader: PropTypes.func, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comma |
||
}; | ||
|
||
export default NodeHeader; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,18 @@ import Drawer from './Drawer'; | |
import Loading from './Loading'; | ||
|
||
const Li = styled('li', { | ||
shouldForwardProp: prop => ['className', 'children', 'ref'].indexOf(prop) !== -1 | ||
shouldForwardProp: prop => ['className', 'children', 'ref'].indexOf(prop) !== -1, | ||
})(({style}) => style); | ||
|
||
class TreeNode extends PureComponent { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove blank line |
||
constructor() { | ||
super(); | ||
this.onClick = this.onClick.bind(this); | ||
this.onClickToggle = this.onClickToggle.bind(this); | ||
this.onClickHeader = this.onClickHeader.bind(this); | ||
} | ||
|
||
onClick() { | ||
const {node, onToggle} = this.props; | ||
const {toggled} = node; | ||
|
@@ -23,17 +31,32 @@ class TreeNode extends PureComponent { | |
} | ||
} | ||
|
||
onClickToggle() { | ||
const {node, onToggle} = this.props; | ||
if (onToggle) { | ||
const {toggled} = node; | ||
onToggle(node, !toggled); | ||
} | ||
} | ||
|
||
onClickHeader() { | ||
const {node, onClickHeader} = this.props; | ||
if (onClickHeader) { | ||
onClickHeader(node); | ||
} | ||
} | ||
|
||
animations() { | ||
const {animations, node} = this.props; | ||
if (!animations) { | ||
return { | ||
toggle: defaultAnimations.toggle(this.props, 0) | ||
toggle: defaultAnimations.toggle(this.props, 0), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comma |
||
}; | ||
} | ||
const animation = Object.assign({}, animations, node.animations); | ||
return { | ||
toggle: animation.toggle(this.props), | ||
drawer: animation.drawer(this.props) | ||
drawer: animation.drawer(this.props), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comma |
||
}; | ||
} | ||
|
||
|
@@ -45,7 +68,14 @@ class TreeNode extends PureComponent { | |
} | ||
|
||
renderChildren(decorators) { | ||
const {animations, decorators: propDecorators, node, style, onToggle} = this.props; | ||
const { | ||
animations, decorators: propDecorators, | ||
node, | ||
style, | ||
separateToggleEvent, | ||
onToggle, | ||
onClickHeader, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comma |
||
} = this.props; | ||
|
||
if (node.loading) { | ||
return ( | ||
|
@@ -62,7 +92,7 @@ class TreeNode extends PureComponent { | |
<Ul style={style.subtree}> | ||
{children.map(child => ( | ||
<TreeNode | ||
{...{onToggle, animations, style}} | ||
{...{separateToggleEvent, onToggle, onClickHeader, animations, style}} | ||
decorators={propDecorators} | ||
key={child.id || randomString()} | ||
node={child} | ||
|
@@ -73,13 +103,18 @@ class TreeNode extends PureComponent { | |
} | ||
|
||
render() { | ||
const {node, style} = this.props; | ||
const {node, style, separateToggleEvent} = this.props; | ||
const decorators = this.decorators(); | ||
const animations = this.animations(); | ||
const {...restAnimationInfo} = animations.drawer; | ||
return ( | ||
<Li style={style.base}> | ||
<NodeHeader {...{decorators, animations, node, style}} onClick={() => this.onClick()}/> | ||
<NodeHeader | ||
{...{decorators, animations, node, style}} | ||
onClick={separateToggleEvent ? null : () => this.onClick()} | ||
onClickHeader={separateToggleEvent ? () => this.onClickHeader() : null} | ||
onClickToggle={separateToggleEvent ? () => this.onClickToggle() : null} | ||
/> | ||
<Drawer restAnimationInfo={{...restAnimationInfo}}> | ||
{node.toggled ? this.renderChildren(decorators, animations) : null} | ||
</Drawer> | ||
|
@@ -89,14 +124,16 @@ class TreeNode extends PureComponent { | |
} | ||
|
||
TreeNode.propTypes = { | ||
separateToggleEvent: PropTypes.bool, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you have props that are not required, you should add |
||
onToggle: PropTypes.func, | ||
onClickHeader: PropTypes.func, | ||
style: PropTypes.object.isRequired, | ||
node: PropTypes.object.isRequired, | ||
decorators: PropTypes.object.isRequired, | ||
animations: PropTypes.oneOfType([ | ||
PropTypes.object, | ||
PropTypes.bool | ||
]).isRequired | ||
PropTypes.bool, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comma |
||
]).isRequired, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comma |
||
}; | ||
|
||
export default TreeNode; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,11 @@ import {Ul} from './common'; | |
import defaultDecorators from './Decorators'; | ||
import TreeNode from './TreeNode'; | ||
|
||
const TreeBeard = ({animations, decorators, data, onToggle, style}) => ( | ||
const TreeBeard = ({animations, decorators, data, separateToggleEvent, onToggle, onClickHeader, style}) => ( | ||
<Ul style={{...defaultTheme.tree.base, ...style.tree.base}}> | ||
{castArray(data).map(node => ( | ||
<TreeNode | ||
{...{decorators, node, onToggle, animations}} | ||
{...{decorators, node, separateToggleEvent, onToggle, onClickHeader, animations}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please create unit test when you press |
||
key={node.id || randomString()} | ||
style={{...defaultTheme.tree.node, ...style.tree.node}} | ||
/> | ||
|
@@ -31,14 +31,16 @@ TreeBeard.propTypes = { | |
PropTypes.object, | ||
PropTypes.bool | ||
]), | ||
separateToggleEvent: PropTypes.bool, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add to defaultProps |
||
onToggle: PropTypes.func, | ||
onClickHeader: PropTypes.func, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add to defaultProps |
||
decorators: PropTypes.object | ||
}; | ||
|
||
TreeBeard.defaultProps = { | ||
style: defaultTheme, | ||
animations: defaultAnimations, | ||
decorators: defaultDecorators | ||
decorators: defaultDecorators, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comma |
||
}; | ||
|
||
export default TreeBeard; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do onClick like this:
onClick={onClick}