Skip to content

Commit

Permalink
Omit internal props for withBoundingRects
Browse files Browse the repository at this point in the history
This matches the prop handling of `withParentSize`
  • Loading branch information
darthmaim committed May 25, 2024
1 parent 6f3b8cc commit d57a3dc
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions packages/visx-bounds/src/enhancers/withBoundingRects.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint react/no-did-mount-set-state: 0, react/no-find-dom-node: 0 */
import React, { ComponentClass } from 'react';
import React from 'react';
import ReactDOM from 'react-dom';

const emptyRect = {
Expand All @@ -20,26 +20,41 @@ type rectShape = {
height: number;
};

export type WithBoundingRectsProps = {
getRects?: () => { rect: rectShape; parentRect: rectShape };
type WithBoundingRectsState = {
rect?: rectShape;
parentRect?: rectShape;
};

type WithBoundingRectsInternalProps = {
getRects?: () => { rect: rectShape; parentRect: rectShape };
nodeRef?: React.RefObject<HTMLElement>;
};

export default function withBoundingRects<Props extends object = {}>(
type WithBoundingRectsProvidedProps = WithBoundingRectsInternalProps & WithBoundingRectsState;

export type WithBoundingRectsProps = WithBoundingRectsProvidedProps;

type WithBoundingRectsComponentProps<P extends WithBoundingRectsProvidedProps> = Omit<
P,
keyof WithBoundingRectsProvidedProps
>;

export default function withBoundingRects<Props extends WithBoundingRectsProvidedProps>(
BaseComponent: React.ComponentType<Props>,
): ComponentClass<Props> {
return class WrappedComponent extends React.PureComponent<Props> {
): React.ComponentClass<WithBoundingRectsComponentProps<Props>> {
return class WrappedComponent extends React.Component<
WithBoundingRectsComponentProps<Props>,
WithBoundingRectsState
> {
static displayName = `withBoundingRects(${BaseComponent.displayName || ''})`;
state = {
rect: undefined,
parentRect: undefined,
};
node: HTMLElement | undefined | null;
nodeRef: React.RefObject<HTMLElement>;
constructor(props: Props) {
constructor(props: WithBoundingRectsComponentProps<Props>) {
super(props);
this.state = {
rect: undefined,
parentRect: undefined,
};
this.nodeRef = React.createRef();
this.getRects = this.getRects.bind(this);
}
Expand Down Expand Up @@ -72,7 +87,7 @@ export default function withBoundingRects<Props extends object = {}>(
nodeRef={this.nodeRef}
getRects={this.getRects}
{...this.state}
{...this.props}
{...(this.props as Props)}
/>
);
}
Expand Down

0 comments on commit d57a3dc

Please sign in to comment.