1.1.11 • Published 3 years ago

react-router-navigation-confirm v1.1.11

Weekly downloads
34
License
MIT
Repository
github
Last release
3 years ago

Table of Contents

Installation

To install this component you can use npm or yarn:

$ npm install --save react-router-navigation-confirm
$ yarn add react-router-navigation-confirm

Demo

You can see demo and docs on this site.

General Usage

Basic example

To show confirmation dialog on every navigation you only need to include <HistoryListener/> provider-component to the root router of your app and <NavigationConfirmModal/> to any Route (inside <HistoryListener>) in which you want to display confirmation dialog:

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { NavigationConfirmModal, HistoryListener } from 'react-router-navigation-confirm';

import App from './App';


ReactDOM.render(
    <BrowserRouter>
        <div>
            <HistoryListener>
                <NavigationConfirmModal/>
            <HistoryListener/>
            <App />
        </div>
    </BrowserRouter>
    , document.getElementById('root')
);
  1. HistoryListener - the component-provider which start to listen all changes of history and help to understand when you go forward and when you go back in the navigation(NavigationConfirmModal and NavigationConfirm will not work without HistoryListener).

  2. NavigationConfirmModal - the component that displays confirmation dialog with to buttons (Confirm and Cancel) when you try to navigate to other page or Route.

Custom dialog

If you want to create custom dialog you can use NavigationConfirm confirm component:

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { NavigationConfirm, HistoryListener } from 'react-router-navigation-confirm';

import App from './App';

const children = ({ onConfirm, onCancel }) => (
    <div>
        <button onClick={ onConfirm }>Confirm</button>
        <button onClick={ onCancel }>Cancel</button>
    </div>
)


ReactDOM.render(
    <BrowserRouter>
        <div>
            <HistoryListener>
                <NavigationConfirm>
                    { children }
                </NavigationConfirm>
            </HistoryListener>
            <App />
        </div>
    </BrowserRouter>
    , document.getElementById('root')
);

NavigationConfirm is the component that displays custom children, you should provide function that takes object with onConfirm and onCancel and return some React element. You can use onConfirm to approve navigation ot onCancel function to reject it and stay on the same page / Route.

API

1. <HistoryListener/>

PropertyDescriptionDefaultTypeRequired
childrenReact element / node to render into the HistoryListener componentundefinedReact.ReactNodefalse

2. <NavigationConfirm/>

PropertyDescriptionDefaultTypeRequired
childrenfunction that takes object with onConfirm and onCancel fields-functions and returns React node to render when user navigate to other page / Routeno default({ onConfirm: function, onCancel: function }) => React.ReactNode;true
unloadMsgmessage to show in the confirmation dialog on page unload event (in new versions in browser message alwase takes default messges according browser)'msg'stringfalse
whencondition to render confirmation, you can hide or show dialog by pass true or false, or create custom behaviour using functiontrueboolean | ((location: Location, routeProps: RouteComponentProps) => boolean)false

3. <NavigationConfirmModal/>

PropertyDescriptionDefaultTypeRequired
childrenReact element / node to render into the NavigationConfirmModal component'Are you sure you want to leave this page?'React.ReactNodefalse
whencondition to render confirmation, you can hide or show dialog by pass true or false, or create custom behaviour using functiontrueboolean | ((location: Location, routeProps: RouteComponentProps) => boolean)false
onCancelfunction to call when user click on cancel buttonundefined() => voidfalse
onConfirmfunction to call when user click on confirm buttonundefined() => voidfalse
confirmTextText for confirm button'Confirm'stringfalse
cancelTextText for cancel button'Cancel'stringfalse
modalClassNamemodal class name'nc-modal'stringfalse
backdropClassNameclass name for modal backdrop'nc-modal__backdrop'stringfalse
contentClassNamecontent class name'nc-modal__content'stringfalse
bodyClassNameclass name for body container'nc-modal__body'stringfalse
footerClassNameclass name for modal footer'nc-modal__footer'stringfalse
buttonClassNameclass name for all modal buttons (applyed for confirm and cancel)'nc-modal__button'stringfalse
buttonConfirmClassNameconfirm button aditional style'confirm'stringfalse

License

MIT

1.1.11

3 years ago

1.1.7

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.0.2

4 years ago

1.0.0

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago