Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
[react-async] Expose renderLoading on AsyncComponentType (#2684)
Browse files Browse the repository at this point in the history
  • Loading branch information
melnikov-s authored Sep 18, 2023
1 parent 439d34a commit 20cbe44
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/curvy-kings-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/react-async': minor
---

Expose renderLoading on AsyncComponentType
5 changes: 5 additions & 0 deletions packages/react-async/src/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ export function createAsyncComponent<
writable: false,
});

Reflect.defineProperty(FinalComponent, 'renderLoading', {
value: renderLoading,
writable: false,
});

return FinalComponent;
}

Expand Down
13 changes: 13 additions & 0 deletions packages/react-async/src/tests/component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ describe('createAsyncComponent()', () => {
expect(renderLoading).toHaveBeenCalledTimes(1);
expect(asyncComponent).not.toContainReactComponent(Loading);
});

it('exposes renderLoading on the async component', () => {
const renderLoading = jest.fn(() => <Loading />);

const resolvable = createResolvablePromise(ResolvedComponent);

const AsyncComponent = createAsyncComponent({
load: () => resolvable.promise,
renderLoading,
});

expect(AsyncComponent.renderLoading).toBe(renderLoading);
});
});

describe('error', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/react-async/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {ReactElement} from 'react';
import type {ReactElement, ReactNode} from 'react';
import type {Resolver} from '@shopify/async';
import type {IfAllOptionalKeys, NoInfer} from '@shopify/useful-types';

Expand Down Expand Up @@ -55,6 +55,7 @@ export interface AsyncComponentType<
Preload(props: PreloadOptions): React.ReactElement<{}> | null;
Prefetch(props: PrefetchOptions): React.ReactElement<{}> | null;
KeepFresh(props: KeepFreshOptions): React.ReactElement<{}> | null;
renderLoading: ((props: Props) => ReactNode) | undefined;
}

export type PreloadOptions<T> = T extends AsyncHookTarget<
Expand Down

0 comments on commit 20cbe44

Please sign in to comment.