From 47a647011288e19b9a64ba4f9781e5c77ea1c192 Mon Sep 17 00:00:00 2001 From: Pascal Corpet Date: Mon, 25 Dec 2023 00:12:20 +0100 Subject: [PATCH] fix(marimekko): use readonly arrays for props as the library does not modify them --- packages/marimekko/src/Bars.tsx | 2 +- packages/marimekko/src/hooks.ts | 6 ++++-- packages/marimekko/src/types.ts | 18 +++++++++--------- packages/scales/src/types.ts | 4 ++-- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/packages/marimekko/src/Bars.tsx b/packages/marimekko/src/Bars.tsx index 5321155e0..317f0375c 100644 --- a/packages/marimekko/src/Bars.tsx +++ b/packages/marimekko/src/Bars.tsx @@ -5,7 +5,7 @@ import { Bar } from './Bar' interface BarsProps extends MouseEventHandlers { isInteractive: boolean - bars: BarDatum[] + bars: readonly BarDatum[] tooltip: CommonProps['tooltip'] } diff --git a/packages/marimekko/src/hooks.ts b/packages/marimekko/src/hooks.ts index d63a03ce3..94fc8af0f 100644 --- a/packages/marimekko/src/hooks.ts +++ b/packages/marimekko/src/hooks.ts @@ -38,7 +38,7 @@ export const useDataDimensions = (rawDimensions: DataProps[' }, [rawDimensions]) export const useStack = ( - dimensionIds: string[], + dimensionIds: readonly string[], dimensions: Record number>, offset: OffsetId ) => @@ -186,6 +186,7 @@ export const useComputedData = ({ height: layout === 'vertical' ? 0 : thickness, dimensions: [], } + const dimensions: DimensionDatum[] = [] const allPositions: number[] = [] let totalSize = 0 @@ -231,7 +232,7 @@ export const useComputedData = ({ dimensionDatum.color = getColor(dimensionDatum) - computedDatum.dimensions.push(dimensionDatum) + dimensions.push(dimensionDatum) } if (layout === 'vertical') { @@ -242,6 +243,7 @@ export const useComputedData = ({ computedDatum.width = totalSize } }) + computedDatum.dimensions = dimensions computedData.push(computedDatum) }) diff --git a/packages/marimekko/src/types.ts b/packages/marimekko/src/types.ts index 80da75b25..57a4b93f1 100644 --- a/packages/marimekko/src/types.ts +++ b/packages/marimekko/src/types.ts @@ -19,10 +19,10 @@ export type DatumFormattedValue = string | number export type DatumPropertyAccessor = (datum: RawDatum) => T export interface DataProps { - data: RawDatum[] + data: readonly RawDatum[] id: string | number | DatumPropertyAccessor value: string | number | DatumPropertyAccessor - dimensions: { + dimensions: readonly { id: string value: string | number | DatumPropertyAccessor }[] @@ -53,7 +53,7 @@ export interface ComputedDatum extends NormalizedDatum { y: number width: number height: number - dimensions: DimensionDatum[] + dimensions: readonly DimensionDatum[] } export interface BarDatum extends DimensionDatum { @@ -68,8 +68,8 @@ export type LabelAccessorFunction = (datum: ComputedDatum) = export type LayerId = 'grid' | 'axes' | 'bars' | 'legends' export interface CustomLayerProps { - data: ComputedDatum[] - bars: BarDatum[] + data: readonly ComputedDatum[] + bars: readonly BarDatum[] thicknessScale: ScaleLinear dimensionsScale: ScaleLinear } @@ -119,9 +119,9 @@ export type CommonProps = { axisBottom?: AxisProps | null axisLeft?: AxisProps | null enableGridX: boolean - gridXValues?: number[] + gridXValues?: readonly number[] enableGridY: boolean - gridYValues?: number[] + gridYValues?: readonly number[] // colors, theme and border colors: OrdinalColorScaleConfig, 'color' | 'fill'>> @@ -140,7 +140,7 @@ export type CommonProps = { isInteractive: boolean tooltip: BarTooltipType - legends: LegendProps[] + legends: readonly LegendProps[] role: string } @@ -163,5 +163,5 @@ export type SvgProps = DataProps & MotionProps & SvgDefsAndFill> & MouseEventHandlers & { - layers?: Layer[] + layers?: readonly Layer[] } diff --git a/packages/scales/src/types.ts b/packages/scales/src/types.ts index 85331cb42..1ca664179 100644 --- a/packages/scales/src/types.ts +++ b/packages/scales/src/types.ts @@ -142,7 +142,7 @@ export type SerieAxis = { }[] export type ComputedSerieAxis = { - all: Value[] + all: readonly Value[] min: Value minStacked?: Value max: Value @@ -159,4 +159,4 @@ export type TicksSpec = // for example: every 2 weeks | string // override scale ticks with custom explicit values - | Value[] + | readonly Value[]