diff --git a/packages/visx-axis/src/axis/AxisRenderer.tsx b/packages/visx-axis/src/axis/AxisRenderer.tsx index 3f738bfa4..a3f5aa3eb 100644 --- a/packages/visx-axis/src/axis/AxisRenderer.tsx +++ b/packages/visx-axis/src/axis/AxisRenderer.tsx @@ -34,7 +34,7 @@ export default function AxisRenderer({ strokeWidth = 1, tickClassName, tickComponent, - tickLabelProps = (/** tickValue, index */) => defaultTextProps, + tickLabelProps = (/** tickValue, index, tickValues */) => defaultTextProps, tickLength = 8, tickStroke = '#222', tickTransform, @@ -42,7 +42,7 @@ export default function AxisRenderer({ ticksComponent = Ticks, }: AxisRendererProps) { // compute the max tick label size to compute label offset - const allTickLabelProps = ticks.map(({ value, index }) => tickLabelProps(value, index)); + const allTickLabelProps = ticks.map(({ value, index }) => tickLabelProps(value, index, ticks)); const maxTickLabelFontSize = Math.max( 10, ...allTickLabelProps.map(props => (typeof props.fontSize === 'number' ? props.fontSize : 0)), diff --git a/packages/visx-axis/src/types.ts b/packages/visx-axis/src/types.ts index 4617f9394..7b14828a7 100644 --- a/packages/visx-axis/src/types.ts +++ b/packages/visx-axis/src/types.ts @@ -19,7 +19,11 @@ export type TickFormatter = ( values: { value: T; index: number }[], ) => FormattedValue; -export type TickLabelProps = (value: T, index: number) => Partial; +export type TickLabelProps = ( + value: T, + index: number, + values: { value: T; index: number }[], +) => Partial; export type TickRendererProps = Partial & { x: number; diff --git a/packages/visx-axis/test/Axis.test.tsx b/packages/visx-axis/test/Axis.test.tsx index 3377e595d..a980d4218 100644 --- a/packages/visx-axis/test/Axis.test.tsx +++ b/packages/visx-axis/test/Axis.test.tsx @@ -17,16 +17,16 @@ const axisProps = { }; describe('', () => { - test('it should be defined', () => { + it('should be defined', () => { expect(Axis).toBeDefined(); }); - test('it should render with class .visx-axis', () => { + it('should render with class .visx-axis', () => { const wrapper = shallow(); expect(wrapper.prop('className')).toEqual('visx-axis'); }); - test('it should call children function with required args', () => { + it('should call children function with required args', () => { const mockFn = jest.fn(); shallow({mockFn}); const args = mockFn.mock.calls[0][0]; @@ -44,7 +44,7 @@ describe('', () => { expect(Object.keys(args.ticks[0])).toEqual(['value', 'index', 'from', 'to', 'formattedValue']); }); - test('it should set user-specified axisClassName, axisLineClassName, labelClassName, and tickClassName', () => { + it('should set user-specified axisClassName, axisLineClassName, labelClassName, and tickClassName', () => { const axisClassName = 'axis-test-class'; const axisLineClassName = 'axisline-test-class'; const labelClassName = 'label-test-class'; @@ -66,7 +66,7 @@ describe('', () => { expect(wrapper.find(`.${tickClassName}`).length).toBeGreaterThan(0); }); - test('it should pass the output of tickLabelProps to tick labels', () => { + it('should pass the output of tickLabelProps to tick labels', () => { const tickProps = { fontSize: 50, fill: 'magenta' }; const wrapper = shallow( tickProps} />); @@ -78,13 +78,22 @@ describe('', () => { expect.hasAssertions(); }); - test('it should call the tickLabelProps func with the signature (tickValue, index)', () => { + it('should call the tickLabelProps func with the signature (value, index, values)', () => { + expect.hasAssertions(); shallow( { + tickLabelProps={(value, index, values) => { expect(value).toEqual(expect.any(Number)); expect(index).toBeGreaterThan(-1); + expect(values).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + value: expect.any(Number), + index: expect.any(Number), + }), + ]), + ); return {}; }} />, @@ -93,7 +102,7 @@ describe('', () => { expect.hasAssertions(); }); - test('it should pass labelProps to the axis label', () => { + it('should pass labelProps to the axis label', () => { const labelProps = { fontSize: 50, fill: 'magenta' }; const wrapper = shallow(); @@ -101,7 +110,7 @@ describe('', () => { expect(label.find(Text).props()).toEqual(expect.objectContaining(labelProps)); }); - test('it should render the 0th tick if hideZero is false', () => { + it('should render the 0th tick if hideZero is false', () => { const wrapper = shallow(); expect( wrapper @@ -111,7 +120,7 @@ describe('', () => { ).toBe('visx-tick-0-0'); }); - test('it should not show 0th tick if hideZero is true', () => { + it('should not show 0th tick if hideZero is true', () => { const wrapper = shallow(); expect( wrapper @@ -121,7 +130,7 @@ describe('', () => { ).toBe('visx-tick-1-0'); }); - test('it should SHOW an axis line if hideAxisLine is false', () => { + it('should SHOW an axis line if hideAxisLine is false', () => { const wrapper = shallow(); expect( wrapper @@ -131,7 +140,7 @@ describe('', () => { ).toHaveLength(1); }); - test('it should HIDE an axis line if hideAxisLine is true', () => { + it('should HIDE an axis line if hideAxisLine is true', () => { const wrapper = shallow(); expect( wrapper @@ -141,12 +150,12 @@ describe('', () => { ).toHaveLength(0); }); - test('it should SHOW ticks if hideTicks is false', () => { + it('should SHOW ticks if hideTicks is false', () => { const wrapper = shallow(); expect(wrapper.children().find('.visx-axis-tick').length).toBeGreaterThan(0); }); - test('it should HIDE ticks if hideTicks is true', () => { + it('should HIDE ticks if hideTicks is true', () => { const wrapper = shallow(); expect( wrapper @@ -156,7 +165,7 @@ describe('', () => { ).toHaveLength(0); }); - test('it should render one tick for each value specified in tickValues', () => { + it('should render one tick for each value specified in tickValues', () => { let wrapper = shallow(); expect( wrapper @@ -173,7 +182,7 @@ describe('', () => { expect(wrapper.children().find('.visx-axis-tick')).toHaveLength(7); }); - test('it should use tickFormat to format ticks if passed', () => { + it('should use tickFormat to format ticks if passed', () => { const wrapper = shallow( 'test!!!'} />); expect( wrapper @@ -197,7 +206,7 @@ describe('', () => { ).toBe('0'); }); - test('it should use center if scale is band', () => { + it('should use center if scale is band', () => { const wrapper = shallow( ', () => { - test('it should be defined', () => { + it('should be defined', () => { expect(AxisBottom).toBeDefined(); }); - test('it should render with class .visx-axis-bottom', () => { + it('should render with class .visx-axis-bottom', () => { const wrapper = shallow(); expect(wrapper.prop('axisClassName')).toEqual('visx-axis-bottom'); }); - test('it should set user-specified axisClassName, axisLineClassName, labelClassName, and tickClassName', () => { + it('should set user-specified axisClassName, axisLineClassName, labelClassName, and tickClassName', () => { const axisClassName = 'axis-test-class'; const axisLineClassName = 'axisline-test-class'; const labelClassName = 'label-test-class'; @@ -45,29 +45,29 @@ describe('', () => { expect(axis.prop('tickClassName')).toBe(tickClassName); }); - test('it should default labelOffset prop to 8', () => { + it('should default labelOffset prop to 8', () => { const wrapper = shallow(); expect(wrapper.prop('labelOffset')).toEqual(8); }); - test('it should set labelOffset prop', () => { + it('should set labelOffset prop', () => { const labelOffset = 3; const wrapper = shallow(); expect(wrapper.prop('labelOffset')).toEqual(labelOffset); }); - test('it should default tickLength prop to 8', () => { + it('should default tickLength prop to 8', () => { const wrapper = shallow(); expect(wrapper.prop('tickLength')).toEqual(8); }); - test('it should set tickLength prop', () => { + it('should set tickLength prop', () => { const tickLength = 15; const wrapper = shallow(); expect(wrapper.prop('tickLength')).toEqual(tickLength); }); - test('it should set label prop', () => { + it('should set label prop', () => { const label = 'test'; const wrapper = shallow().dive(); const text = wrapper.find('.visx-axis-label'); diff --git a/packages/visx-axis/test/AxisLeft.test.tsx b/packages/visx-axis/test/AxisLeft.test.tsx index 2be583dbe..df53a2e31 100644 --- a/packages/visx-axis/test/AxisLeft.test.tsx +++ b/packages/visx-axis/test/AxisLeft.test.tsx @@ -13,16 +13,16 @@ const axisProps = { }; describe('', () => { - test('it should be defined', () => { + it('should be defined', () => { expect(AxisLeft).toBeDefined(); }); - test('it should render with class .visx-axis-left', () => { + it('should render with class .visx-axis-left', () => { const wrapper = shallow(); expect(wrapper.prop('axisClassName')).toEqual('visx-axis-left'); }); - test('it should set user-specified axisClassName, axisLineClassName, labelClassName, and tickClassName', () => { + it('should set user-specified axisClassName, axisLineClassName, labelClassName, and tickClassName', () => { const axisClassName = 'axis-test-class'; const axisLineClassName = 'axisline-test-class'; const labelClassName = 'label-test-class'; @@ -45,29 +45,29 @@ describe('', () => { expect(axis.prop('tickClassName')).toBe(tickClassName); }); - test('it should default labelOffset prop to 36', () => { + it('should default labelOffset prop to 36', () => { const wrapper = shallow(); expect(wrapper.prop('labelOffset')).toEqual(36); }); - test('it should set labelOffset prop', () => { + it('should set labelOffset prop', () => { const labelOffset = 3; const wrapper = shallow(); expect(wrapper.prop('labelOffset')).toEqual(labelOffset); }); - test('it should default tickLength prop to 8', () => { + it('should default tickLength prop to 8', () => { const wrapper = shallow(); expect(wrapper.prop('tickLength')).toEqual(8); }); - test('it should set tickLength prop', () => { + it('should set tickLength prop', () => { const tickLength = 15; const wrapper = shallow(); expect(wrapper.prop('tickLength')).toEqual(tickLength); }); - test('it should set label prop', () => { + it('should set label prop', () => { const label = 'test'; const wrapper = shallow().dive(); const text = wrapper.find('.visx-axis-label'); diff --git a/packages/visx-axis/test/AxisRight.test.tsx b/packages/visx-axis/test/AxisRight.test.tsx index 5b63e4b7f..ee31b22d2 100644 --- a/packages/visx-axis/test/AxisRight.test.tsx +++ b/packages/visx-axis/test/AxisRight.test.tsx @@ -13,16 +13,16 @@ const axisProps = { }; describe('', () => { - test('it should be defined', () => { + it('should be defined', () => { expect(AxisRight).toBeDefined(); }); - test('it should render with class .visx-axis-right', () => { + it('should render with class .visx-axis-right', () => { const wrapper = shallow(); expect(wrapper.prop('axisClassName')).toEqual('visx-axis-right'); }); - test('it should set user-specified axisClassName, axisLineClassName, labelClassName, and tickClassName', () => { + it('should set user-specified axisClassName, axisLineClassName, labelClassName, and tickClassName', () => { const axisClassName = 'axis-test-class'; const axisLineClassName = 'axisline-test-class'; const labelClassName = 'label-test-class'; @@ -45,29 +45,29 @@ describe('', () => { expect(axis.prop('tickClassName')).toBe(tickClassName); }); - test('it should default labelOffset prop to 36', () => { + it('should default labelOffset prop to 36', () => { const wrapper = shallow(); expect(wrapper.prop('labelOffset')).toEqual(36); }); - test('it should set labelOffset prop', () => { + it('should set labelOffset prop', () => { const labelOffset = 3; const wrapper = shallow(); expect(wrapper.prop('labelOffset')).toEqual(labelOffset); }); - test('it should default tickLength prop to 8', () => { + it('should default tickLength prop to 8', () => { const wrapper = shallow(); expect(wrapper.prop('tickLength')).toEqual(8); }); - test('it should set tickLength prop', () => { + it('should set tickLength prop', () => { const tickLength = 15; const wrapper = shallow(); expect(wrapper.prop('tickLength')).toEqual(tickLength); }); - test('it should set label prop', () => { + it('should set label prop', () => { const label = 'test'; const wrapper = shallow().dive(); const text = wrapper.find('.visx-axis-label'); diff --git a/packages/visx-axis/test/AxisTop.test.tsx b/packages/visx-axis/test/AxisTop.test.tsx index c7d736fa3..828a15b7f 100644 --- a/packages/visx-axis/test/AxisTop.test.tsx +++ b/packages/visx-axis/test/AxisTop.test.tsx @@ -13,16 +13,16 @@ const axisProps = { }; describe('', () => { - test('it should be defined', () => { + it('should be defined', () => { expect(AxisTop).toBeDefined(); }); - test('it should render with class .visx-axis-top', () => { + it('should render with class .visx-axis-top', () => { const wrapper = shallow(); expect(wrapper.prop('axisClassName')).toEqual('visx-axis-top'); }); - test('it should set user-specified axisClassName, axisLineClassName, labelClassName, and tickClassName', () => { + it('should set user-specified axisClassName, axisLineClassName, labelClassName, and tickClassName', () => { const axisClassName = 'axis-test-class'; const axisLineClassName = 'axisline-test-class'; const labelClassName = 'label-test-class'; @@ -45,29 +45,29 @@ describe('', () => { expect(axis.prop('tickClassName')).toBe(tickClassName); }); - test('it should default labelOffset prop to 8', () => { + it('should default labelOffset prop to 8', () => { const wrapper = shallow(); expect(wrapper.prop('labelOffset')).toEqual(8); }); - test('it should set labelOffset prop', () => { + it('should set labelOffset prop', () => { const labelOffset = 3; const wrapper = shallow(); expect(wrapper.prop('labelOffset')).toEqual(labelOffset); }); - test('it should default tickLength prop to 8', () => { + it('should default tickLength prop to 8', () => { const wrapper = shallow(); expect(wrapper.prop('tickLength')).toEqual(8); }); - test('it should set tickLength prop', () => { + it('should set tickLength prop', () => { const tickLength = 15; const wrapper = shallow(); expect(wrapper.prop('tickLength')).toEqual(tickLength); }); - test('it should set label prop', () => { + it('should set label prop', () => { const label = 'test'; const wrapper = shallow().dive(); const text = wrapper.find('.visx-axis-label'); diff --git a/packages/visx-axis/test/Orientation.test.ts b/packages/visx-axis/test/Orientation.test.ts index 3e5aaba53..73ac295c3 100644 --- a/packages/visx-axis/test/Orientation.test.ts +++ b/packages/visx-axis/test/Orientation.test.ts @@ -1,19 +1,12 @@ import { Orientation } from '../src'; describe('Orientation', () => { - test('it should be defined', () => { - expect(Orientation).toBeDefined(); - }); - test('top should be defined', () => { - expect(Orientation.top).toBeDefined(); - }); - test('left should be defined', () => { - expect(Orientation.left).toBeDefined(); - }); - test('right should be defined', () => { - expect(Orientation.right).toBeDefined(); - }); - test('bottom should be defined', () => { - expect(Orientation.bottom).toBeDefined(); + it('should have keys for top/right/bottom/left', () => { + expect(Orientation).toEqual({ + top: expect.any(String), + right: expect.any(String), + bottom: expect.any(String), + left: expect.any(String), + }); }); }); diff --git a/packages/visx-xychart/src/components/axis/BaseAxis.tsx b/packages/visx-xychart/src/components/axis/BaseAxis.tsx index b4178c126..76dcd0336 100644 --- a/packages/visx-xychart/src/components/axis/BaseAxis.tsx +++ b/packages/visx-xychart/src/components/axis/BaseAxis.tsx @@ -1,5 +1,5 @@ import React, { useMemo, useContext } from 'react'; -import { AxisScale } from '@visx/axis'; +import { AxisScale, TickLabelProps } from '@visx/axis'; import { AxisProps as VxAxisProps } from '@visx/axis/lib/axis/Axis'; import { ScaleInput } from '@visx/scale'; import DataContext from '../../context/DataContext'; @@ -34,10 +34,10 @@ export default function BaseAxis({ [theme, orientation], ); - const tickLabelProps = useMemo( + const tickLabelProps = useMemo> | undefined>( () => props.tickLabelProps || axisStyles // construct from props + theme if possible - ? (value: ScaleInput, index: number) => + ? (value, index, values) => // by default, wrap vertical-axis tick labels within the allotted margin space // this does not currently account for axis label ({ @@ -46,10 +46,9 @@ export default function BaseAxis({ orientation === 'left' || orientation === 'right' ? margin?.[orientation] : undefined, - ...props.tickLabelProps?.(value, index), + ...props.tickLabelProps?.(value, index, values), }) : undefined, - [props.tickLabelProps, axisStyles, orientation, margin], );