Skip to content

Commit 17a3b6d

Browse files
committed
feat(bar): improve default props casting
1 parent 5a21345 commit 17a3b6d

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

packages/bar/src/Bar.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { BarLegends } from './BarLegends'
1717
import { useBar } from './hooks'
1818
import { svgDefaultProps } from './defaults'
1919
import {
20+
BarAnnotationMatcher,
21+
BarBorderColor,
2022
BarComponent,
2123
BarCustomLayerProps,
2224
BarDatum,
@@ -66,14 +68,15 @@ const InnerBar = <D extends BarDatum = BarDatum, I extends BarIndex = string>({
6668
labelPosition = svgDefaultProps.labelPosition,
6769
labelOffset = svgDefaultProps.labelOffset,
6870
markers = svgDefaultProps.markers,
69-
colorBy,
7071
colors,
72+
colorBy,
7173
defs = svgDefaultProps.defs,
74+
// @ts-expect-error the typings for SVG fill are not easy to get right.
7275
fill = svgDefaultProps.fill,
7376
borderRadius = svgDefaultProps.borderRadius,
7477
borderWidth = svgDefaultProps.borderWidth,
75-
borderColor,
76-
annotations = svgDefaultProps.annotations,
78+
borderColor = svgDefaultProps.borderColor as BarBorderColor<D, I>,
79+
annotations = svgDefaultProps.annotations as BarAnnotationMatcher<D, I>[],
7780
legendLabel,
7881
tooltipLabel,
7982
valueFormat,

packages/bar/src/BarCanvas.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { renderAxesToCanvas, renderGridLinesToCanvas } from '@nivo/axes'
3030
import { renderLegendToCanvas } from '@nivo/legends'
3131
import { useTooltip } from '@nivo/tooltip'
3232
import {
33+
BarAnnotationMatcher,
3334
BarCanvasCustomLayerProps,
3435
BarCanvasLayer,
3536
BarCanvasProps,
@@ -113,7 +114,7 @@ const InnerBarCanvas = <D extends BarDatum = BarDatum, I extends BarIndex = stri
113114
borderRadius = canvasDefaultProps.borderRadius,
114115
borderWidth = canvasDefaultProps.borderWidth,
115116
borderColor,
116-
annotations = canvasDefaultProps.annotations,
117+
annotations = canvasDefaultProps.annotations as BarAnnotationMatcher<D, I>[],
117118
legendLabel,
118119
tooltipLabel,
119120
valueFormat,

packages/bar/src/defaults.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { InheritedColorConfig, OrdinalColorScaleConfig } from '@nivo/colors'
21
import {
32
BarCommonProps,
4-
BarDatum,
53
ComputedDatum,
64
BarSvgPropsWithDefaults,
75
BarCanvasPropsWithDefaults,
@@ -13,8 +11,8 @@ import { renderBar } from './renderBar'
1311
export const commonDefaultProps: Omit<BarCommonProps, 'data' | 'theme'> = {
1412
indexBy: 'id',
1513
keys: ['value'],
16-
groupMode: 'stacked' as const,
17-
layout: 'vertical' as const,
14+
groupMode: 'stacked',
15+
layout: 'vertical',
1816
valueScale: { type: 'linear', nice: true, round: false },
1917
indexScale: { type: 'band', round: false },
2018
padding: 0.1,
@@ -23,16 +21,16 @@ export const commonDefaultProps: Omit<BarCommonProps, 'data' | 'theme'> = {
2321
enableGridY: true,
2422
enableLabel: true,
2523
label: 'formattedValue',
26-
labelPosition: 'middle' as const,
24+
labelPosition: 'middle',
2725
labelOffset: 0,
2826
labelSkipWidth: 0,
2927
labelSkipHeight: 0,
3028
labelTextColor: { theme: 'labels.text.fill' },
31-
colorBy: 'id' as const,
32-
colors: { scheme: 'nivo' } as OrdinalColorScaleConfig,
29+
colorBy: 'id',
30+
colors: { scheme: 'nivo' },
3331
borderRadius: 0,
3432
borderWidth: 0,
35-
borderColor: { from: 'color' } as InheritedColorConfig<any>,
33+
borderColor: { from: 'color' },
3634
isInteractive: true,
3735
tooltip: BarTooltip,
3836
tooltipLabel: (datum: ComputedDatum) => `${datum.id} - ${datum.indexValue}`,

packages/bar/src/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export type BarIndex = string | number | Date
3333
export type BarValue = number | null
3434
// Default datum type for the bar chart.
3535
export type BarDatum = Record<string, unknown>
36+
export type BarBorderColor<D extends BarDatum = BarDatum, I extends BarIndex = string> = InheritedColorConfig<ComputedBarDatumWithValue<D, I>>
37+
export type BarAnnotationMatcher<D extends BarDatum = BarDatum, I extends BarIndex = string> = AnnotationMatcher<ComputedBarDatum<D, I>>
3638

3739
export interface DataProps<D extends BarDatum> {
3840
data: readonly D[]
@@ -261,7 +263,7 @@ export type BarCommonProps<D extends BarDatum = BarDatum, I extends BarIndex = s
261263
gridXValues?: GridValues<string | number>
262264
enableGridY: boolean
263265
gridYValues?: GridValues<string | number>
264-
borderColor: InheritedColorConfig<ComputedBarDatumWithValue<D>>
266+
borderColor: BarBorderColor<D, I>
265267
borderRadius: number
266268
borderWidth: number
267269
enableLabel: boolean
@@ -369,6 +371,6 @@ export type BarCanvasPropsWithDefaults<
369371
Dimensions
370372

371373
export type BarAnnotationsProps<D extends BarDatum = BarDatum, I extends BarIndex = string> = {
372-
annotations: readonly AnnotationMatcher<ComputedBarDatum<D, I>>[]
374+
annotations: readonly BarAnnotationMatcher<D, I>[]
373375
bars: readonly ComputedBarDatum<D, I>[]
374376
}

0 commit comments

Comments
 (0)