Skip to content

Commit 93b425c

Browse files
authored
Merge pull request #14 from coverfy/LOCATION-PROMPT
Conditional prompt
2 parents a028f6c + 25558aa commit 93b425c

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ ReactDom.render(
163163
| when | Boolean | false | Make render props `isShow` to be true or false when routing transition occurs. |
164164
| alertBeforeUnload | Boolean | false | Turn on the browser alert. Technically, when you **refresh** or **close** browser window, only browser itself can detect and popup alert for you. If you want to remind the user when doing actions above, turn on this option. |
165165
| alertMessage | String | '' | Custom browser alert messages. Note that this option only works for **IE**. |
166+
| conditionalPrompt | func | | Custom callback to show the prompt conditionally based on the location. The function receives the location and you can return true to allow the transition or false to show the prompt. |
167+
166168
167169
**react-goodbye** will handle all of the code logic for you. Use provided `render props` to show whatever you want (modal, lightbox, dialog, popup, etc)
168170

src/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,19 @@ class GoodBye extends React.Component {
2727
}
2828

2929
render() {
30-
const { when, children } = this.props;
30+
const { when, children, conditionalPrompt } = this.props;
3131
return (
3232
<Fragment>
33-
<ReactRouterPrompt when={when} message="" />
33+
<ReactRouterPrompt
34+
when={when}
35+
message={location => {
36+
if (typeof conditionalPrompt === 'undefined' || conditionalPrompt(location) !== true) {
37+
return '';
38+
}
39+
40+
return true;
41+
}}
42+
/>
3443
<GoodByeContext.Consumer>
3544
{renderProps => children({ ...renderProps })}
3645
</GoodByeContext.Consumer>
@@ -41,6 +50,7 @@ class GoodBye extends React.Component {
4150

4251
GoodBye.propTypes = {
4352
when: PropTypes.bool,
53+
conditionalPrompt: PropTypes.func,
4454
alertBeforeUnload: PropTypes.bool,
4555
alertMessage: PropTypes.string,
4656
children: PropTypes.func.isRequired,

0 commit comments

Comments
 (0)