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

[core] fix Toaster type backcompat, better OverlayToaster docs #6165

Merged
merged 6 commits into from
May 17, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
distinguish between ref callbacks and objects
  • Loading branch information
adidahiya committed May 17, 2023
commit 4824b2e9e5fcce97895a84c2f0d2cff1eab6bd0f
2 changes: 1 addition & 1 deletion packages/core/src/components/toast/toast.md
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ There are three ways to use __OverlayToaster__:

1. `OverlayToaster.create(props)` static method returns a new `ToasterInstance` instance. Use the instance method `toaster.show()` to manipulate this instance. __(recommended)__
1. `<OverlayToaster><Toast />...</OverlayToaster>`: Render a `<OverlayToaster>` element with React `children`.
1. `<OverlayToaster ref={(ref: ToasterInstance) => ref.show({ ...toast })} />`: Render a `<OverlayToaster>` element and use the `ref` prop to access its instance methods.
1. `<OverlayToaster ref={(ref: ToasterInstance) => ref.show({ ...toast })} />`: Render a `<OverlayToaster>` element and use a `ref` callback to access its instance methods. Note that if you use a ref object, you will have to use the more specific `OverlayToaster` type (e.g. `const myToaster = React.createRef<OverlayToaster>()`).

<div class="@ns-callout @ns-intent-primary @ns-icon-info-sign">
<h5 class="@ns-heading">Working with multiple toasters</h5>
4 changes: 1 addition & 3 deletions packages/core/src/components/toast/toaster.tsx
Original file line number Diff line number Diff line change
@@ -145,7 +145,7 @@ export class OverlayToaster
const toaster = ReactDOM.render<OverlayToasterProps>(
<OverlayToaster {...props} usePortal={false} />,
containerElement,
) as OverlayToaster;
) as OverlayToaster as ToasterInstance;
if (toaster == null) {
throw new Error(TOASTER_CREATE_NULL);
}
@@ -277,7 +277,5 @@ export const Toaster = OverlayToaster;
/** @deprecated use the new, more specific type `ToasterInstance` instead (forwards-compatible with v5) */
// eslint-disable-next-line @typescript-eslint/no-redeclare
export type Toaster = OverlayToaster;
// eslint-disable-next-line deprecation/deprecation
Toaster.displayName = `${DISPLAYNAME_PREFIX}.Toaster`;
/** @deprecated use `OverlayToasterProps` instead */
export type IToasterProps = OverlayToasterProps;
18 changes: 18 additions & 0 deletions packages/core/test/toast/toasterTests.tsx
Original file line number Diff line number Diff line change
@@ -188,6 +188,24 @@ describe("OverlayToaster", () => {
mount(React.createElement(LifecycleToaster));
});

it("ref callback is assignable to ToasterInstance", () => {
const handleToasterRef = (_toaster: ToasterInstance | null) => {
/* no-op */
};
const refSpy = spy(handleToasterRef);
mount(<OverlayToaster ref={refSpy} />);
assert.isTrue(refSpy.calledOnce);
});

it("ref object is assignable to OverlayToaster", () => {
const toasterInstance = React.createRef<ToasterInstance>();
const overlayToaster = React.createRef<OverlayToaster>();
// @ts-expect-error
const invalidToaster = <OverlayToaster ref={toasterInstance} />;
mount(<OverlayToaster ref={overlayToaster} />);
assert.isDefined(overlayToaster.current);
});

// this type compatibility test can be removed in Blueprint v5.0
it("ref is backwards-compatible with (deprecated) Toaster type", () => {
// N.B. without `export type Toaster = ...`, the following `Toaster` reference will be invalid