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(axes): fix AxisProps types to be correct #1502

Merged
merged 1 commit into from
May 2, 2021
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
14 changes: 7 additions & 7 deletions packages/axes/src/components/Axes.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { Axis } from './Axis'
import { positions } from '../props'
import { AnyScale, SingleAxisProp, AxisValue } from '../types'
import { AnyScale, AxisProps, AxisValue } from '../types'

export const Axes = <X extends AxisValue, Y extends AxisValue>({
xScale,
Expand All @@ -17,19 +17,19 @@ export const Axes = <X extends AxisValue, Y extends AxisValue>({
yScale: AnyScale
width: number
height: number
top?: SingleAxisProp<X>
right?: SingleAxisProp<Y>
bottom?: SingleAxisProp<X>
left?: SingleAxisProp<Y>
top?: AxisProps<X>
right?: AxisProps<Y>
bottom?: AxisProps<X>
left?: AxisProps<Y>
}) => {
const axes = { top, right, bottom, left }

return (
<>
{positions.map(position => {
const axis = axes[position] as typeof position extends 'bottom' | 'top'
? SingleAxisProp<X> | undefined
: SingleAxisProp<Y> | undefined
? AxisProps<X> | undefined
: AxisProps<Y> | undefined

if (!axis) return null

Expand Down
11 changes: 9 additions & 2 deletions packages/axes/src/components/Axis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useSpring, useTransition, animated } from 'react-spring'
import { useTheme, useMotionConfig } from '@nivo/core'
import { computeCartesianTicks, getFormatter } from '../compute'
import { AxisTick } from './AxisTick'
import { AxisProps, AxisValue } from '../types'
import { AnyScale, AxisProps, AxisValue } from '../types'

export const Axis = <Value extends AxisValue>({
axis,
Expand All @@ -23,7 +23,14 @@ export const Axis = <Value extends AxisValue>({
legendOffset = 0,
onClick,
ariaHidden,
}: AxisProps<Value>) => {
}: AxisProps<Value> & {
axis: 'x' | 'y'
scale: AnyScale
x?: number
y?: number
length: number
onClick?: (event: React.MouseEvent<SVGGElement, MouseEvent>, value: Value | string) => void
}) => {
const theme = useTheme()

const formatValue = useMemo(() => getFormatter(format, scale), [format, scale])
Expand Down
25 changes: 3 additions & 22 deletions packages/axes/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export type AxisLegendPosition = 'start' | 'middle' | 'end'

export type ValueFormatter<Value extends AxisValue> = (value: Value) => Value | string

export interface SingleAxisProp<Value extends AxisValue> {
export interface AxisProps<Value extends AxisValue = any> {
ticksPosition?: 'before' | 'after'
tickValues?: TicksSpec<Value>
tickSize?: number
Expand All @@ -66,33 +66,14 @@ export interface SingleAxisProp<Value extends AxisValue> {
legend?: React.ReactNode
legendPosition?: AxisLegendPosition
legendOffset?: number
ariaHidden?: boolean
}

export interface CanvasAxisProp<Value extends string | number | Date>
extends Omit<SingleAxisProp<Value>, 'legend'> {
extends Omit<AxisProps<Value>, 'legend'> {
legend?: string
}

export interface AxisProps<Value extends AxisValue = any> {
axis: 'x' | 'y'
scale: AnyScale
x?: number
y?: number
length: number
ticksPosition: 'before' | 'after'
tickValues?: TicksSpec<Value>
tickSize?: number
tickPadding?: number
tickRotation?: number
format?: string | ValueFormatter<Value>
renderTick?: (props: AxisTickProps<Value>) => JSX.Element
legend?: React.ReactNode
legendPosition?: 'start' | 'middle' | 'end'
legendOffset?: number
onClick?: (event: React.MouseEvent<SVGGElement, MouseEvent>, value: Value | string) => void
ariaHidden?: boolean
}

export interface AxisTickProps<Value extends AxisValue> {
tickIndex: number
value: Value
Expand Down