@tengo10/joy-ui-confirm
v1.0.1
Published
Simple confirmation dialogs built on top of @mui/joy
Downloads
40
Readme
Joy-UI confirm
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/joy and straightforward to use thanks to React Hooks. I adapted material-ui-confirm to work with the @mui/joy library.
Installation
npm install --save @tengo10/joy-ui-confirmUsage
Wrap your app inside the ConfirmProvider component.\
import React from "react";
import { ConfirmProvider } from "@tengo10/joy-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/joy/Button";
import { useConfirm } from "@tengo10/joy-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 | {} | Joy-UI ModalDialog props. |
| dialogActionsProps | object | {} | Joy-UI DialogActions props. |
| confirmationButtonProps | object | {} | Joy-UI Button props for the confirmation button. |
| cancellationButtonProps | object | {} | Joy-UI Button props for the cancellation button. |
| titleProps | object | {} | Joy-UI DialogTitle props for the dialog title. |
| contentProps | object | {} | Joy-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 |
| confirmationKeywordInputProps | object | {} | Joy-UI Input props for the confirmation keyword textfield. |
| acknowledgement | string | undefined | If provided shows the acknowledge checkbox with this string as checkbox label and disables the confirm button while the checkbox is unchecked. |
| acknowledgementCheckboxProps | object | {} | Joy-UI Checkbox props for the acknowledge checkbox. |
| 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>
);
};