Skip to content

Commit 75eafa1

Browse files
jquintozamorajoseq-concentra
andauthoredJul 27, 2021
feat(bullet): add custom tooltip (#1694)
Co-authored-by: Jose Quinto Zamora <jose.quintozamora@concentra.co.uk>
1 parent 7ff0e72 commit 75eafa1

File tree

7 files changed

+96
-20
lines changed

7 files changed

+96
-20
lines changed
 

‎packages/bullet/src/Bullet.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export const Bullet = (props: BulletSvgProps) => {
4242
markerColors,
4343

4444
theme,
45+
tooltip = defaultProps.tooltip,
4546

4647
animate,
4748
motionConfig,
@@ -112,6 +113,7 @@ export const Bullet = (props: BulletSvgProps) => {
112113
onRangeClick={onRangeClick}
113114
onMeasureClick={onMeasureClick}
114115
onMarkerClick={onMarkerClick}
116+
tooltip={tooltip}
115117
/>
116118
))}
117119
</SvgWrapper>

‎packages/bullet/src/BulletItem.tsx

+7-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useSpring, animated } from '@react-spring/web'
33
import { Axis } from '@nivo/axes'
44
// @ts-ignore
55
import { getColorScale, useMotionConfig, useTheme } from '@nivo/core'
6-
import { BasicTooltip, useTooltip } from '@nivo/tooltip'
6+
import { useTooltip } from '@nivo/tooltip'
77
import { stackValues } from './compute'
88
import { BulletMarkers } from './BulletMarkers'
99
import { BulletRects } from './BulletRects'
@@ -27,6 +27,7 @@ export const BulletItem = ({
2727
titleOffsetX,
2828
titleOffsetY,
2929
titleRotation,
30+
tooltip,
3031

3132
rangeBorderColor,
3233
rangeBorderWidth,
@@ -77,6 +78,8 @@ export const BulletItem = ({
7778
}))
7879
}, [markerColors, markers, scale])
7980

