Skip to content

Commit

Permalink
feat(interactivity): add onClick support for Sankey (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
bripkens authored and Raphaël Benitte committed Oct 20, 2017
1 parent 5b6f5c8 commit a547917
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/components/charts/sankey/Sankey.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const Sankey = ({

// interactivity
isInteractive,
onClick,
}) => {
const sankey = d3Sankey()
.nodeAlign(sankeyAlignmentFromProp(align))
Expand Down Expand Up @@ -143,6 +144,7 @@ const Sankey = ({
currentNode={currentNode}
currentLink={currentLink}
isCurrentLink={isCurrentLink}
onClick={onClick}
theme={theme}
{...motionProps}
/>
Expand All @@ -160,6 +162,7 @@ const Sankey = ({
currentNode={currentNode}
currentLink={currentLink}
isCurrentNode={isCurrentNode}
onClick={onClick}
theme={theme}
{...motionProps}
/>
Expand Down
4 changes: 4 additions & 0 deletions src/components/charts/sankey/SankeyLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const SankeyLinks = ({
currentNode,
currentLink,
isCurrentLink,
onClick,

theme,
}) => {
Expand All @@ -61,6 +62,7 @@ const SankeyLinks = ({
showTooltip={showTooltip}
hideTooltip={hideTooltip}
setCurrent={setCurrentLink}
onClick={onClick}
theme={theme}
/>
))}
Expand Down Expand Up @@ -93,6 +95,7 @@ const SankeyLinks = ({
showTooltip={showTooltip}
hideTooltip={hideTooltip}
setCurrent={setCurrentLink}
onClick={onClick}
theme={theme}
/>
)}
Expand Down Expand Up @@ -132,6 +135,7 @@ SankeyLinks.propTypes = {
setCurrentLink: PropTypes.func.isRequired,
currentLink: PropTypes.object,
isCurrentLink: PropTypes.func.isRequired,
onClick: PropTypes.func.isRequired,
}

export default pure(SankeyLinks)
6 changes: 6 additions & 0 deletions src/components/charts/sankey/SankeyLinksItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const SankeyLinksItem = ({
handleMouseEnter,
handleMouseMove,
handleMouseLeave,
onClick,
}) => (
<path
fill="none"
Expand All @@ -64,6 +65,7 @@ const SankeyLinksItem = ({
onMouseEnter={handleMouseEnter}
onMouseMove={handleMouseMove}
onMouseLeave={handleMouseLeave}
onClick={onClick}
/>
)

Expand Down Expand Up @@ -91,12 +93,16 @@ SankeyLinksItem.propTypes = {
showTooltip: PropTypes.func.isRequired,
hideTooltip: PropTypes.func.isRequired,
setCurrent: PropTypes.func.isRequired,
onClick: PropTypes.func.isRequired,
}

const enhance = compose(
withPropsOnChange(['link', 'theme'], ({ link, theme }) => ({
tooltip: <BasicTooltip id={<TooltipContent link={link} />} theme={theme} />,
})),
withPropsOnChange(['onClick', 'link'], ({ onClick, link }) => ({
onClick: event => onClick(link, event),
})),
withHandlers({
handleMouseEnter: ({ showTooltip, setCurrent, link, tooltip }) => e => {
setCurrent(link)
Expand Down
4 changes: 4 additions & 0 deletions src/components/charts/sankey/SankeyNodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const SankeyNodes = ({
currentNode,
currentLink,
isCurrentNode,
onClick,

theme,
}) => {
Expand Down Expand Up @@ -63,6 +64,7 @@ const SankeyNodes = ({
showTooltip={showTooltip}
hideTooltip={hideTooltip}
setCurrent={setCurrentNode}
onClick={onClick}
theme={theme}
/>
))}
Expand Down Expand Up @@ -112,6 +114,7 @@ const SankeyNodes = ({
showTooltip={showTooltip}
hideTooltip={hideTooltip}
setCurrent={setCurrentNode}
onClick={onClick}
theme={theme}
/>
)
Expand Down Expand Up @@ -152,6 +155,7 @@ SankeyNodes.propTypes = {
currentNode: PropTypes.object,
currentLink: PropTypes.object,
isCurrentNode: PropTypes.func.isRequired,
onClick: PropTypes.func.isRequired,
}

export default pure(SankeyNodes)
6 changes: 6 additions & 0 deletions src/components/charts/sankey/SankeyNodesItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const SankeyNodesItem = ({
handleMouseEnter,
handleMouseMove,
handleMouseLeave,
onClick,
}) => (
<rect
x={x}
Expand All @@ -46,6 +47,7 @@ const SankeyNodesItem = ({
onMouseEnter={handleMouseEnter}
onMouseMove={handleMouseMove}
onMouseLeave={handleMouseLeave}
onClick={onClick}
/>
)

Expand All @@ -69,6 +71,7 @@ SankeyNodesItem.propTypes = {
showTooltip: PropTypes.func.isRequired,
hideTooltip: PropTypes.func.isRequired,
setCurrent: PropTypes.func.isRequired,
onClick: PropTypes.func.isRequired,

theme: PropTypes.object.isRequired,
}
Expand All @@ -77,6 +80,9 @@ const enhance = compose(
withPropsOnChange(['node', 'theme'], ({ node, theme }) => ({
tooltip: <BasicTooltip id={node.id} enableChip={true} color={node.color} theme={theme} />,
})),
withPropsOnChange(['onClick', 'node'], ({ onClick, node }) => ({
onClick: event => onClick(node, event),
})),
withHandlers({
handleMouseEnter: ({ showTooltip, setCurrent, node, tooltip }) => e => {
setCurrent(node)
Expand Down
3 changes: 3 additions & 0 deletions src/components/charts/sankey/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
import PropTypes from 'prop-types'
import { sankeyAlignmentPropType } from '../../../props'
import noop from '../../../lib/noop'

export const SankeyPropTypes = {
data: PropTypes.shape({
Expand Down Expand Up @@ -52,6 +53,7 @@ export const SankeyPropTypes = {

// interactivity
isInteractive: PropTypes.bool.isRequired,
onClick: PropTypes.func.isRequired,
}

export const SankeyDefaultProps = {
Expand Down Expand Up @@ -82,4 +84,5 @@ export const SankeyDefaultProps = {

// interactivity
isInteractive: true,
onClick: noop,
}
4 changes: 4 additions & 0 deletions stories/charts/sankey.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ stories.add('nodes x padding', () => (
))

stories.add('contracting links', () => <Sankey {...commonProperties} linkContract={10} />)

stories.add('click listener (console)', () => (
<Sankey {...commonProperties} onClick={(data, event) => console.log({ data, event })} />
))

0 comments on commit a547917

Please sign in to comment.