Skip to content

Commit 0c9efe4

Browse files
martingordonRaphaël Benitte
authored andcommitted
feat(axes): add onClick handler to axis ticks (#60)
* 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
1 parent a547917 commit 0c9efe4

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/components/axes/Axis.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ const Axis = ({
7676
animate,
7777
motionStiffness,
7878
motionDamping,
79+
80+
// interactivity
81+
onClick,
7982
}) => {
8083
const { x, y, ticks, textAlign, textBaseline } = computeAxisTicks({
8184
width,
@@ -152,6 +155,7 @@ const Axis = ({
152155
theme={theme}
153156
x={tick.x}
154157
y={tick.y}
158+
{...(onClick ? { onClick } : {})}
155159
/>
156160
))}
157161
</g>
@@ -193,6 +197,7 @@ const Axis = ({
193197
textBaseline={textBaseline}
194198
textAnchor={textAlign}
195199
theme={theme}
200+
{...(onClick ? { onClick } : {})}
196201
{...style}
197202
/>
198203
))}
@@ -232,6 +237,9 @@ Axis.propTypes = {
232237

233238
// theming
234239
theme: PropTypes.object.isRequired,
240+
241+
// interactivity
242+
onClick: PropTypes.func,
235243
}
236244

237245
Axis.defaultProps = {

src/components/axes/AxisTick.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default class AxisTick extends Component {
3434
format,
3535
lineX,
3636
lineY,
37+
onClick,
3738
textX,
3839
textY,
3940
textBaseline,
@@ -46,8 +47,17 @@ export default class AxisTick extends Component {
4647
value = format(value)
4748
}
4849

50+
let gStyle = { opacity }
51+
if (onClick) {
52+
gStyle['cursor'] = 'pointer'
53+
}
54+
4955
return (
50-
<g transform={`translate(${x},${y})`} style={{ opacity }}>
56+
<g
57+
transform={`translate(${x},${y})`}
58+
{...(onClick ? { onClick: e => onClick(e, value) } : {})}
59+
style={gStyle}
60+
>
5161
<line x1={0} x2={lineX} y1={0} y2={lineY} stroke={theme.axis.tickColor} />
5262
<text
5363
alignmentBaseline={textBaseline}

0 commit comments

Comments
 (0)