-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from 4Catalyzer/transition-hook
Add transition hook support
- Loading branch information
Showing
10 changed files
with
250 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Found Transition Hook Example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "transition-hook", | ||
"version": "0.1.0", | ||
"private": true, | ||
"devDependencies": { | ||
"react-scripts": "0.7.0" | ||
}, | ||
"dependencies": { | ||
"found": "../..", | ||
"react": "^15.3.2", | ||
"react-dom": "^15.3.2" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>Found Transition Hook Example</title> | ||
</head> | ||
|
||
<body> | ||
<div id="root"></div> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import createBrowserRouter from 'found/lib/createBrowserRouter'; | ||
import { routerShape } from 'found/lib/PropTypes'; | ||
import Link from 'found/lib/Link'; | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
function LinkItem(props) { | ||
// TODO: Remove the pragma once evcohen/eslint-plugin-jsx-a11y#81 ships. | ||
return ( | ||
<li> | ||
<Link // eslint-disable-line jsx-a11y/anchor-has-content | ||
{...props} | ||
activeStyle={{ fontWeight: 'bold' }} | ||
/> | ||
</li> | ||
); | ||
} | ||
|
||
const appPropTypes = { | ||
children: React.PropTypes.node, | ||
}; | ||
|
||
function App({ children }) { | ||
return ( | ||
<div> | ||
<ul> | ||
<LinkItem to="/" exact> | ||
Main | ||
</LinkItem> | ||
<LinkItem to="/other"> | ||
Other | ||
</LinkItem> | ||
</ul> | ||
|
||
{children} | ||
</div> | ||
); | ||
} | ||
|
||
App.propTypes = appPropTypes; | ||
|
||
const mainPropTypes = { | ||
router: routerShape.isRequired, | ||
}; | ||
|
||
class Main extends React.Component { | ||
constructor(props, context) { | ||
super(props, context); | ||
|
||
this.state = { | ||
transitionType: 'confirm', | ||
showCustomConfirm: false, | ||
}; | ||
|
||
this.removeTransitionHook = props.router.addTransitionHook( | ||
this.onTransition, | ||
); | ||
|
||
this.resolveCustomConfirm = null; | ||
} | ||
|
||
componentWillUnmount() { | ||
this.removeTransitionHook(); | ||
} | ||
|
||
onTransition = (location) => { | ||
switch (this.state.transitionType) { | ||
case 'confirm': | ||
return 'Confirm'; | ||
case 'customConfirm': | ||
// Handle the before unload case. | ||
return location ? this.showCustomConfirm() : 'Confirm'; | ||
case 'allow': | ||
return true; | ||
case 'block': | ||
return false; | ||
case 'delayedConfirm': | ||
// This won't prompt on before unload. | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, 1000, 'Confirm'); | ||
}); | ||
case 'delayedAllow': | ||
// This won't prompt on before unload. | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, 1000, true); | ||
}); | ||
default: | ||
return null; | ||
} | ||
}; | ||
|
||
onChangeSelect = (event) => { | ||
this.setState({ transitionType: event.target.value }); | ||
}; | ||
|
||
onClickYes = () => { | ||
this.resolveCustomConfirm(true); | ||
}; | ||
|
||
onClickNo = () => { | ||
this.resolveCustomConfirm(false); | ||
}; | ||
|
||
showCustomConfirm() { | ||
this.setState({ showCustomConfirm: true }); | ||
|
||
return new Promise((resolve) => { | ||
this.resolveCustomConfirm = (result) => { | ||
this.setState({ showCustomConfirm: false }); | ||
this.resolveCustomConfirm = null; | ||
resolve(result); | ||
}; | ||
}); | ||
} | ||
|
||
render() { | ||
const { transitionType, showCustomConfirm } = this.state; | ||
|
||
return ( | ||
<div> | ||
<label htmlFor="transition-type"> | ||
Transition type | ||
</label> | ||
{' '} | ||
<select | ||
id="transition-type" | ||
value={transitionType} | ||
onChange={this.onChangeSelect} | ||
> | ||
<option value="confirm">Confirm</option> | ||
<option value="customConfirm">Custom confirm</option> | ||
<option value="allow">Allow</option> | ||
<option value="block">Block</option> | ||
<option value="delayedConfirm">Delayed confirm</option> | ||
<option value="delayedAllow">Delayed allow</option> | ||
</select> | ||
|
||
{showCustomConfirm && ( | ||
<div> | ||
Confirm | ||
{' '} | ||
<button onClick={this.onClickYes}>Yes</button> | ||
<button onClick={this.onClickNo}>No</button> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
Main.propTypes = mainPropTypes; | ||
|
||
const BrowserRouter = createBrowserRouter({ | ||
historyOptions: { useBeforeUnload: true }, | ||
|
||
routeConfig: [ | ||
{ | ||
path: '/', | ||
Component: App, | ||
children: [ | ||
{ | ||
Component: Main, | ||
}, | ||
{ | ||
path: 'other', | ||
Component: () => <div>Other</div>, | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
|
||
ReactDOM.render( | ||
<BrowserRouter />, | ||
document.getElementById('root'), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters