1.6.0 • Published 7 months ago

react-confirm-service v1.6.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

react-confirm-service

Build npm

Provides a service to show alerts, confirmations, and choices in react apps.

Installation

npm install react-confirm-service

Basic Usage

First you start by using the ConfirmComponentHost in your application:

import { ConfirmComponentHost } from "react-confirm-service";

...

<MyApp>
    <ConfirmComponentHost
        renderAlert={props => (
            <Notification
                isOpen={props.isVisible}
                onClose={props.onClose}
            >
                {props.message}
            </Notification>
        )}
        renderConfirm={props => (
            <Dialog
                isOpen={props.isOpen}
                title={props.title}
            >
                {/* render content, buttons, ... */}
            </Dialog>
        )}
        renderChoice={props => {
            const options = props.options.map(option => (
                <li
                    key={option.key}
                    onClick={() => props.onConfirm(option)}
                >
                    {option.key}
                </li>
            ));

            return (
                <Dialog
                    isOpen={props.isOpen}
                    title={props.title}
                >
                    <>
                        <ul>
                            {options}
                        </ul>
                        <button onClick={props.onCancel}>Cancel</button>
                    </>
                </Dialog>
            );
        }}
    />
</MyApp>

The implementation depends on the UI components you want to use to show alerts, confirmation, and choice dialogs.

After that, you can use the ConfirmService anywhere in your application:

import { ConfirmService } from "react-confirm-service";

...

ConfirmService.alert("Something happened", "info");

...

ConfirmService.confirm({
    message: "Close without saving?"
})
    .then(/* Yes */)
    .catch(/* No */);

...

ConfirmService.choose({
    title: "Fruits",
    options: [
        { key: "Bananas" },
        { key: "Apples" },
        { key: "Pineapples" },
    ]
})
    .then(choice => /* ... */)
    .catch(/* cancelled */)

How to use the ConfirmComponentHost

The ConfirmComponentHost accepts the following props:

PropertyRequiredDescription
renderAlertyesProvide a function which renders the alert component. See renderAlert
renderConfirmyesProvide a function which renders the confirmation component. See renderConfirm
renderChoicenoProvide a function which renders the choice component. See renderChoice
stringsnoTakes an object to provide default values for yes, no, and cancel button captions. Use this to localize these texts.
alertDurationsnoYou can provide an object to set the durations of an alert for each severity in ms. The defaults are: info: 3000, success: 3000, warning: 10000, error: 10000.

You can use the ConfirmComponentHost in multiple places in your application. The ConfirmService will use the last one which was mounted.

renderAlert

renderAlert is a function with one parameter of type AlertRenderProps:

PropertyDescription
isVisibleAlert is shown or not.
messageThe message to display to the user.
durationHow long should the message be displayed, in ms.
severityThe severity of the alert. Use different icons and/or colors.
onCloseCall this function when the alert should close.

renderConfirm

renderConfirm is a function with one parameter of type ConfirmRenderProps:

PropertyDescription
isOpenIs the confirmation dialog opened?
titleThe optional title of the confirmation.
messageThe message of the confirmation.
confirmCaptionThe caption of the button to accept the confirmation.
onConfirmCall this function when the button to accept is pressed.
denyCaptionThe caption of the button to deny the confirmation. Do not display this button if the caption is an empty string ("").
onDenyCall this function when the button to deny is pressed.

renderChoice

renderChoice is a function with one parameter of type ChoiceRenderProps:

PropertyDescription
isOpenIs the choice dialog opened?
titleThe optional title of the choice.
optionsThe list of selectable options to show.
typeThe optional type of the options to distinguish when rendering.
cancelCaptionThe caption of the action to cancel the choice.
onConfirmCall this function when a choice is selected.
onCancelCall this function when the choice is cancelled.
extraOptional custom data.

How to use the ConfirmService

Alert

To show an alert to the user, call the alert function. It has the following parameters:

ParameterRequiredDescription
messageyesThe message to display.
severityOrOptionsyesThe severity of the alert or an options object.

Confirm

To show a confirmation to the user, use the confirm function. It takes one options parameter:

PropertyRequiredDescription
titlenoThe title of the confirmation.
messageyesThe message of the confirmation.
yesnoThe caption of the button to accept. If not provided the yes property of strings is used. The default is "Yes".
nonoThe caption of the button to deny. If not provided the no property of strings is used. The default is "No". If you pass null, the button is not displayed.

This function returns a Promise. It will be resolved if the confirmation is accepted and rejected if the confirmation is denied.

Choose

To show a choice to the user, use the choose function. It takes one options parameter:

PropertyRequiredDescription
titlenoThe title of the choice.
optionsyesThe possible choices.
typenoThe optional type of the options to distinguish when rendering.
cancelCaptionnoThe caption of the cancel action. If not provided the cancel property of strings is used. The default is "Cancel".
extranoOptional custom data.

This function returns a Promise. It will be resolved with the selected option and rejected if the choice is cancelled.

The choose function is a generic function. So you can provide a custom type for your options. The generic parameter is optional.

Your custom type must have a key property of type string | number.

interface CustomOption {
    key: string | number,
    data: MyData,
}

ConfirmService.choose<CustomOption>({
    options: [
        { key: "1", data: myData1 },
        { key: "2", data: myData2 },
        { key: "3", data: myData3 }
    ]
});
1.6.0

7 months ago

1.5.0

11 months ago

1.4.3

12 months ago

1.4.2

1 year ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.1.0

3 years ago