• Stars
    star
    318
  • Rank 127,336 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple confirmation dialogs built on top of @mui/material and straightforward to use thanks to React Hooks

Material-UI confirm GitHub license npm version Actions Status Coverage Status

Confirming user choice is a good thing to do, it should also be easy to do.

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

Installation

npm install --save material-ui-confirm

Demo

Edit material-ui-confirm 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";

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";

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

  const handleClick = () => {
    confirm({ description: "This action is permanent!" })
      .then(() => {
        /* ... */
      })
      .catch(() => {
        /* ... */
      });
  };

  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
Name Type Default Description
defaultOptions object {} 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
Name Type Default Description
title ReactNode 'Are you sure?' Dialog title.
description ReactNode '' Dialog content, automatically wrapped in DialogContentText.
content ReactNode null Dialog content, same as description but not wrapped in DialogContentText. Supersedes description if present.
confirmationText ReactNode 'Ok' Confirmation button caption.
cancellationText ReactNode 'Cancel' Cancellation button caption.
dialogProps object {} Material-UI Dialog props.
dialogActionsProps object {} Material-UI DialogActions props.
confirmationButtonProps object {} Material-UI Button props for the confirmation button.
cancellationButtonProps object {} Material-UI Button props for the cancellation button.
titleProps object {} Material-UI DialogTitle props for the dialog title.
contentProps object {} Material-UI DialogContent props for the dialog content.
allowClose boolean true Whether natural close (escape or backdrop click) should close the dialog. When set to false force the user to either cancel or confirm explicitly.
confirmationKeyword string undefined If 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
confirmationKeywordTextFieldProps object {} Material-UI TextField props for the confirmation keyword textfield.
hideCancelButton boolean false Whether to hide the cancel button.
buttonOrder string[] ["cancel", "confirm"] Specify the order of confirm and cancel buttons.

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>
  );
};

More Repositories

1

meow

Multi-population evolutionary algorithms with support for GPU and distributed computing
Elixir
56
star
2

notebooks

A collection of Livebook notebooks
47
star
3

wca_statistics

Build kit for generating cubing-related rankings
Ruby
17
star
4

vega_lite_lv_example

Elixir
16
star
5

kino_lab

Experimental Kino widgets for Liveobok
Elixir
13
star
6

groupifier

Task and group management tool for WCA competition organizers.
JavaScript
11
star
7

reconstructions

A tool simplifying the process of reconstructing Rubik's Cube solves.
JavaScript
10
star
8

gerbil

Simulating mouse device with Raspberry PI 4 and computer vision
Python
7
star
9

solution-analyzer

A library for analyzing Rubik's Cube solutions.
JavaScript
7
star
10

md-collection-pagination

Angular Material Design component for paginating a collection.
JavaScript
6
star
11

taskerito

Manage your tasks while eating burrito
Elixir
5
star
12

rankings

A simple web app for creating WCA-like rankings for specific group of people.
JavaScript
5
star
13

alg-sets

Web app for storing and exploring cubing algorithms.
JavaScript
4
star
14

internationalize

Translating Rails apps made easy.
JavaScript
3
star
15

alg-drill

A tiny tool for practicing recognition of newly learnt cubing algorithms.
JavaScript
2
star
16

latex_example

Adding LaTeX support to ExDoc generated documentation
Elixir
1
star
17

dotfiles

Shell
1
star
18

ea_comparison

Example evolutionary algorithms implemented using different frameworks
Python
1
star
19

exla_precompiled

Precompiled EXLA binaries for common targets
Elixir
1
star
20

livebook_setups

Automated Livebook setups
Jupyter Notebook
1
star
21

mix_install_watcher

Automatic dependency recompilation for Mix.install/2
Elixir
1
star