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

feat(axis): Expose innerRef prop on the various Axis components #1749

Merged
merged 4 commits into from
Oct 10, 2023
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
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', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you very much for adding tests 🙏 💯

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);
});
});