-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add examples with Flowtype support + libdefs for redux and react-redux (
#1887) * libdefs * todos-flow example * fix react-redux libdef using the `$Supertype` magic type * add comment to the workaround and link to the relevant issue
- Loading branch information
Showing
23 changed files
with
736 additions
and
0 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,9 @@ | ||
[ignore] | ||
<PROJECT_ROOT>/node_modules/fbjs | ||
|
||
[include] | ||
|
||
[libs] | ||
../../flow-typed | ||
|
||
[options] |
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,11 @@ | ||
# See http://help.github.com/ignore-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
node_modules | ||
|
||
# production | ||
build | ||
|
||
# misc | ||
.DS_Store | ||
npm-debug.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Redux Todos Example | ||
|
||
This project template was built with [Create React App](https://github.com/facebookincubator/create-react-app). | ||
|
||
## Available Scripts | ||
|
||
In the project directory, you can run: | ||
|
||
### `npm start` | ||
|
||
Runs the app in the development mode.<br> | ||
Open [http://localhost:3000](http://localhost:3000) to view it in the browser. | ||
|
||
The page will reload if you make edits.<br> | ||
You will also see any lint errors in the console. | ||
|
||
### `npm run build` | ||
|
||
Builds the app for production to the `build` folder.<br> | ||
It correctly bundles React in production mode and optimizes the build for the best performance. | ||
|
||
The build is minified and the filenames include the hashes.<br> | ||
Your app is ready to be deployed! | ||
|
||
### `npm run eject` | ||
|
||
**Note: this is a one-way operation. Once you `eject`, you can’t go back!** | ||
|
||
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. | ||
|
||
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. | ||
|
||
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. | ||
|
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 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>Redux Todos Example</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<!-- | ||
This HTML file is a template. | ||
If you open it directly in the browser, you will see an empty page. | ||
You can add webfonts, meta tags, or analytics to this file. | ||
The build step will place the bundled scripts into the <body> tag. | ||
To begin the development, run `npm start` in this folder. | ||
To create a production bundle, use `npm run build`. | ||
--> | ||
</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,25 @@ | ||
{ | ||
"name": "todos", | ||
"version": "0.0.1", | ||
"private": true, | ||
"devDependencies": { | ||
"enzyme": "^2.4.1", | ||
"react-addons-test-utils": "^15.3.0", | ||
"react-scripts": "^0.4.0" | ||
}, | ||
"dependencies": { | ||
"react": "^15.3.0", | ||
"react-dom": "^15.3.0", | ||
"react-redux": "^4.4.5", | ||
"redux": "^3.5.2" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start", | ||
"build": "react-scripts build", | ||
"eject": "react-scripts eject", | ||
"test": "react-scripts test" | ||
}, | ||
"eslintConfig": { | ||
"extends": "./node_modules/react-scripts/config/eslint.js" | ||
} | ||
} |
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,26 @@ | ||
// @flow | ||
import type { Id, Text, VisibilityFilter, Action } from '../types' | ||
|
||
let nextTodoId: Id = 0 | ||
|
||
export const addTodo = (text: Text): Action => { | ||
return { | ||
type: 'ADD_TODO', | ||
id: nextTodoId++, | ||
text | ||
} | ||
} | ||
|
||
export const setVisibilityFilter = (filter: VisibilityFilter): Action => { | ||
return { | ||
type: 'SET_VISIBILITY_FILTER', | ||
filter | ||
} | ||
} | ||
|
||
export const toggleTodo = (id: Id): Action => { | ||
return { | ||
type: 'TOGGLE_TODO', | ||
id | ||
} | ||
} |
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,25 @@ | ||
import * as actions from './index' | ||
|
||
describe('todo actions', () => { | ||
it('addTodo should create ADD_TODO action', () => { | ||
expect(actions.addTodo('Use Redux')).toEqual({ | ||
type: 'ADD_TODO', | ||
id: 0, | ||
text: 'Use Redux' | ||
}) | ||
}) | ||
|
||
it('setVisibilityFilter should create SET_VISIBILITY_FILTER action', () => { | ||
expect(actions.setVisibilityFilter('active')).toEqual({ | ||
type: 'SET_VISIBILITY_FILTER', | ||
filter: 'active' | ||
}) | ||
}) | ||
|
||
it('toggleTodo should create TOGGLE_TODO action', () => { | ||
expect(actions.toggleTodo(1)).toEqual({ | ||
type: 'TOGGLE_TODO', | ||
id: 1 | ||
}) | ||
}) | ||
}) |
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,15 @@ | ||
// @flow | ||
import React from 'react' | ||
import Footer from './Footer' | ||
import AddTodo from '../containers/AddTodo' | ||
import VisibleTodoList from '../containers/VisibleTodoList' | ||
|
||
const App = () => ( | ||
<div> | ||
<AddTodo /> | ||
<VisibleTodoList /> | ||
<Footer /> | ||
</div> | ||
) | ||
|
||
export default App |
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,23 @@ | ||
// @flow | ||
import React from 'react' | ||
import FilterLink from '../containers/FilterLink' | ||
|
||
const Footer = () => ( | ||
<p> | ||
Show: | ||
{" "} | ||
<FilterLink filter="SHOW_ALL"> | ||
All | ||
</FilterLink> | ||
{", "} | ||
<FilterLink filter="SHOW_ACTIVE"> | ||
Active | ||
</FilterLink> | ||
{", "} | ||
<FilterLink filter="SHOW_COMPLETED"> | ||
Completed | ||
</FilterLink> | ||
</p> | ||
) | ||
|
||
export default Footer |
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,27 @@ | ||
// @flow | ||
import React from 'react' | ||
|
||
export type Props = { | ||
active: boolean, | ||
children?: React$Element<any>, | ||
onClick: () => void | ||
}; | ||
|
||
const Link = ({ active, children, onClick }: Props) => { | ||
if (active) { | ||
return <span>{children}</span> | ||
} | ||
|
||
return ( | ||
<a href="#" | ||
onClick={e => { | ||
e.preventDefault() | ||
onClick() | ||
}} | ||
> | ||
{children} | ||
</a> | ||
) | ||
} | ||
|
||
export default Link |
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,22 @@ | ||
// @flow | ||
import React from 'react' | ||
import type { Text } from '../types' | ||
|
||
export type Props = { | ||
onClick: () => void, | ||
completed: boolean, | ||
text: Text | ||
}; | ||
|
||
const Todo = ({ onClick, completed, text }: Props) => ( | ||
<li | ||
onClick={onClick} | ||
style={{ | ||
textDecoration: completed ? 'line-through' : 'none' | ||
}} | ||
> | ||
{text} | ||
</li> | ||
) | ||
|
||
export default Todo |
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,23 @@ | ||
// @flow | ||
import React from 'react' | ||
import Todo from './Todo' | ||
import type { Todos, Id } from '../types' | ||
|
||
export type Props = { | ||
todos: Todos, | ||
onTodoClick: (id: Id) => void | ||
}; | ||
|
||
const TodoList = ({ todos, onTodoClick }: Props) => ( | ||
<ul> | ||
{todos.map(todo => | ||
<Todo | ||
key={todo.id} | ||
{...todo} | ||
onClick={() => onTodoClick(todo.id)} | ||
/> | ||
)} | ||
</ul> | ||
) | ||
|
||
export default TodoList |
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,38 @@ | ||
// @flow | ||
import React from 'react' | ||
import { connect } from 'react-redux' | ||
import { addTodo } from '../actions' | ||
import type { Dispatch } from '../types' | ||
import type { Connector } from 'react-redux' | ||
|
||
type Props = { | ||
dispatch: Dispatch | ||
}; | ||
|
||
const AddTodo = ({ dispatch }) => { | ||
let input | ||
|
||
return ( | ||
<div> | ||
<form onSubmit={e => { | ||
e.preventDefault() | ||
if (!input.value.trim()) { | ||
return | ||
} | ||
dispatch(addTodo(input.value)) | ||
input.value = '' | ||
}}> | ||
<input ref={node => { | ||
input = node | ||
}} /> | ||
<button type="submit"> | ||
Add Todo | ||
</button> | ||
</form> | ||
</div> | ||
) | ||
} | ||
|
||
const connector: Connector<{}, Props> = connect() | ||
|
||
export default connector(AddTodo) |
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,32 @@ | ||
// @flow | ||
import { connect } from 'react-redux' | ||
import { setVisibilityFilter } from '../actions' | ||
import Link from '../components/Link' | ||
import type { Props } from '../components/Link' | ||
import type { State, Dispatch, VisibilityFilter } from '../types' | ||
import type { Connector } from 'react-redux' | ||
|
||
type OwnProps = { | ||
filter: VisibilityFilter | ||
}; | ||
|
||
const mapStateToProps = (state: State, ownProps) => { | ||
return { | ||
active: ownProps.filter === state.visibilityFilter | ||
} | ||
} | ||
|
||
const mapDispatchToProps = (dispatch: Dispatch, ownProps) => { | ||
return { | ||
onClick: () => { | ||
dispatch(setVisibilityFilter(ownProps.filter)) | ||
} | ||
} | ||
} | ||
|
||
const connector: Connector<OwnProps, Props> = connect( | ||
This comment has been minimized.
Sorry, something went wrong. |
||
mapStateToProps, | ||
mapDispatchToProps | ||
) | ||
|
||
export default connector(Link) |
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 @@ | ||
// @flow | ||
import { connect } from 'react-redux' | ||
import { toggleTodo } from '../actions' | ||
import TodoList from '../components/TodoList' | ||
import type { State, Dispatch } from '../types' | ||
import type { Connector } from 'react-redux' | ||
import type { Props } from '../components/TodoList' | ||
|
||
const getVisibleTodos = (todos, filter) => { | ||
switch (filter) { | ||
case 'SHOW_COMPLETED': | ||
return todos.filter(t => t.completed) | ||
case 'SHOW_ACTIVE': | ||
return todos.filter(t => !t.completed) | ||
case 'SHOW_ALL': | ||
default : | ||
return todos | ||
} | ||
} | ||
|
||
const mapStateToProps = (state: State) => { | ||
return { | ||
todos: getVisibleTodos(state.todos, state.visibilityFilter) | ||
} | ||
} | ||
|
||
const mapDispatchToProps = (dispatch: Dispatch) => { | ||
return { | ||
onTodoClick: (id) => { | ||
dispatch(toggleTodo(id)) | ||
} | ||
} | ||
} | ||
|
||
const connector: Connector<{}, Props> = connect( | ||
mapStateToProps, | ||
mapDispatchToProps | ||
) | ||
|
||
export default connector(TodoList) |
Oops, something went wrong.
I love this! I've been looking all over for this.
One thing though: when specifying
OwnProps
andProps
like this:I get the following error:
I have no idea what that means or what to do about it. Is this the intended usage for cleanly providing re-usable
OwnProps
andProps
between the container component and the component itself? What is?