Skip to content

Commit 85e2368

Browse files
gcantitimdorr
authored andcommitted
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
1 parent 5051dbc commit 85e2368

23 files changed

+736
-0
lines changed

examples/todos-flow/.flowconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[ignore]
2+
<PROJECT_ROOT>/node_modules/fbjs
3+
4+
[include]
5+
6+
[libs]
7+
../../flow-typed
8+
9+
[options]

examples/todos-flow/.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# dependencies
4+
node_modules
5+
6+
# production
7+
build
8+
9+
# misc
10+
.DS_Store
11+
npm-debug.log

examples/todos-flow/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Redux Todos Example
2+
3+
This project template was built with [Create React App](https://github.com/facebookincubator/create-react-app).
4+
5+
## Available Scripts
6+
7+
In the project directory, you can run:
8+
9+
### `npm start`
10+
11+
Runs the app in the development mode.<br>
12+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13+
14+
The page will reload if you make edits.<br>
15+
You will also see any lint errors in the console.
16+
17+
### `npm run build`
18+
19+
Builds the app for production to the `build` folder.<br>
20+
It correctly bundles React in production mode and optimizes the build for the best performance.
21+
22+
The build is minified and the filenames include the hashes.<br>
23+
Your app is ready to be deployed!
24+
25+
### `npm run eject`
26+
27+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
28+
29+
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.
30+
31+
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.
32+
33+
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.
34+

examples/todos-flow/index.html

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>Redux Todos Example</title>
7+
</head>
8+
<body>
9+
<div id="root"></div>
10+
<!--
11+
This HTML file is a template.
12+
If you open it directly in the browser, you will see an empty page.
13+
14+
You can add webfonts, meta tags, or analytics to this file.
15+
The build step will place the bundled scripts into the <body> tag.
16+
17+
To begin the development, run `npm start` in this folder.
18+
To create a production bundle, use `npm run build`.
19+
-->
20+
</body>
21+
</html>

examples/todos-flow/package.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "todos",
3+
"version": "0.0.1",
4+
"private": true,
5+
"devDependencies": {
6+
"enzyme": "^2.4.1",
7+
"react-addons-test-utils": "^15.3.0",
8+
"react-scripts": "^0.4.0"
9+
},
10+
"dependencies": {
11+
"react": "^15.3.0",
12+
"react-dom": "^15.3.0",
13+
"react-redux": "^4.4.5",
14+
"redux": "^3.5.2"
15+
},
16+
"scripts": {
17+
"start": "react-scripts start",
18+
"build": "react-scripts build",
19+
"eject": "react-scripts eject",
20+
"test": "react-scripts test"
21+
},
22+
"eslintConfig": {
23+
"extends": "./node_modules/react-scripts/config/eslint.js"
24+
}
25+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// @flow
2+
import type { Id, Text, VisibilityFilter, Action } from '../types'
3+
4+
let nextTodoId: Id = 0
5+
6+
export const addTodo = (text: Text): Action => {
7+
return {
8+
type: 'ADD_TODO',
9+
id: nextTodoId++,
10+
text
11+
}
12+
}
13+
14+
export const setVisibilityFilter = (filter: VisibilityFilter): Action => {
15+
return {
16+
type: 'SET_VISIBILITY_FILTER',
17+
filter
18+
}
19+
}
20+
21+
export const toggleTodo = (id: Id): Action => {
22+
return {
23+
type: 'TOGGLE_TODO',
24+
id
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as actions from './index'
2+
3+
describe('todo actions', () => {
4+
it('addTodo should create ADD_TODO action', () => {
5+
expect(actions.addTodo('Use Redux')).toEqual({
6+
type: 'ADD_TODO',
7+
id: 0,
8+
text: 'Use Redux'
9+
})
10+
})
11+
12+
it('setVisibilityFilter should create SET_VISIBILITY_FILTER action', () => {
13+
expect(actions.setVisibilityFilter('active')).toEqual({
14+
type: 'SET_VISIBILITY_FILTER',
15+
filter: 'active'
16+
})
17+
})
18+
19+
it('toggleTodo should create TOGGLE_TODO action', () => {
20+
expect(actions.toggleTodo(1)).toEqual({
21+
type: 'TOGGLE_TODO',
22+
id: 1
23+
})
24+
})
25+
})
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @flow
2+
import React from 'react'
3+
import Footer from './Footer'
4+
import AddTodo from '../containers/AddTodo'
5+
import VisibleTodoList from '../containers/VisibleTodoList'
6+
7+
const App = () => (
8+
<div>
9+
<AddTodo />
10+
<VisibleTodoList />
11+
<Footer />
12+
</div>
13+
)
14+
15+
export default App
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @flow
2+
import React from 'react'
3+
import FilterLink from '../containers/FilterLink'
4+
5+
const Footer = () => (
6+
<p>
7+
Show:
8+
{" "}
9+
<FilterLink filter="SHOW_ALL">
10+
All
11+
</FilterLink>
12+
{", "}
13+
<FilterLink filter="SHOW_ACTIVE">
14+
Active
15+
</FilterLink>
16+
{", "}
17+
<FilterLink filter="SHOW_COMPLETED">
18+
Completed
19+
</FilterLink>
20+
</p>
21+
)
22+
23+
export default Footer
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// @flow
2+
import React from 'react'
3+
4+
export type Props = {
5+
active: boolean,
6+
children?: React$Element<any>,
7+
onClick: () => void
8+
};
9+
10+
const Link = ({ active, children, onClick }: Props) => {
11+
if (active) {
12+
return <span>{children}</span>
13+
}
14+
15+
return (
16+
<a href="#"
17+
onClick={e => {
18+
e.preventDefault()
19+
onClick()
20+
}}
21+
>
22+
{children}
23+
</a>
24+
)
25+
}
26+
27+
export default Link
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @flow
2+
import React from 'react'
3+
import type { Text } from '../types'
4+
5+
export type Props = {
6+
onClick: () => void,
7+
completed: boolean,
8+
text: Text
9+
};
10+
11+
const Todo = ({ onClick, completed, text }: Props) => (
12+
<li
13+
onClick={onClick}
14+
style={{
15+
textDecoration: completed ? 'line-through' : 'none'
16+
}}
17+
>
18+
{text}
19+
</li>
20+
)
21+
22+
export default Todo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @flow
2+
import React from 'react'
3+
import Todo from './Todo'
4+
import type { Todos, Id } from '../types'
5+
6+
export type Props = {
7+
todos: Todos,
8+
onTodoClick: (id: Id) => void
9+
};
10+
11+
const TodoList = ({ todos, onTodoClick }: Props) => (
12+
<ul>
13+
{todos.map(todo =>
14+
<Todo
15+
key={todo.id}
16+
{...todo}
17+
onClick={() => onTodoClick(todo.id)}
18+
/>
19+
)}
20+
</ul>
21+
)
22+
23+
export default TodoList
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// @flow
2+
import React from 'react'
3+
import { connect } from 'react-redux'
4+
import { addTodo } from '../actions'
5+
import type { Dispatch } from '../types'
6+
import type { Connector } from 'react-redux'
7+
8+
type Props = {
9+
dispatch: Dispatch
10+
};
11+
12+
const AddTodo = ({ dispatch }) => {
13+
let input
14+
15+
return (
16+
<div>
17+
<form onSubmit={e => {
18+
e.preventDefault()
19+
if (!input.value.trim()) {
20+
return
21+
}
22+
dispatch(addTodo(input.value))
23+
input.value = ''
24+
}}>
25+
<input ref={node => {
26+
input = node
27+
}} />
28+
<button type="submit">
29+
Add Todo
30+
</button>
31+
</form>
32+
</div>
33+
)
34+
}
35+
36+
const connector: Connector<{}, Props> = connect()
37+
38+
export default connector(AddTodo)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// @flow
2+
import { connect } from 'react-redux'
3+
import { setVisibilityFilter } from '../actions'
4+
import Link from '../components/Link'
5+
import type { Props } from '../components/Link'
6+
import type { State, Dispatch, VisibilityFilter } from '../types'
7+
import type { Connector } from 'react-redux'
8+
9+
type OwnProps = {
10+
filter: VisibilityFilter
11+
};
12+
13+
const mapStateToProps = (state: State, ownProps) => {
14+
return {
15+
active: ownProps.filter === state.visibilityFilter
16+
}
17+
}
18+
19+
const mapDispatchToProps = (dispatch: Dispatch, ownProps) => {
20+
return {
21+
onClick: () => {
22+
dispatch(setVisibilityFilter(ownProps.filter))
23+
}
24+
}
25+
}
26+
27+
const connector: Connector<OwnProps, Props> = connect(
28+
mapStateToProps,
29+
mapDispatchToProps
30+
)
31+
32+
export default connector(Link)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// @flow
2+
import { connect } from 'react-redux'
3+
import { toggleTodo } from '../actions'
4+
import TodoList from '../components/TodoList'
5+
import type { State, Dispatch } from '../types'
6+
import type { Connector } from 'react-redux'
7+
import type { Props } from '../components/TodoList'
8+
9+
const getVisibleTodos = (todos, filter) => {
10+
switch (filter) {
11+
case 'SHOW_COMPLETED':
12+
return todos.filter(t => t.completed)
13+
case 'SHOW_ACTIVE':
14+
return todos.filter(t => !t.completed)
15+
case 'SHOW_ALL':
16+
default :
17+
return todos
18+
}
19+
}
20+
21+
const mapStateToProps = (state: State) => {
22+
return {
23+
todos: getVisibleTodos(state.todos, state.visibilityFilter)
24+
}
25+
}
26+
27+
const mapDispatchToProps = (dispatch: Dispatch) => {
28+
return {
29+
onTodoClick: (id) => {
30+
dispatch(toggleTodo(id))
31+
}
32+
}
33+
}
34+
35+
const connector: Connector<{}, Props> = connect(
36+
mapStateToProps,
37+
mapDispatchToProps
38+
)
39+
40+
export default connector(TodoList)

0 commit comments

Comments
 (0)