Skip to content

Commit 2e69633

Browse files
committedSep 7, 2021
feat(radar): improve dots related typings
1 parent 828e06b commit 2e69633

File tree

10 files changed

+64
-59
lines changed

10 files changed

+64
-59
lines changed
 

‎packages/core/index.d.ts

+23
Original file line numberDiff line numberDiff line change
@@ -500,4 +500,27 @@ declare module '@nivo/core' {
500500
export const curveFromProp: (interpolation: CurveFactoryId) => CurveFactory
501501

502502
export const useCurveInterpolation: (interpolation: CurveFactoryId) => CurveFactory
503+
504+
export interface DotsItemSymbolProps {
505+
size: number
506+
color: string
507+
borderWidth: number
508+
borderColor: string
509+
}
510+
export type DotsItemSymbolComponent = React.FunctionComponent<DotsItemSymbolProps>
511+
512+
export interface DotsItemProps<D = any> {
513+
datum: D
514+
x: number
515+
y: number
516+
size: number
517+
color: string
518+
borderWidth: number
519+
borderColor: string
520+
label?: string | number
521+
labelTextAnchor?: 'start' | 'middle' | 'end'
522+
labelYOffset?: number
523+
symbol?: DotsItemSymbolComponent
524+
}
525+
export const DotsItem: React.FunctionComponent<DotsItemProps>
503526
}

‎packages/core/src/components/dots/DotsItem.js

+6-27
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
1-
/*
2-
* This file is part of the nivo project.
3-
*
4-
* Copyright 2016-present, Raphaël Benitte.
5-
*
6-
* For the full copyright and license information, please view the LICENSE
7-
* file that was distributed with this source code.
8-
*/
91
import { createElement, memo } from 'react'
102
import PropTypes from 'prop-types'
113
import { useSpring, animated } from '@react-spring/web'
12-
import { dotsThemePropType } from '../../theming'
4+
import { useTheme } from '../../theming'
135
import { useMotionConfig } from '../../motion'
146
import DotsItemSymbol from './DotsItemSymbol'
157