81+
const TooltipComponent = tooltip
82+
8083
const rangeNodes = (
8184
<BulletRects
8285
data={computedRanges}
@@ -92,15 +95,7 @@ export const BulletItem = ({
9295
borderWidth={rangeBorderWidth}
9396
onMouseEnter={(range, event) => {
9497
showTooltipFromEvent(
95-
<BasicTooltip
96-
id={
97-
<span>
98-
<strong>{range.v0}</strong> to <strong>{range.v1}</strong>
99-
</span>
100-
}
101-
enableChip={true}
102-
color={range.color}
103-
/>,
98+
<TooltipComponent color={range.color} v0={range.v0} v1={range.v1} />,
10499
event
105100
)
106101
}}
@@ -122,11 +117,7 @@ export const BulletItem = ({
122117
component={markerComponent}
123118
onMouseEnter={(marker, event) => {
124119
showTooltipFromEvent(
125-
<BasicTooltip
126-
id={<strong>{marker.value}</strong>}
127-
enableChip={true}
128-
color={marker.color}
129-
/>,
120+
<TooltipComponent color={marker.color} v0={marker.value} />,
130121
event
131122
)
132123
}}
@@ -208,11 +199,7 @@ export const BulletItem = ({
208199
borderWidth={measureBorderWidth}
209200
onMouseEnter={(measure, event) => {
210201
showTooltipFromEvent(
211-
<BasicTooltip
212-
id={<strong>{measure.v1}</strong>}
213-
enableChip={true}
214-
color={measure.color}
215-
/>,
202+
<TooltipComponent color={measure.color} v0={measure.v1} />,
216203
event
217204
)
218205
}}

‎packages/bullet/src/BulletTooltip.tsx

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { BulletTooltipProps } from './types'
2+
import { BasicTooltip } from '@nivo/tooltip'
3+
4+
export const BulletTooltip = ({ color, v0, v1 }: BulletTooltipProps) => {
5+
return (
6+
<BasicTooltip
7+
id={
8+
v1 ? (
9+
<span>
10+
<strong>{v0}</strong> to <strong>{v1}</strong>
11+
</span>
12+
) : (
13+
<strong>{v0}</strong>
14+
)
15+
}
16+
enableChip={true}
17+
color={color}
18+
/>
19+
)
20+
}

‎packages/bullet/src/props.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BulletMarkersItem } from './BulletMarkersItem'
22
import { BulletRectsItem } from './BulletRectsItem'
33
import { motionDefaultProps, defaultMargin } from '@nivo/core'
4+
import { BulletTooltip } from './BulletTooltip'
45

56
export const defaultProps = {
67
layout: 'horizontal',
@@ -28,6 +29,7 @@ export const defaultProps = {
2829
measureBorderColor: { from: 'color' },
2930
markerSize: 0.6,
3031
isInteractive: true,
32+
tooltip: BulletTooltip,
3133
animate: motionDefaultProps.animate,
3234
motionConfig: motionDefaultProps.config,
3335
margin: defaultMargin,

‎packages/bullet/src/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export type CommonBulletProps = Dimensions & {
5656
titleOffsetX: number
5757
titleOffsetY: number
5858
titleRotation: number
59+
tooltip: React.ComponentType<BulletTooltipProps>
5960

6061
rangeBorderColor: InheritedColorConfig<ComputedRangeDatum>
6162
rangeBorderWidth: number
@@ -201,3 +202,9 @@ export type BulletItemProps = Omit<
201202
measureHeight: number
202203
markerHeight: number
203204
}
205+
206+
export interface BulletTooltipProps {
207+
v0: number
208+
v1?: number
209+
color: string
210+
}

‎packages/bullet/stories/bullet.stories.tsx

+33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { storiesOf } from '@storybook/react'
22
import { generateBulletData } from '@nivo/generators'
3+
import { BasicTooltip } from '@nivo/tooltip'
34
import { withKnobs, boolean, number } from '@storybook/addon-knobs'
45
import { Bullet } from '../src'
56

@@ -181,3 +182,35 @@ stories.add('support min/max value property', () => (
181182
}
182183
/>
183184
))
185+
186+
const CustomTooltip = ({ v0, v1, color }: { color: string; v0: number; v1?: number }) => {
187+
return (
188+
<BasicTooltip
189+
id={
190+
v1 ? (
191+
<span style={{ color: 'peachpuff' }}>
192+
<strong>{v0}</strong> to <strong>{v1}</strong>
193+
</span>
194+
) : (
195+
<strong style={{ color: 'rosybrown' }}>{v0}</strong>
196+
)
197+
}
198+
enableChip={true}
199+
color={color}
200+
/>
201+
)
202+
}
203+
204+
stories.add('custom tooltip', () => (
205+
<Bullet
206+
{...commonProps}
207+
tooltip={CustomTooltip}
208+
theme={{
209+
tooltip: {
210+
container: {
211+
background: '#333',
212+
},
213+
},
214+
}}
215+
/>
216+
))

‎packages/bullet/tests/Bullet.test.tsx

+25
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,31 @@ describe('Bullet', () => {
339339
expect(tooltip.exists()).toBeTruthy()
340340
expect(tooltip.text()).toEqual('20')
341341
})
342+
343+
it('should allow to override the default tooltip', () => {
344+
const CustomTooltip = ({
345+
color,
346+
v0,
347+
v1,
348+
}: {
349+
color: string
350+
v0: number
351+
v1?: number
352+
}) => (
353+
<span style={{ backgroundColor: color }}>
354+
{v1 ? `${v0} to ${v1}` : `Custom${v0}`}
355+
</span>
356+
)
357+
const wrapper = mount(
358+
<Bullet width={400} height={400} data={sampleData} tooltip={CustomTooltip} />
359+
)
360+
361+
wrapper.find('BulletMarkersItem').at(0).simulate('mouseenter')
362+
363+
const tooltip = wrapper.find(CustomTooltip)
364+
expect(tooltip.exists()).toBeTruthy()
365+
expect(tooltip.text()).toEqual('Custom20')
366+
})
342367
})
343368

344369
describe('custom components', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.