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(marimekko): use readonly arrays for props as the library does not modify them #2493

Merged
merged 1 commit into from
Mar 5, 2024
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
2 changes: 1 addition & 1 deletion packages/marimekko/src/Bars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Bar } from './Bar'

interface BarsProps<RawDatum> extends MouseEventHandlers<RawDatum, SVGRectElement> {
isInteractive: boolean
bars: BarDatum<RawDatum>[]
bars: readonly BarDatum<RawDatum>[]
tooltip: CommonProps<RawDatum>['tooltip']
}

Expand Down
6 changes: 4 additions & 2 deletions packages/marimekko/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const useDataDimensions = <RawDatum>(rawDimensions: DataProps<RawDatum>['
}, [rawDimensions])

export const useStack = <RawDatum>(
dimensionIds: string[],
dimensionIds: readonly string[],
dimensions: Record<string, (datum: RawDatum) => number>,
offset: OffsetId
) =>
Expand Down Expand Up @@ -186,6 +186,7 @@ export const useComputedData = <RawDatum>({
height: layout === 'vertical' ? 0 : thickness,
dimensions: [],
}
const dimensions: DimensionDatum<RawDatum>[] = []

const allPositions: number[] = []
let totalSize = 0
Expand Down Expand Up @@ -231,7 +232,7 @@ export const useComputedData = <RawDatum>({

dimensionDatum.color = getColor(dimensionDatum)

computedDatum.dimensions.push(dimensionDatum)
dimensions.push(dimensionDatum)
}

if (layout === 'vertical') {
Expand All @@ -242,6 +243,7 @@ export const useComputedData = <RawDatum>({
computedDatum.width = totalSize
}
})
computedDatum.dimensions = dimensions

computedData.push(computedDatum)
})
Expand Down
18 changes: 9 additions & 9 deletions packages/marimekko/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export type DatumFormattedValue = string | number
export type DatumPropertyAccessor<RawDatum, T> = (datum: RawDatum) => T

export interface DataProps<RawDatum> {
data: RawDatum[]
data: readonly RawDatum[]
id: string | number | DatumPropertyAccessor<RawDatum, DatumId>
value: string | number | DatumPropertyAccessor<RawDatum, DatumValue>
dimensions: {
dimensions: readonly {
id: string
value: string | number | DatumPropertyAccessor<RawDatum, DatumValue>
}[]
Expand Down Expand Up @@ -53,7 +53,7 @@ export interface ComputedDatum<RawDatum> extends NormalizedDatum<RawDatum> {
y: number
width: number
height: number
dimensions: DimensionDatum<RawDatum>[]
dimensions: readonly DimensionDatum<RawDatum>[]
}

export interface BarDatum<RawDatum> extends DimensionDatum<RawDatum> {
Expand All @@ -68,8 +68,8 @@ export type LabelAccessorFunction<RawDatum> = (datum: ComputedDatum<RawDatum>) =
export type LayerId = 'grid' | 'axes' | 'bars' | 'legends'

export interface CustomLayerProps<RawDatum> {
data: ComputedDatum<RawDatum>[]
bars: BarDatum<RawDatum>[]
data: readonly ComputedDatum<RawDatum>[]
bars: readonly BarDatum<RawDatum>[]
thicknessScale: ScaleLinear<number>
dimensionsScale: ScaleLinear<number>
}
Expand Down Expand Up @@ -119,9 +119,9 @@ export type CommonProps<RawDatum> = {
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<Omit<DimensionDatum<RawDatum>, 'color' | 'fill'>>
Expand All @@ -140,7 +140,7 @@ export type CommonProps<RawDatum> = {
isInteractive: boolean
tooltip: BarTooltipType<RawDatum>

legends: LegendProps[]
legends: readonly LegendProps[]

role: string
}
Expand All @@ -163,5 +163,5 @@ export type SvgProps<RawDatum> = DataProps<RawDatum> &
MotionProps &
SvgDefsAndFill<BarDatum<RawDatum>> &
MouseEventHandlers<RawDatum, SVGRectElement> & {
layers?: Layer<RawDatum>[]
layers?: readonly Layer<RawDatum>[]
}
4 changes: 2 additions & 2 deletions packages/scales/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export type SerieAxis<Axis extends ScaleAxis, Value extends ScaleValue> = {
}[]

export type ComputedSerieAxis<Value extends ScaleValue> = {
all: Value[]
all: readonly Value[]
min: Value
minStacked?: Value
max: Value
Expand All @@ -159,4 +159,4 @@ export type TicksSpec<Value extends ScaleValue> =
// for example: every 2 weeks
| string
// override scale ticks with custom explicit values
| Value[]
| readonly Value[]