Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Add a fusion react-router plugin #2

Merged
merged 2 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .cuprc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
babel: {
presets: [
require.resolve('babel-preset-react'),
],
}
};
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: [require.resolve('eslint-config-fusion')],
};
9 changes: 9 additions & 0 deletions .gitignore
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
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
dist: trusty
sudo: false
language: node_js
node_js: 8.8.0
node_js: 8.9.0
before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.2.1
- export PATH=$HOME/.yarn/bin:$PATH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add this:

before_script:
- export DISPLAY=':99.0'
- sh -e /etc/init.d/xvfb start

otherwise browser tests will hang in CI

Copy link
Contributor Author

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!

before_script:
- export DISPLAY=':99.0'
- sh -e /etc/init.d/xvfb start
cache:
yarn: true
before_deploy:
Expand All @@ -13,6 +16,7 @@ before_deploy:
deploy:
provider: script
script: npm-publish-prerelease
skip_cleanup: true
on:
tags: true
env:
Expand Down
21 changes: 21 additions & 0 deletions LICENSE
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.
190 changes: 190 additions & 0 deletions README.md
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.

60 changes: 60 additions & 0 deletions package.json
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"
}
}
40 changes: 40 additions & 0 deletions src/__tests__/__browser__/notfound.js
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();
});
Loading