168
const DotsItem = ({
179
x,
1810
y,
19-
symbol,
11+
symbol = DotsItemSymbol,
2012
size,
2113
datum,
2214
color,
2315
borderWidth,
2416
borderColor,
2517
label,
26-
labelTextAnchor,
27-
labelYOffset,
28-
theme,
18+
labelTextAnchor = 'middle',
19+
labelYOffset = -12,
2920
}) => {
30-
const { animate, config: springConfig } = useMotionConfig()
21+
const theme = useTheme()
3122

23+
const { animate, config: springConfig } = useMotionConfig()
3224
const animatedProps = useSpring({
3325
transform: `translate(${x}, ${y})`,
3426
config: springConfig,
@@ -68,19 +60,6 @@ DotsItem.propTypes = {
6860
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
6961
labelTextAnchor: PropTypes.oneOf(['start', 'middle', 'end']),
7062
labelYOffset: PropTypes.number.isRequired,
71-
72-
theme: PropTypes.shape({
73-
dots: dotsThemePropType.isRequired,
74-
}).isRequired,
7563
}
7664

77-
export const DotsItemDefaultProps = {
78-
symbol: DotsItemSymbol,
79-
80-
labelTextAnchor: 'middle',
81-
labelYOffset: -12,
82-
}
83-
84-
DotsItem.defaultProps = DotsItemDefaultProps
85-
8665
export default memo(DotsItem)

‎packages/radar/src/RadarDots.tsx

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { useMemo } from 'react'
22
import { ScaleLinear } from 'd3-scale'
3-
import {
4-
useTheme,
5-
positionFromAngle,
6-
// @ts-ignore: core package should be converted to TypeScript
7-
DotsItem,
8-
usePropertyAccessor,
9-
} from '@nivo/core'
3+
import { useTheme, positionFromAngle, DotsItem, usePropertyAccessor } from '@nivo/core'
104
import { getInheritedColorGenerator } from '@nivo/colors'
115
import { RadarCommonProps, RadarDataProps, PointProps, PointData, RadarColorMapping } from './types'
126

@@ -67,7 +61,7 @@ export const RadarDots = <D extends Record<string, unknown>>({
6761

6862
acc.push({
6963
key: `${key}.${index}`,
70-
label: enableLabel ? getLabel(pointData) : null,
64+
label: enableLabel ? getLabel(pointData) : undefined,
7165
style: {
7266
fill: fillColor(pointData),
7367
stroke: strokeColor(pointData),
@@ -84,10 +78,12 @@ export const RadarDots = <D extends Record<string, unknown>>({
8478
}, [] as PointProps[]),
8579
[
8680
data,
81+
keys,
8782
getIndex,
8883
colorByKey,
8984
enableLabel,
9085
getLabel,
86+
formatValue,
9187
fillColor,
9288
strokeColor,
9389
angleStep,
@@ -109,7 +105,6 @@ export const RadarDots = <D extends Record<string, unknown>>({
109105
borderColor={point.style.stroke}
110106
label={point.label}
111107
labelYOffset={labelYOffset}
112-
theme={theme}
113108
datum={point.data}
114109
/>
115110
))}

‎packages/radar/src/RadarGridLevels.tsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memo, useMemo } from 'react'
1+
import { memo, SVGProps, useMemo } from 'react'
22
import { lineRadial, curveLinearClosed } from 'd3-shape'
33
import { animated, useSpring, to } from '@react-spring/web'
44
import { useTheme, useAnimatedPath, useMotionConfig } from '@nivo/core'
@@ -22,7 +22,7 @@ const RadarGridLevelCircular = memo(({ radius }: RadarGridLevelCircularProps) =>
2222
<animated.circle
2323
fill="none"
2424
r={to(animatedProps.radius, value => Math.max(value, 0))}
25-
{...(theme.grid.line as any)}
25+
{...(theme.grid.line as Omit<SVGProps<SVGCircleElement>, 'ref'>)}
2626
/>
2727
)
2828
})
@@ -48,7 +48,13 @@ const RadarGridLevelLinear = ({ radius, angleStep, dataLength }: RadarGridLevelL
4848
const points = Array.from({ length: dataLength }, (_, i) => i)
4949
const animatedPath = useAnimatedPath(radarLineGenerator(points) as string)
5050

51-
return <animated.path fill="none" d={animatedPath} {...(theme.grid.line as any)} />
51+
return (
52+
<animated.path
53+
fill="none"
54+
d={animatedPath}
55+
{...(theme.grid.line as Omit<SVGProps<SVGPathElement>, 'ref'>)}
56+
/>
57+
)
5258
}
5359

5460
interface RadarGridLevelsProps {

‎packages/radar/src/RadarTooltipItem.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface RadarTooltipItemProps<D extends Record<string, unknown>> {
1313
startAngle: number
1414
endAngle: number
1515
radius: number
16-
arcGenerator: Arc<any, { startAngle: number; endAngle: number }>
16+
arcGenerator: Arc<void, { startAngle: number; endAngle: number }>
1717
}
1818

1919
type TooltipRow = [ReactNode, string, number | string]
@@ -49,7 +49,7 @@ export const RadarTooltipItem = <D extends Record<string, unknown>>({
4949
})
5050

5151
return <TableTooltip title={<strong>{index}</strong>} rows={rows} />
52-
}, [datum, keys, index, formatValue, colorByKey, theme])
52+
}, [datum, keys, index, formatValue, colorByKey])
5353
const showItemTooltip = useCallback(
5454
event => {
5555
setIsHover(true)

‎packages/radar/src/hooks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const useRadar = <D extends Record<string, unknown>>({
5959
centerY: height / 2,
6060
angleStep: (Math.PI * 2) / data.length,
6161
}
62-
}, [keys, indexBy, data, maxValue, width, height])
62+
}, [keys, data, maxValue, width, height])
6363

6464
const curveFactory = useCurveInterpolation(curve)
6565

‎packages/radar/src/types.ts

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FunctionComponent, AriaAttributes, ReactNode } from 'react'
1+
import { FunctionComponent, AriaAttributes } from 'react'
22
import { AnimatedProps } from '@react-spring/web'
33
import {
44
Box,
@@ -9,6 +9,7 @@ import {
99
PropertyAccessor,
1010
ValueFormat,
1111
ClosedCurveFactoryId,
12+
DotsItemSymbolComponent,
1213
} from '@nivo/core'
1314
import { InheritedColorConfig, OrdinalColorScaleConfig } from '@nivo/colors'
1415
import { LegendProps } from '@nivo/legends'
@@ -41,7 +42,7 @@ export type PointData = {
4142

4243
export interface PointProps {
4344
key: string
44-
label: ReactNode | null
45+
label: string | number | undefined
4546
data: PointData
4647
style: {
4748
fill: string
@@ -51,14 +52,6 @@ export interface PointProps {
5152
}
5253
}
5354

54-
export interface DotSymbolProps {
55-
size: number
56-
color: InheritedColorConfig<PointData>
57-
borderWidth: number
58-
borderColor: InheritedColorConfig<PointData>
59-
}
60-
export type DotSymbolComponent = FunctionComponent<DotSymbolProps>
61-
6255
export type RadarLayerId = 'grid' | 'shapes' | 'dots' | 'legends'
6356

6457
export type RadarColorMapping = Record<string, string>
@@ -79,7 +72,7 @@ export interface RadarCommonProps {
7972
gridLabelOffset: number
8073

8174
enableDots: boolean
82-
dotSymbol: DotSymbolComponent
75+
dotSymbol: DotsItemSymbolComponent
8376
dotSize: number
8477
dotColor: InheritedColorConfig<PointData>
8578
dotBorderWidth: number

‎website/src/data/components/line/props.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { lineCurvePropKeys, DotsItemDefaultProps as dotDefaults } from '@nivo/core'
1+
import { lineCurvePropKeys } from '@nivo/core'
22
import { LineDefaultProps as defaults } from '@nivo/line'
33
import {
44
themeProperty,
@@ -383,7 +383,7 @@ const props: ChartProperty[] = [
383383
help: 'Label Y offset from point shape.',
384384
type: 'number',
385385
required: false,
386-
defaultValue: dotDefaults.labelYOffset,
386+
defaultValue: -12,
387387
controlType: 'range',
388388
controlOptions: {
389389
unit: 'px',

‎website/src/pages/radar/api.js

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const RadarApi = () => {
2727
keys,
2828
indexBy: 'taste',
2929
maxValue: 'auto',
30+
valueFormat: { format: '>-.2f', enabled: true },
3031

3132
margin: {
3233
top: 70,

‎website/src/pages/radar/index.tsx

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import React from 'react'
22
import { generateWinesTastes } from '@nivo/generators'
3-
import { ResponsiveRadar, svgDefaultProps } from '@nivo/radar'
3+
import { ResponsiveRadar, RadarSvgProps, svgDefaultProps } from '@nivo/radar'
44
import { ComponentTemplate } from '../../components/components/ComponentTemplate'
55
import meta from '../../data/components/radar/meta.yml'
66
import mapper from '../../data/components/radar/mapper'
77
import { groups } from '../../data/components/radar/props'
88

9-
const initialProperties = {
9+
type MappedRadarProps = Omit<RadarSvgProps<any>, 'data' | 'keys' | 'width' | 'height'>
10+
type UnmappedRadarProps = Omit<MappedRadarProps, 'valueFormat'> & {
11+
valueFormat: {
12+
format: string
13+
enabled: boolean
14+
}
15+
}
16+
17+
const initialProperties: UnmappedRadarProps = {
1018
indexBy: 'taste',
1119
maxValue: 'auto',
1220
valueFormat: { format: '>-.2f', enabled: true },
@@ -72,7 +80,7 @@ const initialProperties = {
7280
}
7381

7482
const Radar = () => (
75-
<ComponentTemplate
83+
<ComponentTemplate<UnmappedRadarProps, MappedRadarProps, any>
7684
name="Radar"
7785
meta={meta.Radar}
7886
icon="radar"
@@ -82,7 +90,7 @@ const Radar = () => (
8290
initialProperties={initialProperties}
8391
defaultProperties={svgDefaultProps}
8492
propertiesMapper={mapper}
85-
codePropertiesMapper={(properties, data) => ({
93+
codePropertiesMapper={(properties: any, data: any) => ({
8694
keys: data.keys,
8795
...properties,
8896
})}

0 commit comments

Comments
 (0)
Please sign in to comment.