Code Monkey home page Code Monkey logo

bezzubovegor / react-router-navigation-confirm Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 1.0 12.14 MB

A collection of components to display a custom confirmation dialog on navigation. More flexible solution to prevent than default react-router 'Prompt'

Home Page: https://bezzubovegor.github.io/react-router-navigation-confirm

License: MIT License

JavaScript 0.84% TypeScript 89.19% CSS 8.14% HTML 1.83%
dialog modal prompt react react-navigation-prompt react-prompt react-confirm router react-router user

react-router-navigation-confirm's Introduction

RRNC

React Router Navigation Confirm

Build Status License: MIT codecov

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/>

Property Description Default Type Required
children React element / node to render into the HistoryListener component undefined React.ReactNode false

2. <NavigationConfirm/>

Property Description Default Type Required
children function that takes object with onConfirm and onCancel fields-functions and returns React node to render when user navigate to other page / Route no default ({ onConfirm: function, onCancel: function }) => React.ReactNode; true
unloadMsg message to show in the confirmation dialog on page unload event (in new versions in browser message alwase takes default messges according browser) 'msg' string false
when condition to render confirmation, you can hide or show dialog by pass true or false, or create custom behaviour using function true `boolean ((location: Location, routeProps: RouteComponentProps) => boolean)`

3. <NavigationConfirmModal/>

Property Description Default Type Required
children React element / node to render into the NavigationConfirmModal component 'Are you sure you want to leave this page?' React.ReactNode false
when condition to render confirmation, you can hide or show dialog by pass true or false, or create custom behaviour using function true `boolean ((location: Location, routeProps: RouteComponentProps) => boolean)`
onCancel function to call when user click on cancel button undefined () => void false
onConfirm function to call when user click on confirm button undefined () => void false
confirmText Text for confirm button 'Confirm' string false
cancelText Text for cancel button 'Cancel' string false
modalClassName modal class name 'nc-modal' string false
backdropClassName class name for modal backdrop 'nc-modal__backdrop' string false
contentClassName content class name 'nc-modal__content' string false
bodyClassName class name for body container 'nc-modal__body' string false
footerClassName class name for modal footer 'nc-modal__footer' string false
buttonClassName class name for all modal buttons (applyed for confirm and cancel) 'nc-modal__button' string false
buttonConfirmClassName confirm button aditional style 'confirm' string false

License

MIT

react-router-navigation-confirm's People

Contributors

bezzubovegor avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

itzik-bd

react-router-navigation-confirm's Issues

Navigation confirm does not take location search

The isTheSameLocation current implementation is:

private isTheSameLocation = (nextLocation: Location): boolean => nextLocation.pathname === this.props.location.pathname;

However, it does not take into account the location.search so it misses blocking navigations.

Changing the implementation to the following fix this issue:

private isTheSameLocation = (nextLocation: Location): boolean => nextLocation.pathname === this.props.location.pathname && nextLocation.search === this.props.location.search;

Add tests / test coverage

  • Add tests for all parts of the libriary.
  • Coverage should be 100%
  • NavigationConfirm tests
  • NavigationConfirmModal tests
  • HistoryListener tests
  • history-service tests

Add demo of this package

  • Create react app with demos and docs
  • Add home page
  • Add api page
  • Add get started page
  • Add demo page
  • NavigationConfirm demos
  • NavigationConfirmModal demos

Add 'when' support to <NavigationConfirm/> component

Add when property that should be a function or boolean. This function should return true to show confirmation or false to not show it. The same behaviour and for boolean type.

If this prop has function type, then it should take Location as argument.

prop type:

 when: boolean | (location: Location) => boolean

README.md upgrade

  • Add logo
  • Add static code analytics
  • Install instructions
  • Add linter statistics

Support of hooks

Add support of new approach for react => React Hooks. Think about possibilities:

  1. Do we need an old variant with HOC?
  2. What parts can be improved?
  3. Will hooks make usage easier?
  4. How we can improve API?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.