1.0.3 • Published 1 year ago

material-ui-confirm-async v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Material-UI confirm async GitHub license

This package provides simple confirmation dialogs built on top of @mui/material, @mui/lab and @material-ui-confirm and straightforward to use thanks to React Hooks.

This package is specificaly made for integration with asynchronous functions, promises, and callbacks.

Installation

npm install --save material-ui-confirm-async

Demo

Edit material-ui-confirm-async demo

Usage

Wrap your app inside the ConfirmProvider component.\ Note: If you're using Material UI ThemeProvider, make sure ConfirmProvider is a child of it.

import React from "react";
import { ConfirmProvider } from "material-ui-confirm-async";

const App = () => {
  return <ConfirmProvider>{/* ... */}</ConfirmProvider>;
};

export default App;

Call the useConfirm hook wherever you need the confirm function.\ Note: A component calling useConfirm must be a child of ConfirmProvider.

import React from "react";
import Button from "@mui/material/Button";
import { useConfirm } from "material-ui-confirm-async";

const Item = () => {
  const confirm = useConfirm();

  const handleClick = () => {
    confirm({
      title: "Are you sure ?",
      description: "This action cannot be undone",
      confirmationText: "Accept",
      cancellationText: "Cancel",
      onConfirm: () =>
        new Promise((res, rej) => {
          setTimeout(() => {
            res("success");
          }, 2000);
        }),
    })
      .then((data) => {
        console.log(data);
      })
      .catch((e) => {
        console.log(e);
      });
  };

  return <Button onClick={handleClick}>Click</Button>;
};

export default Item;

API

ConfirmProvider

This component is required in order to render a dialog in the component tree.

Props
NameTypeDefaultDescription
defaultOptionsobject{}Overrides the default options used by confirm.

useConfirm() => confirm

This hook returns the confirm function.

confirm([options]) => Promise

This function opens a confirmation dialog and returns a promise representing the user choice (resolved on confirmation and rejected on cancellation).

Options
NameTypeDefaultDescription
titleReactNode'Are you sure?'Dialog title.
descriptionReactNode''Dialog content, automatically wrapped in DialogContentText.
contentReactNodenullDialog content, same as description but not wrapped in DialogContentText. Supersedes description if present.
confirmationTextReactNode'Ok'Confirmation button caption.
cancellationTextReactNode'Cancel'Cancellation button caption.
dialogPropsobject{}Material-UI Dialog props.
dialogActionsPropsobject{}Material-UI DialogActions props.
confirmationButtonPropsobject{}Material-UI Button props for the confirmation button.
cancellationButtonPropsobject{}Material-UI Button props for the cancellation button.
titlePropsobject{}Material-UI DialogTitle props for the dialog title.
contentPropsobject{}Material-UI DialogContent props for the dialog content.
allowClosebooleantrueWhether natural close (escape or backdrop click) should close the dialog. When set to false force the user to either cancel or confirm explicitly.
confirmationKeywordstringundefinedIf provided the confirmation button will be disabled by default and an additional textfield will be rendered. The confirmation button will only be enabled when the contents of the textfield match the value of confirmationKeyword
confirmationKeywordTextFieldPropsobject{}Material-UI TextField props for the confirmation keyword textfield.
hideCancelButtonbooleanfalseWhether to hide the cancel button.
buttonOrderstring[]["cancel", "confirm"]Specify the order of confirm and cancel buttons.
onConfirm() => Promise<any>nullA callback of confirmation

Useful notes

Confirm by pressing Enter

You can get this behavior by adding the autoFocus property to the confirmation button. This way the button is focused as soon as the dialog opens and hitting Enter naturally triggers a click.

Locally
const MyComponent = () => {
  // ...

  const handleClick = () => {
    confirm({ confirmationButtonProps: { autoFocus: true } })
      .then(() => {
        /* ... */
      })
      .catch(() => {
        /* ... */
      });
  };

  // ...
};
Globally
const App = () => {
  return (
    <ConfirmProvider
      defaultOptions={{
        confirmationButtonProps: { autoFocus: true },
      }}
    >
      {/* ... */}
    </ConfirmProvider>
  );
};
1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago