Skip to content

Commit

Permalink
fix: Fix forwardRef warning in split panel
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Oct 16, 2024
1 parent ce7dff3 commit 6e82535
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 259 deletions.
38 changes: 1 addition & 37 deletions src/internal/widgets/__tests__/widgets.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,13 @@ import React from 'react';
import { render } from '@testing-library/react';

import { useVisualRefresh } from '../../../../lib/components/internal/hooks/use-visual-mode';
import { createWidgetizedComponent, createWidgetizedForwardRef } from '../../../../lib/components/internal/widgets';
import { createWidgetizedComponent } from '../../../../lib/components/internal/widgets';
import { describeWithAppLayoutFeatureFlagEnabled } from './utils';

const LoaderSkeleton = () => <div data-testid="loader">Loading...</div>;
const RealComponent = () => <div data-testid="content">Real content</div>;
const WidgetizedComponent = createWidgetizedComponent(RealComponent)(LoaderSkeleton);

const LoaderWithRef = React.forwardRef<HTMLDivElement>((props, ref) => (
<div ref={ref} data-testid="loader">
Loading...
</div>
));
const RealComponentWithRef = React.forwardRef<HTMLDivElement>((props, ref) => (
<div ref={ref} data-testid="content">
Real content
</div>
));
const WidgetizedComponentWithRef = createWidgetizedForwardRef<
{ children?: React.ReactNode },
HTMLDivElement,
typeof RealComponentWithRef
>(RealComponentWithRef)(LoaderWithRef);

function findLoader(container: HTMLElement) {
return container.querySelector('[data-testid="loader"]');
}
Expand Down Expand Up @@ -77,23 +61,3 @@ describe('Refresh design', () => {
});
});
});

describe('Ref handling', () => {
test('should forward ref to content', () => {
const ref = React.createRef<HTMLDivElement>();
const { container } = render(<WidgetizedComponentWithRef ref={ref} />);
expect(findContent(container)).toBeTruthy();
expect(findLoader(container)).toBeFalsy();
expect(ref.current).toHaveTextContent('Real content');
});

describeWithAppLayoutFeatureFlagEnabled(() => {
test('should forward ref to loader', () => {
const ref = React.createRef<HTMLDivElement>();
const { container } = render(<WidgetizedComponentWithRef ref={ref} />);
expect(findContent(container)).toBeFalsy();
expect(findLoader(container)).toBeTruthy();
expect(ref.current).toHaveTextContent('Loading...');
});
});
});
17 changes: 0 additions & 17 deletions src/internal/widgets/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,3 @@ export function createWidgetizedComponent<Component extends FunctionComponent<an
}) as Component;
};
}

export function createWidgetizedForwardRef<
Props,
RefType,
Component extends React.ForwardRefExoticComponent<Props & React.RefAttributes<RefType>>,
>(Implementation: Component) {
return (Loader?: Component): Component => {
return React.forwardRef<RefType, Props>((props, ref) => {
const isRefresh = useVisualRefresh();
if (isRefresh && getGlobalFlag('appLayoutWidget') && Loader) {
return <Loader ref={ref} {...(props as any)} />;
}

return <Implementation ref={ref} {...(props as any)} />;
}) as Component;
};
}
Loading

0 comments on commit 6e82535

Please sign in to comment.