Skip to content

Commit

Permalink
fix(axes): update some types
Browse files Browse the repository at this point in the history
  • Loading branch information
wyze committed Apr 30, 2021
1 parent 934a299 commit b5d5f0f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
5 changes: 1 addition & 4 deletions packages/axes/src/components/AxisTick.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ export const AxisTick = <Value extends AxisValue>({
}: AxisTickProps<Value>) => {
const theme = useTheme()

let value = _value
if (typeof format === 'function') {
value = format(value)
}
const value = format?.(_value) ?? _value

const props = useMemo(() => {
const style = { opacity: animatedProps.opacity }
Expand Down
6 changes: 3 additions & 3 deletions packages/axes/src/compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
Line,
} from './types'

export const centerScale = (scale: ScaleWithBandwidth) => {
export const centerScale = <Value>(scale: ScaleWithBandwidth) => {
const bandwidth = scale.bandwidth()

if (bandwidth === 0) return scale
Expand All @@ -55,7 +55,7 @@ export const centerScale = (scale: ScaleWithBandwidth) => {
offset = Math.round(offset)
}

return <T>(d: T) => (scale(d) ?? 0) + offset
return <T extends Value>(d: T) => (scale(d) ?? 0) + offset
}

const timeByType: Record<string, [CountableTimeInterval, CountableTimeInterval]> = {
Expand Down Expand Up @@ -161,7 +161,7 @@ export const computeCartesianTicks = <Value extends AxisValue>({
const text = { textX: 0, textY: 0 }

const isRTL = typeof document === 'object' ? document.dir === 'rtl' : false
let translate: (value: string | number) => Point
let translate: (value: Value) => Point
let textAlign: CanvasTextAlign = textProps.align.center
let textBaseline: CanvasTextBaseline = textProps.baseline.center

Expand Down
18 changes: 9 additions & 9 deletions packages/axes/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ export type ScaleWithBandwidth =
| (ScalePoint<any> & { type: 'point' })

export type AnyScale =
| (ScaleLinear<any, any> & { type: 'linear' })
| (ScaleOrdinal<any, any> & { type: 'ordinal' })
| (ScaleTime<any, any> & { useUTC: boolean; type: 'time' })
| (ScaleSymLog<any, any> & { type: 'symlog' })
| (ScaleLogarithmic<any, any> & { type: 'log' })
| (ScaleLinear<any, number> & { type: 'linear' })
| (ScaleOrdinal<any, number> & { type: 'ordinal' })
| (ScaleTime<any, number> & { useUTC: boolean; type: 'time' })
| (ScaleSymLog<any, number> & { type: 'symlog' })
| (ScaleLogarithmic<any, number> & { type: 'log' })
| ScaleWithBandwidth

export type TicksSpec<Value extends AxisValue> =
Expand All @@ -53,7 +53,7 @@ export type TicksSpec<Value extends AxisValue> =

export type AxisLegendPosition = 'start' | 'middle' | 'end'

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

export interface AxisProp<Value extends AxisValue> {
ticksPosition?: 'before' | 'after'
Expand Down Expand Up @@ -89,14 +89,14 @@ export interface AxisProps<Value extends AxisValue = any> {
legend?: React.ReactNode
legendPosition?: 'start' | 'middle' | 'end'
legendOffset?: number
onClick?: (event: React.MouseEvent<SVGGElement, MouseEvent>, value: Value) => void
onClick?: (event: React.MouseEvent<SVGGElement, MouseEvent>, value: Value | string) => void
ariaHidden?: boolean
}

export interface AxisTickProps<Value extends AxisValue> {
tickIndex: number
value: Value
format?: string | ValueFormatter<Value>
format?: ValueFormatter<Value>
x: number
y: number
lineX: number
Expand All @@ -112,7 +112,7 @@ export interface AxisTickProps<Value extends AxisValue> {
textTransform: string
transform: string
}>
onClick?: (event: React.MouseEvent<SVGGElement, MouseEvent>, value: Value) => void
onClick?: (event: React.MouseEvent<SVGGElement, MouseEvent>, value: Value | string) => void
}

export type Line = {
Expand Down

0 comments on commit b5d5f0f

Please sign in to comment.