npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rsuite/interactions

v2.2.0

Published

Call rsuite Modal at ease.

Downloads

386

Readme

RSuite Interactions

Call RSuite Modals like a boss.

npm GitHub Actions codecov

npm install @rsuite/interactions@next --save

Features

  • Easy to call out alert(), confirm(), prompt() styles modals as you already know how.
  • Await their return values.
  • Multiple calls are queued.

API

alert

Use it like you are using window.alert(), and you can await it.

const buyNewPhone = useCallback(async () => {
  await alert("Congrats! You've got a new iPhone!");
  console.log('alert closed');
}, []);

Signatures

alert(
  message?: React.ReactNode,
  modalConfig?: AlertModalProps
): Promise<void>;

interface AlertModalProps {
  okButtonText?: string;
  onOk?: (() => void) | (() => Promise<any>);
}
  • okButtonText: Customize "OK" button text.
  • onOk: Callback function when "OK" is clicked. If onOk returns a Promise, "OK" button shows loading status until the promise finishes.

confirm

Use it like you are using window.confirm() but await its return value.

const confirmSmashPhone = useCallback(async () => {
  if (await confirm('Are you sure you what to do this?')) {
    alert('Rest in pieces.');
  }
}, []);

Signatures

confirm(
  message?: React.ReactNode,
  modalConfig?: ConfirmModalProps
): Promise<boolean>;

interface ConfirmModalProps {
  okButtonText?: string;
  cancelButtonText?: string;
  onOk?: (() => void) | (() => Promise<any>);
  onCancel?: (isSubmitLoading?: boolean) => any;
  canCancelOnLoading?: boolean;
}
  • okButtonText: Customize "OK" button text.
  • okButtonDangerous: When set true, "OK" button is red colored.
  • cancelButtonText: Customize "Cancel" button text.
  • onOk: Callback function when "OK" is clicked. If onOk returns a Promise, "OK" button shows loading status until the promise finishes.
  • onCancel: Callback function when "Cancel" is clicked. If not provided, "Cancel" is disabled when "OK" is loading.
  • canCancelOnLoading: When onCancel is set, you can still use this option to force disable "Cancel" button.

prompt

Use it like you are using window.prompt() but await its return value.

const promptForName = useCallback(async () => {
  const name = await prompt('What is your name?');
  if (name) {
    alert(`It\'s ok, ${name}.`);
  }
}, []);

Signatures

prompt(
  message?: React.ReactNode,
  _default?: string,
  modalConfig?: PromptModalProps
): Promise<string | null>;

interface PromptModalProps {
  okButtonText?: string;
  cancelButtonText?: string;
  valdiate?: (inputValue: string) => void;
  onOk?: ((inputVal?: string) => void) | ((inputVal: string) => Promise<any>);
  onCancel?: (isSubmitLoading?: boolean) => any;
  canCancelOnLoading?: boolean;
  inputProps?: InputProps;
}
  • okButtonText: Customize "OK" button text.
  • okButtonDangerous: When set true, "OK" button is red colored.
  • cancelButtonText: Customize "Cancel" button text.
  • validate: Validate current input value. Disable OK button if validation fails.
  • onOk: Callback function when "OK" is clicked, receiving a string representing the user input. If onOk returns a Promise, "OK" button shows loading status until the promise finishes.
  • onCancel: Callback function when "Cancel" is clicked. If not provided, "Cancel" is disabled when "OK" is loading.
  • canCancelOnLoading: When onCancel is set, you can still use this option to force disable "Cancel" button.
  • inputProps: custom input properties, learn more information about InputProps from here

Testing

If you use @rsuite/interactions to call alert dialogs in your app, you can easily test it with @testing-library/react.

Say you want to show a confirm dialog when user clicks a button that will delete a post:

import { confirm } from '@rsuite/interactions';

function App() {
  async function confirmDeletePost(id) {
    if (await confirm('Are you sure?')) {
      await api.deletePost(id);
    }
  }

  return <button onClick={() => confirmDeletePost(1)}>Delete post 1</button>;
}

And you want to test that the dialog is shown when the button is clicked:

import { render, fireEvent, screen } from '@testing-library/react';

it('Should show a confirm dialog', () => {
  const { getByRole } = render(<App />);

  fireEvent.click(getByRole('button', { name: /delete post/i }));

  // Assume you're using Jest with @testing-library/jest-dom
  const dialog = screen.getByRole('alertdialog');
  expect(dialog).toBeVisible();
  // Assert on its a11y description
  expect(dialog).toHaveAccessibleDescription('Are you sure?');
  // Or if you're using jest-dom < 5.14, you can assert on its textContent
  expect(dialog).toHaveTextContent('Are you sure?');
});

License

MIT licensed