Skip to content

Commit ce2b34e

Browse files
committed
[chore] added example with react-router.
1 parent 16c3dce commit ce2b34e

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

Diff for: examples/basic/app.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,34 @@ import ReactDOM from 'react-dom';
33
import Modal from 'react-modal';
44
import SimpleUsage from './simple_usage';
55
import MultipleModals from './multiple_modals';
6+
import ReactRouter from './react-router';
67

78
const appElement = document.getElementById('example');
89

910
Modal.setAppElement('#example');
1011

1112
const examples = [
1213
SimpleUsage,
13-
MultipleModals
14+
MultipleModals,
15+
ReactRouter
1416
];
1517

16-
function App(props) {
17-
return examples.map((example, key) => {
18-
const ExampleApp = example.app;
18+
class App extends Component {
19+
render() {
1920
return (
20-
<div key={key} className="example">
21-
<h3>{example.label}</h3>
22-
<ExampleApp />
21+
<div>
22+
{examples.map((example, key) => {
23+
const ExampleApp = example.app;
24+
return (
25+
<div key={key} className="example">
26+
<h3>{example.label}</h3>
27+
<ExampleApp />
28+
</div>
29+
);
30+
})}
2331
</div>
2432
);
25-
});
33+
}
2634
}
2735

28-
ReactDOM.render(<App/>, appElement);
36+
ReactDOM.render(<App />, appElement);

Diff for: examples/basic/react-router/index.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import PropTypes from 'prop-types';
2+
import React, { Component } from 'react';
3+
import createHistory from 'history/createBrowserHistory';
4+
import { Router, Route, Switch } from 'react-router';
5+
import { Link } from 'react-router-dom';
6+
import Modal from 'react-modal';
7+
8+
const history = createHistory();
9+
10+
const Content = label => () => <p>{`Content ${label}`}</p>;
11+
12+
const shouldOpenModal = locationPath => /\bmodal\b/.test(locationPath);
13+
14+
const ReactRouterModal = props => (
15+
<Modal
16+
isOpen={shouldOpenModal(props.location.pathname)}
17+
onRequestClose={() => history.push("/basic")}>
18+
<div>
19+
<Link to="/basic/modal/a">Link A</Link><br />
20+
<Link to="/basic/modal/b">Link B</Link>
21+
<div>
22+
<Switch location={props.location}>
23+
<Route path="/basic/modal/a" component={Content('A')} />
24+
<Route path="/basic/modal/b" component={Content('B')} />
25+
</Switch>
26+
</div>
27+
</div>
28+
</Modal>
29+
);
30+
31+
class App extends Component {
32+
render() {
33+
return (
34+
<Router history={history}>
35+
<div>
36+
<Link to="/basic/modal" className="btn btn-primary">Modal</Link>
37+
<Route path="/basic/modal" component={ReactRouterModal} />
38+
</div>
39+
</Router>
40+
);
41+
}
42+
}
43+
44+
export default {
45+
label: "#3. react-modal and react-router.",
46+
app: App
47+
};

Diff for: package.json

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
"npm-run-all": "^4.1.1",
4949
"react": "^15.6.1",
5050
"react-dom": "^15.6.1",
51+
"react-router": "^4.2.0",
52+
"react-router-dom": "^4.2.2",
5153
"should": "^13.1.0",
5254
"sinon": "next",
5355
"uglify-js": "3.1.1",

Diff for: webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = {
2929
output: {
3030
filename: '[name].js',
3131
chunkFilename: '[id].chunk.js',
32-
path: 'examples/__build__',
32+
path: path.resolve(__dirname, './examples/__build__'),
3333
publicPath: '/__build__/'
3434
},
3535

@@ -46,7 +46,7 @@ module.exports = {
4646
},
4747

4848
plugins: [
49-
new webpack.optimize.CommonsChunkPlugin('shared.js'),
49+
new webpack.optimize.CommonsChunkPlugin('shared'),
5050
new webpack.LoaderOptionsPlugin({ debug: true })
5151
]
5252

0 commit comments

Comments
 (0)