Skip to content

Commit

Permalink
feat(axis): Expose innerRef prop on the various Axis components (airb…
Browse files Browse the repository at this point in the history
…nb#1749)

* feat(axis): Expose innerRef prop on the Axis component

* feat(axis): Improve documentation for the innerRef prop

* feat(axis): Remove unnecessary import

* feat(axis): Retrigger build
  • Loading branch information
SheaJanke authored and Onxi95 committed Nov 21, 2023
1 parent e967322 commit 7d0529b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/visx-axis/src/axis/Axis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default function Axis<Scale extends AxisScale>({
hideAxisLine = false,
hideTicks = false,
hideZero = false,
innerRef,
left = 0,
numTicks = 10,
orientation = Orientation.bottom,
Expand Down Expand Up @@ -69,7 +70,7 @@ export default function Axis<Scale extends AxisScale>({
});

return (
<Group className={cx('visx-axis', axisClassName)} top={top} left={left}>
<Group className={cx('visx-axis', axisClassName)} innerRef={innerRef} top={top} left={left}>
{children({
...restProps,
axisFromPoint,
Expand Down
4 changes: 3 additions & 1 deletion packages/visx-axis/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { D3Scale, NumberLike, ScaleInput, ValueOf } from '@visx/scale';
import { TextProps } from '@visx/text/lib/Text';
import { ReactNode, SVGProps } from 'react';
import { ReactNode, Ref, SVGProps } from 'react';
import Orientation from './constants/orientation';

// In order to plot values on an axis, output of the scale must be number.
Expand Down Expand Up @@ -132,6 +132,8 @@ export type SharedAxisProps<Scale extends AxisScale> = CommonProps<Scale> & {
axisClassName?: string;
/** A left pixel offset applied to the entire axis. */
left?: number;
/** The ref to the outermost axis group element. */
innerRef?: Ref<SVGGElement>;
/** A [d3](https://github.com/d3/d3-scale) or [visx](https://github.com/airbnb/visx/tree/master/packages/visx-scale) scale function. */
scale: Scale;
/** An array of values that determine the number and values of the ticks. Falls back to `scale.ticks()` or `.domain()`. */
Expand Down
12 changes: 12 additions & 0 deletions packages/visx-axis/test/Axis.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { shallow, ShallowWrapper } from 'enzyme';
import { render } from '@testing-library/react';

import { Line } from '@visx/shape';
import { Text } from '@visx/text';
Expand Down Expand Up @@ -215,4 +216,15 @@ describe('<Axis />', () => {
expect(points.at(2).prop('from')).toEqual({ x: 10.5, y: 0 });
expect(points.at(2).prop('to')).toEqual({ x: 0.5, y: 0 });
});

test('should expose its ref via an innerRef prop', () => {
const fakeRef = React.createRef<SVGGElement>();
const { container } = render(
<svg>
<Axis {...axisProps} innerRef={fakeRef} />
</svg>,
);
const AxisGroupElement = container.querySelector('g.visx-axis');
expect(fakeRef.current).toBe(AxisGroupElement);
});
});

0 comments on commit 7d0529b

Please sign in to comment.