Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandofranca committed Feb 4, 2018
0 parents commit fd8e2d7
Show file tree
Hide file tree
Showing 41 changed files with 10,382 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"presets": [
"react",
"es2015",
"stage-0"
]
}
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/node_modules/
/dist/
/dist-components/
/__tests__/
85 changes: 85 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"parser": "babel-eslint",
"rules": {
"eqeqeq": [2, "allow-null"],
"no-extra-semi": 2,
"semi": ["error", "always"],
"no-cond-assign": 2,
"accessor-pairs": 2,
"no-with": 2,
"no-sparse-arrays": 2,
"vars-on-top": 2,
"no-void": 2,
"no-undef": 2,
"no-caller": 2,
"no-new": 2,
"yoda": 2,
"no-whitespace-before-property": 2,
"no-inner-declarations": 2,
"no-ex-assign": 2,
"no-else-return": 2,
"no-dupe-keys": 2,
"constructor-super": 2,
"no-extra-boolean-cast": 2,
"no-obj-calls": 2,
"no-empty": 2,
"semi-spacing": ["error", {
"before": false, "after": true
}],
"keyword-spacing": ["error", {"overrides": {
"if": {"after": true},
"else": {"before": true, "after": true},
"for": {"after": false},
"while": {"after": false},
"function": {"after": true}
}}],
"space-before-function-paren": ["error", {
"anonymous": "never", "named": "never"
}],
"comma-dangle": [2, "never"],
"comma-style": [2, "last"],
"comma-spacing": ["error", {
"before": false, "after": true
}],
"quotes": [2, "single", "avoid-escape"],
"no-trailing-spaces": 2,
"no-lonely-if": 0,
"eol-last": 2,
"no-nested-ternary": 2,
"space-in-parens": [2, "never"],
"brace-style": [2, "1tbs", {
"allowSingleLine": true
}],
"object-curly-spacing": [2, "always", {
"objectsInObjects": true,
"arraysInObjects": true
}],
"computed-property-spacing": ["error", "never"],
"block-spacing": ["error", "always"],
"one-var": [2, {
"let": "always",
"const": "never"
}],
"no-spaced-func": 2,
"spaced-comment": 0,
"quote-props": [2, "as-needed", {
"keywords": true
}],
"linebreak-style": ["error", "unix"],
"guard-for-in": 2,
"prefer-const": 2,
"no-var": 2,
"prefer-arrow-callback": 2,
"no-const-assign": 2,
"arrow-parens": [2, "as-needed"],
"arrow-spacing": [2, {
"before": true,
"after": true
}]
},
"globals": {
"console": true,
"process": true,
"__DEV__": true
}
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules/
/.idea/
/coverage/
*.DS_Store
npm-debug.log
22 changes: 22 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/.idea/
/__tests__/
/gulp/
/src/example/
/src/component/js/
/coverage/
/docs/
/webpack/
/src/component/index.html
/src/component/index.js
demo.gif
.editorconfig
.babelrc
.eslintignore
.eslintrc
.gitignore
.scsslint.yml
.travis.yml
config.json
gulpfile.babel.js
webpack.config.babel.js
webpack.config.example.babel.js
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js
node_js:
- "6"
before_install:
- npm install -g gulp
- npm install -g eslint
- npm install -g babel-eslint
script:
- "npm run lint && npm test"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Fernando de França

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.
124 changes: 124 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# react-director-router
A versatile React Router based on [Director](https://github.com/flatiron/director)

## Features
- Auto wraps views with custom Higher Order Components.
- Inject route props into your views (route parameters, query parameters).
- Inject custom props into your views.
- Route handler functions be fired on route change and will receive route params, query params and injected props.
- Small footprint.

## Advantages
Makes your components cleaner by:

- discouraging to rely on `componentDidMount` for http operations or state management.
- avoiding repetitive "glue code" by auto wrapping HOCs.

By using route handlers your view components can be **more functional**, **more view related** and carry **less business logic**.

Therefore view component tests would be simpler.

## Demo ##
`yarn start-example`

## Usage ##

### Install
`yarn add react-director-router`

### App.js
```
import Router from '../../component'
import HomeView from '../Views/HomeView'
import BookWithStatus from '../Views/BookWithStatus'
import NotFoundView from '../Views/NotFoundView'
const routes = [
{
path:'/',
viewClass: HomeView,
handler: () => {
console.log('Home handler');
}
},
{
path:'/book/:status/:id',
viewClass: BookWithStatus,
handler: (params) => {
console.log('/book/:status/:id > ', params)
},
},
{
path:'/not-found',
viewClass: NotFoundView
},
]
const notFoundPath = '/not-found'
const customProps = {
foo: 'bar',
count: 9000,
userRoles: ['basic-user']
}
const HOCs = [
view => someHigherOrderComponent(view),
view => anotherHigherOrderComponent(view),
]
const middlewares = [
(injectedProps, params, queryParams, route) => {
console.log(':: Log Middleware ::', route.viewClass.name);
}
]
class App extends Component {
render() {
return (
<div className="App">
<Router
routes={routes}
notFoundPath={notFoundPath}
injectProps={customProps}
middlewares={middlewares}
viewHOCs={HOCs}
/>
</div>
)
}
}
export default App
```

### Middlewares
Middlewares are functions that can intercept every route change and are a great opportunity to restrict a route or redirect the navigation flow.

By returning false on any middleware function, the middleware execution sequence will be interrupted, the view will not render and the route handler will not run. You can then redirect the navigation flow acording to the desired business logic.

Another common use for a middleware is logging navigation activity.

#### Example

A role restriction middleware:

```
import { redirect } from 'react-director-router'
export default function roleRestrictedMiddleware(injectedProps, params, queryParams, route) {
if (route.requiredRoles && injectedProps.userRoles){
console.log(':: roleRestrictedMiddleware ::');
const isAllowed = injectedProps.userRoles === route.requiredRoles
if (!isAllowed){
redirect('restricted-content')
return isAllowed
}
}
}
```

## Roadmap
- Hash navigation
- Async routing
3 changes: 3 additions & 0 deletions __mocks__/styleMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// __mocks__/styleMock.js

module.exports = {};
50 changes: 50 additions & 0 deletions __tests__/Component-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'

jest.disableAutomock()

import React from 'react'
import { findDOMNode } from 'react-dom'
import { createRenderer, Simulate, renderIntoDocument } from 'react-dom/test-utils'
// import renderer from 'react-test-renderer'
// import { shallow, mount } from 'enzyme'
// import toJson from 'enzyme-to-json'

import Router, { redirect } from '../src/component'

import HomeView from '../src/example/Views/HomeView.js'
import BookWithStatus from '../src/example/Views/BookWithStatus.js'
import NotFoundView from '../src/example/Views/NotFoundView.js'

const routes = [
{
path:'/',
viewClass: HomeView,
handler: () => {
console.log('Home handler')
}
},
{
path:'/book/:status/:id',
viewClass: BookWithStatus,
handler: (params) => {
console.log('/book/:status/:id > ', params)
},
},
{
path:'/not-found',
viewClass: NotFoundView
},
]

const notFoundPath = '/not-found'

describe('Render', () => {
it('should render', () => {
const renderer = createRenderer()
renderer.render(
<Router routes={routes} notFoundPath={notFoundPath} />
)
const component = renderer.getRenderOutput()
expect(component.type.name).toBe('HomeView')
})
})
7 changes: 7 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"scripts": {
"fileName": "component",
"entry": "index",
"library": "Component"
}
}
Loading

0 comments on commit fd8e2d7

Please sign in to comment.