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

fix(bar): use readonly arrays for props as the library does not modif… #2377

Merged
merged 1 commit into from
Nov 14, 2023
Merged
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
4 changes: 2 additions & 2 deletions packages/annotations/src/compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export const bindAnnotations = <
getPosition,
getDimensions,
}: {
data: Datum[]
annotations: AnnotationMatcher<Datum>[]
data: readonly Datum[]
annotations: readonly AnnotationMatcher<Datum>[]
getPosition: AnnotationPositionGetter<Datum>
getDimensions: AnnotationDimensionsGetter<Datum>
}): BoundAnnotation<Datum>[] =>
Expand Down
6 changes: 3 additions & 3 deletions packages/annotations/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const useAnnotations = <Datum>({
getPosition,
getDimensions,
}: {
data: Datum[]
annotations: AnnotationMatcher<Datum>[]
data: readonly Datum[]
annotations: readonly AnnotationMatcher<Datum>[]
getPosition: AnnotationPositionGetter<Datum>
getDimensions: AnnotationDimensionsGetter<Datum>
}) =>
Expand All @@ -35,7 +35,7 @@ export const useAnnotations = <Datum>({
export const useComputedAnnotations = <Datum>({
annotations,
}: {
annotations: BoundAnnotation<Datum>[]
annotations: readonly BoundAnnotation<Datum>[]
}) =>
useMemo(
() =>
Expand Down
2 changes: 1 addition & 1 deletion packages/bar/src/Bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const InnerBar = <RawDatum extends BarDatum>({
gridXValues,
gridYValues,

layers = svgDefaultProps.layers as BarLayer<RawDatum>[],
layers = svgDefaultProps.layers as readonly BarLayer<RawDatum>[],
barComponent = svgDefaultProps.barComponent,

enableLabel = svgDefaultProps.enableLabel,
Expand Down
4 changes: 2 additions & 2 deletions packages/bar/src/compute/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ScaleBandSpec, ScaleBand, computeScale } from '@nivo/scales'
* Generates indexed scale.
*/
export const getIndexScale = <RawDatum>(
data: RawDatum[],
data: readonly RawDatum[],
getIndex: (datum: RawDatum) => string,
padding: number,
indexScale: ScaleBandSpec,
Expand All @@ -24,7 +24,7 @@ export const getIndexScale = <RawDatum>(
/**
* This method ensures all the provided keys exist in the entire series.
*/
export const normalizeData = <RawDatum>(data: RawDatum[], keys: string[]) =>
export const normalizeData = <RawDatum>(data: readonly RawDatum[], keys: readonly string[]) =>
data.map(
item =>
({
Expand Down
4 changes: 2 additions & 2 deletions packages/bar/src/compute/grouped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BarDatum, BarSvgProps, ComputedBarDatum, ComputedDatum } from '../types
import { coerceValue, filterNullValues, getIndexScale, normalizeData } from './common'

type Params<RawDatum, XScaleInput, YScaleInput> = {
data: RawDatum[]
data: readonly RawDatum[]
formatValue: (value: number) => string
getColor: OrdinalColorScale<ComputedDatum<RawDatum>>
getIndex: (datum: RawDatum) => string
Expand Down Expand Up @@ -186,7 +186,7 @@ export const generateGroupedBars = <RawDatum extends BarDatum>({
getIndex: (datum: RawDatum) => string
getTooltipLabel: (datum: ComputedDatum<RawDatum>) => string
margin: Margin
hiddenIds?: (string | number)[]
hiddenIds?: readonly (string | number)[]
}) => {
const keys = props.keys.filter(key => !hiddenIds.includes(key))
const data = normalizeData(props.data, keys)
Expand Down
2 changes: 1 addition & 1 deletion packages/bar/src/compute/stacked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export const generateStackedBars = <RawDatum extends BarDatum>({
getIndex: (datum: RawDatum) => string
getTooltipLabel: (datum: ComputedDatum<RawDatum>) => string
margin: Margin
hiddenIds?: (string | number)[]
hiddenIds?: readonly (string | number)[]
}) => {
const keys = props.keys.filter(key => !hiddenIds.includes(key))
const stackedData = stack<RawDatum, string>().keys(keys).offset(stackOffsetDiverging)(
Expand Down
22 changes: 11 additions & 11 deletions packages/bar/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface BarDatum {
}

export interface DataProps<RawDatum extends BarDatum> {
data: RawDatum[]
data: readonly RawDatum[]
}

export type BarDatumWithColor = BarDatum & {
Expand Down Expand Up @@ -109,8 +109,8 @@ interface BarCustomLayerBaseProps<RawDatum>
| 'tooltip'
>,
Dimensions {
bars: ComputedBarDatum<RawDatum>[]
legendData: [BarLegendProps, LegendData[]][]
bars: readonly ComputedBarDatum<RawDatum>[]
legendData: [BarLegendProps, readonly LegendData[]][]

margin: Margin
innerWidth: number
Expand Down Expand Up @@ -208,7 +208,7 @@ export type BarHandlers<RawDatum, Element> = {

export type BarCommonProps<RawDatum> = {
indexBy: PropertyAccessor<RawDatum, string>
keys: string[]
keys: readonly string[]

maxValue: 'auto' | number
minValue: 'auto' | number
Expand Down Expand Up @@ -253,12 +253,12 @@ export type BarCommonProps<RawDatum> = {
colors: OrdinalColorScaleConfig<ComputedDatum<RawDatum>>
theme: Theme

annotations: AnnotationMatcher<ComputedBarDatum<RawDatum>>[]
legends: BarLegendProps[]
annotations: readonly AnnotationMatcher<ComputedBarDatum<RawDatum>>[]
legends: readonly BarLegendProps[]

renderWrapper?: boolean

initialHiddenIds: (string | number)[]
initialHiddenIds: readonly (string | number)[]
}

export type BarSvgProps<RawDatum extends BarDatum> = Partial<BarCommonProps<RawDatum>> &
Expand All @@ -275,9 +275,9 @@ export type BarSvgProps<RawDatum extends BarDatum> = Partial<BarCommonProps<RawD

barComponent: React.FC<BarItemProps<RawDatum>>

markers: CartesianMarkerProps[]
markers: readonly CartesianMarkerProps[]

layers: BarLayer<RawDatum>[]
layers: readonly BarLayer<RawDatum>[]

role: string
ariaLabel?: React.AriaAttributes['aria-label']
Expand Down Expand Up @@ -310,6 +310,6 @@ export type BarCanvasProps<RawDatum extends BarDatum> = Partial<BarCommonProps<R
}>

export type BarAnnotationsProps<RawDatum> = {
annotations: AnnotationMatcher<ComputedBarDatum<RawDatum>>[]
bars: ComputedBarDatum<RawDatum>[]
annotations: readonly AnnotationMatcher<ComputedBarDatum<RawDatum>>[]
bars: readonly ComputedBarDatum<RawDatum>[]
}
Loading