Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(line): add support for onSliceClick #2187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions packages/line/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,27 @@ declare module '@nivo/line' {

export type PointMouseHandler = (point: Point, event: React.MouseEvent) => void

export type SliceMouseHandler = (slice: Slice, event: React.MouseEvent) => void

export interface PointTooltipProps {
point: Point
}
export type PointTooltip = React.FunctionComponent<PointTooltipProps>

export type Slice = {
id: DatumValue
height: number
width: number
x0: number
x: number
y0: number
y: number
points: Point[]
}

export interface SliceTooltipProps {
axis: 'x' | 'y'
slice: {
id: DatumValue
height: number
width: number
x0: number
x: number
y0: number
y: number
points: Point[]
}
slice: Slice
}
export type SliceTooltip = React.FunctionComponent<SliceTooltipProps>

Expand Down Expand Up @@ -186,6 +190,7 @@ declare module '@nivo/line' {
onMouseMove?: PointMouseHandler
onMouseLeave?: PointMouseHandler
onClick?: PointMouseHandler
onSliceClick?: SliceMouseHandler

debugMesh?: boolean

Expand Down
2 changes: 2 additions & 0 deletions packages/line/src/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const Line = props => {
onMouseMove,
onMouseLeave,
onClick,
onSliceClick,

tooltip,

Expand Down Expand Up @@ -220,6 +221,7 @@ const Line = props => {
tooltip={sliceTooltip}
current={currentSlice}
setCurrent={setCurrentSlice}
onSliceClick={onSliceClick}
/>
)
}
Expand Down
4 changes: 3 additions & 1 deletion packages/line/src/Slices.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { memo } from 'react'
import PropTypes from 'prop-types'
import SlicesItem from './SlicesItem'

const Slices = ({ slices, axis, debug, height, tooltip, current, setCurrent }) => {
const Slices = ({ slices, axis, debug, height, tooltip, current, setCurrent, onSliceClick }) => {
return slices.map(slice => (
<SlicesItem
key={slice.id}
Expand All @@ -21,6 +21,7 @@ const Slices = ({ slices, axis, debug, height, tooltip, current, setCurrent }) =
tooltip={tooltip}
setCurrent={setCurrent}
isCurrent={current !== null && current.id === slice.id}
onSliceClick={onSliceClick}
/>
))
}
Expand All @@ -44,6 +45,7 @@ Slices.propTypes = {
tooltip: PropTypes.oneOfType([PropTypes.func, PropTypes.object]).isRequired,
current: PropTypes.object,
setCurrent: PropTypes.func.isRequired,
onSliceClick: PropTypes.func,
}

export default memo(Slices)
10 changes: 9 additions & 1 deletion packages/line/src/SlicesItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createElement, memo, useCallback } from 'react'
import PropTypes from 'prop-types'
import { useTooltip } from '@nivo/tooltip'

const SlicesItem = ({ slice, axis, debug, tooltip, isCurrent, setCurrent }) => {
const SlicesItem = ({ slice, axis, debug, tooltip, isCurrent, setCurrent, onSliceClick }) => {
const { showTooltipFromEvent, hideTooltip } = useTooltip()

const handleMouseEnter = useCallback(
Expand All @@ -33,6 +33,12 @@ const SlicesItem = ({ slice, axis, debug, tooltip, isCurrent, setCurrent }) => {
setCurrent(null)
}, [hideTooltip])

const handleClick = useCallback(() => {
if (onSliceClick) {
onSliceClick(slice)
}
}, [onSliceClick, slice])

return (
<rect
x={slice.x0}
Expand All @@ -47,6 +53,7 @@ const SlicesItem = ({ slice, axis, debug, tooltip, isCurrent, setCurrent }) => {
onMouseEnter={handleMouseEnter}
onMouseMove={handleMouseMove}
onMouseLeave={handleMouseLeave}
onClick={handleClick}
/>
)
}
Expand All @@ -59,6 +66,7 @@ SlicesItem.propTypes = {
tooltip: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
isCurrent: PropTypes.bool.isRequired,
setCurrent: PropTypes.func.isRequired,
onSliceClick: PropTypes.func,
}

export default memo(SlicesItem)