Skip to content

Commit

Permalink
feat(axes): add onClick handler to axis ticks (#60)
Browse files Browse the repository at this point in the history
* feat(axes): add onClick handler to axis ticks

* feat(axes): only include onClick prop if present

* fix(axes): just pass through onClick function

* fix(axes): pass event to onClick handler
  • Loading branch information
martingordon authored and Raphaël Benitte committed Oct 20, 2017
1 parent a547917 commit 0c9efe4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/components/axes/Axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ const Axis = ({
animate,
motionStiffness,
motionDamping,

// interactivity
onClick,
}) => {
const { x, y, ticks, textAlign, textBaseline } = computeAxisTicks({
width,
Expand Down Expand Up @@ -152,6 +155,7 @@ const Axis = ({
theme={theme}
x={tick.x}
y={tick.y}
{...(onClick ? { onClick } : {})}
/>
))}
</g>
Expand Down Expand Up @@ -193,6 +197,7 @@ const Axis = ({
textBaseline={textBaseline}
textAnchor={textAlign}
theme={theme}
{...(onClick ? { onClick } : {})}
{...style}
/>
))}
Expand Down Expand Up @@ -232,6 +237,9 @@ Axis.propTypes = {

// theming
theme: PropTypes.object.isRequired,

// interactivity
onClick: PropTypes.func,
}

Axis.defaultProps = {
Expand Down
12 changes: 11 additions & 1 deletion src/components/axes/AxisTick.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default class AxisTick extends Component {
format,
lineX,
lineY,
onClick,
textX,
textY,
textBaseline,
Expand All @@ -46,8 +47,17 @@ export default class AxisTick extends Component {
value = format(value)
}

let gStyle = { opacity }
if (onClick) {
gStyle['cursor'] = 'pointer'
}

return (
<g transform={`translate(${x},${y})`} style={{ opacity }}>
<g
transform={`translate(${x},${y})`}
{...(onClick ? { onClick: e => onClick(e, value) } : {})}
style={gStyle}
>
<line x1={0} x2={lineX} y1={0} y2={lineY} stroke={theme.axis.tickColor} />
<text
alignmentBaseline={textBaseline}
Expand Down

0 comments on commit 0c9efe4

Please sign in to comment.