This repository has been archived by the owner on May 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Add a fusion react-router plugin #2
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 @@ | ||
module.exports = { | ||
babel: { | ||
presets: [ | ||
require.resolve('babel-preset-react'), | ||
], | ||
} | ||
}; |
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,3 @@ | ||
module.exports = { | ||
extends: [require.resolve('eslint-config-fusion')], | ||
}; |
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,9 @@ | ||
node_modules/ | ||
dist/ | ||
dist-tests/ | ||
coverage/ | ||
.nyc_output/ | ||
|
||
.DS_Store | ||
npm-debug.log | ||
yarn-error.log |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 Uber Technologies, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,190 @@ | ||
# fusion-plugin-react-router | ||
|
||
The `fusion-plugin-react-router` package provides a universal router plugin for React. | ||
|
||
--- | ||
|
||
### Installation | ||
|
||
```sh | ||
yarn add fusion-plugin-react-router | ||
``` | ||
|
||
--- | ||
|
||
### Example | ||
|
||
```jsx | ||
// src/main.js | ||
import App from 'fusion-react'; | ||
import Router from 'fusion-plugin-react-router'; | ||
import root from './components/root'; | ||
|
||
export default function start(App) { | ||
const app = new App(root); | ||
app.plugin(Router); | ||
return app; | ||
} | ||
|
||
// src/components/root.js | ||
import React from 'react'; | ||
import {Router, Route, Link, Switch, NotFound} from 'fusion-plugin-react-router'; | ||
|
||
const Home = () => <div>Hello</div>; | ||
const Test = () => <div>Test</div>; | ||
const PageNotFound = () => <NotFound><div>404</div></NotFound>; | ||
|
||
const root = ( | ||
<div> | ||
<ul> | ||
<li><Link to="/">Home</Link></li> | ||
<li><Link to="/test">Test</Link></li> | ||
<li><Link to="/404">404</Link></li> | ||
</ul> | ||
<Switch> | ||
<Route exact path="/" component={Home} /> | ||
<Route exact path="/test" component={Test} /> | ||
<Route component={PageNotFound} /> | ||
</Switch> | ||
</div> | ||
); | ||
export default root; | ||
``` | ||
|
||
--- | ||
|
||
### API | ||
|
||
- [getRouter](#getrouter) | ||
- [Router](#router) | ||
- [Route](#route) | ||
- [Link](#link) | ||
- [Switch](#switch) | ||
- [Status](#status) | ||
- [NotFound](#notfound) | ||
- [Redirect](#redirect) | ||
|
||
#### `getRouter` | ||
|
||
```jsx | ||
import getRouter from 'fusion-plugin-react-router'; | ||
|
||
app.plugin(getRouter, {EventEmitter}) | ||
``` | ||
|
||
- `EventEmitter: SingletonPlugin` - Optional. | ||
|
||
#### `Router` | ||
|
||
Configures a router and acts as a React context provider for routing concerns. You don't need to use a Router component if you use `getRouter` | ||
|
||
```jsx | ||
import {Router} from 'fusion-plugin-react-router'; | ||
|
||
<Router | ||
location={...} | ||
basename={...} | ||
context={...} | ||
onRoute={...} | ||
>{child}</Router> | ||
``` | ||
|
||
- `location: string` - Required. The current pathname. Should be `ctx.url` in a Fusion plugin, or `req.url` in the server or `location.pathname` in the client | ||
- `basename: string` - Optional. Defaults to `''`. A route prefix. | ||
- `context: {setCode: (string) => void}` - Optional. | ||
- `setCode: (string) => void` - Called when `<Status />` is mounted. Provides an HTTP status code. | ||
- `onRoute: ({page: string, title: string}) => void` - Optional. Called when a route change happens. Provides a pathname and a title. | ||
- `child: React.Element` - Required. | ||
|
||
#### `Route` | ||
|
||
Defines what gets rendered for a given route. Multiple routes can be rendered at the same time if they exist outside a `Switch` component. | ||
|
||
```jsx | ||
import {Router, Route} from 'fusion-plugin-react-router'; | ||
|
||
<Router> | ||
<Route exact component={component} path={...}>{children}</Route> | ||
</Router> | ||
``` | ||
|
||
- `exact: boolean` - Optional. Whether the route matches exact paths only. | ||
- `component: React.Component` - The component to render if the path matches the current URL. | ||
- `path: string` - Optional. The route to match. If not defined, and `exact` is not defined, acts as a catch-all route (e.g. for 404s) | ||
- `children: React.Children` - Optional. Pass-through children. Always render even if the route does not match. | ||
|
||
#### `Link` | ||
|
||
Similar to `<a>`, creates a link that routes using `history.pushState` rather than a page change. | ||
|
||
```jsx | ||
import {Router, Link} from 'fusion-plugin-react-router'; | ||
|
||
<Router> | ||
<Link to="{...}">{children}</Link> | ||
</Router> | ||
``` | ||
|
||
See the [react-router documentation](https://reacttraining.com/react-router/web/api/Link). | ||
|
||
#### `Switch` | ||
|
||
Renders the first child `Route` that matches the path. | ||
|
||
```jsx | ||
import {Router, Switch} from 'fusion-plugin-react-router'; | ||
|
||
<Router> | ||
<Switch>{children}</Switch> | ||
</Router> | ||
``` | ||
|
||
- `children: React.Children<Route>` - React children must be `Route` components. | ||
|
||
See the [react-router documentation](https://reacttraining.com/react-router/web/api/Switch). | ||
|
||
#### `Status` | ||
|
||
Signals to the `Router` context that an HTTP status change is required. | ||
|
||
```jsx | ||
import {Router, Route, Status} from 'fusion-plugin-react-router'; | ||
|
||
<Router> | ||
<Route component={() => <Status code={...}>{child}</Status>} /> | ||
</Router> | ||
``` | ||
|
||
- `code: number` - A HTTP Status code to be used if this component is mounted. The status code is sent to a `context.setCode` call in `Router` | ||
- `child: React.Element` - A React element | ||
|
||
#### `NotFound` | ||
|
||
Equivalent to `<Status code={404}></Status>` | ||
|
||
```jsx | ||
import {Router, Route, NotFound} from 'fusion-plugin-react-router'; | ||
|
||
<Router> | ||
<Route component={() => <NotFound>{child}</NotFound>} /> | ||
</Router> | ||
``` | ||
|
||
- `child: React.Element` - A React element | ||
|
||
#### `Redirect` | ||
|
||
Signals to the `Router` context to navigate to a new location. | ||
|
||
```jsx | ||
import {Router, Route, Redirect} from 'fusion-plugin-react-router'; | ||
|
||
<Router> | ||
<Route component={() => <Redirect to="/">{child}</Redirect>} /> | ||
</Router> | ||
``` | ||
|
||
- `to: string|object` - Required. A URL or location to redirect to. | ||
- `push: boolean` - Optional. When true, redirecting will push a new entry onto the history instead of replacing the current one. | ||
- `code: number` - Optional. A HTTP Status code to be used if this component is mounted. | ||
|
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,60 @@ | ||
{ | ||
"name": "fusion-plugin-react-router", | ||
"description": "The `fusion-plugin-react-router` package provides a universal router plugin for React.", | ||
"version": "0.1.8", | ||
"license": "MIT", | ||
"repository": "fusionjs/fusion-plugin-react-router", | ||
"files": [ | ||
"dist" | ||
], | ||
"main": "./dist/node.cjs.js", | ||
"module": "./dist/node.es.js", | ||
"browser": { | ||
"./dist/node.cjs.js": "./dist/browser.cjs.js", | ||
"./dist/node.es.js": "./dist/browser.es.js" | ||
}, | ||
"es2015": { | ||
"./dist/node.cjs.js": "./dist/node.cjs.es2015.js", | ||
"./dist/node.es.js": "./dist/node.es.es2015.js", | ||
"./dist/browser.cjs.js": "./dist/browser.cjs.es2015.js", | ||
"./dist/browser.es.js": "./dist/browser.es.es2015.js" | ||
}, | ||
"dependencies": { | ||
"history": "^4.6.3", | ||
"prop-types": "^15.5.8", | ||
"react-router-dom": "^4.1.1" | ||
}, | ||
"peerDependencies": { | ||
"react": "14.x - 16.x", | ||
"react-dom": "14.x - 16.x" | ||
}, | ||
"devDependencies": { | ||
"babel-eslint": "^8.0.0", | ||
"babel-preset-react": "^6.24.1", | ||
"create-universal-package": "^1.0.0-rc.14", | ||
"eslint": "^4.2.0", | ||
"eslint-config-fusion": "^0.1.2", | ||
"eslint-plugin-cup": "^1.0.0-rc.4", | ||
"eslint-plugin-flowtype": "^2.35.0", | ||
"eslint-plugin-prettier": "^2.1.2", | ||
"eslint-plugin-react": "^7.1.0", | ||
"fusion-plugin-universal-events": "^0.1.8", | ||
"prettier": "1.4.2", | ||
"react": "^16.0.0", | ||
"react-dom": "^16.0.0", | ||
"tape-cup": "^4.7.1", | ||
"unitest": "^1.0.0" | ||
}, | ||
"scripts": { | ||
"clean": "rm -rf dist", | ||
"lint": "eslint . --ignore-path .gitignore", | ||
"build-test": "rm -rf dist-tests && cup build-tests", | ||
"just-test": "node_modules/.bin/unitest --browser=dist-tests/browser.js --node=dist-tests/node.js", | ||
"test": "npm run build-test && npm run just-test", | ||
"transpile": "npm run clean && cup build", | ||
"prepublish": "npm run transpile" | ||
}, | ||
"engines": { | ||
"node": ">= 8.9.0" | ||
} | ||
} |
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,40 @@ | ||
// MIT License | ||
|
||
// Copyright (c) 2017 Uber Technologies, Inc. | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
/* eslint-env browser */ | ||
import test from 'tape-cup'; | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import {Router, Route, NotFound} from '../../browser'; | ||
|
||
test('noops', t => { | ||
const root = document.createElement('div'); | ||
const Hello = () => <NotFound><div>Hello</div></NotFound>; | ||
const el = ( | ||
<Router pageData={{}}> | ||
<Route component={Hello} /> | ||
</Router> | ||
); | ||
ReactDOM.render(el, root); | ||
t.ok(/Hello/.test(root.innerHTML), 'matches'); | ||
t.end(); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to add this:
otherwise browser tests will hang in CI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It did hang. Added, thanks!