Skip to content

Commit

Permalink
add examples with Flowtype support + libdefs for redux and react-redux (
Browse files Browse the repository at this point in the history
#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
gcanti authored and timdorr committed Sep 19, 2016
1 parent 5051dbc commit 85e2368
Show file tree
Hide file tree
Showing 23 changed files with 736 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/todos-flow/.flowconfig
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]
11 changes: 11 additions & 0 deletions examples/todos-flow/.gitignore
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
34 changes: 34 additions & 0 deletions examples/todos-flow/README.md
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.

21 changes: 21 additions & 0 deletions examples/todos-flow/index.html
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>
25 changes: 25 additions & 0 deletions examples/todos-flow/package.json
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"
}
}
26 changes: 26 additions & 0 deletions examples/todos-flow/src/actions/index.js
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
}
}
25 changes: 25 additions & 0 deletions examples/todos-flow/src/actions/index.spec.js
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
})
})
})
15 changes: 15 additions & 0 deletions examples/todos-flow/src/components/App.js
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
23 changes: 23 additions & 0 deletions examples/todos-flow/src/components/Footer.js
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
27 changes: 27 additions & 0 deletions examples/todos-flow/src/components/Link.js
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
22 changes: 22 additions & 0 deletions examples/todos-flow/src/components/Todo.js
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
23 changes: 23 additions & 0 deletions examples/todos-flow/src/components/TodoList.js
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
38 changes: 38 additions & 0 deletions examples/todos-flow/src/containers/AddTodo.js
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)
32 changes: 32 additions & 0 deletions examples/todos-flow/src/containers/FilterLink.js
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.

Copy link
@faceyspacey

faceyspacey Jan 17, 2017

I love this! I've been looking all over for this.

One thing though: when specifying OwnProps and Props like this:

type OwnProps = {
  name: string,
}

type Props = {
  toggleSidebar: () => void,
}

type HamburgerProps = OwnProps & Props

function Hamburger({ name, toggleSidebar }: HamburgerProps) {
  return (
    <div>
      <IconButton
        name={name}
        onPress={toggleSidebar}
      />
    </div>
  )
}


const connector: Connector<OwnProps, Props> = connect(
  null,
  { toggleSidebar },
)

export default connector(Hamburger)

I get the following error:

file: 'file:///Users/jamesgillmore/React/apps/celebvidy-web/src/components/Sidebar/Hamburger.js'
severity: 'Error'
message: '[flow] function call (Function cannot be called on any member of intersection type intersection Member 1: function type Error: property `name` Property not found in object type Member 2: polymorphic type: function type Error: function Callable signature not found in statics of React$Component)'
at: '38,16'
source: 'flow'

I have no idea what that means or what to do about it. Is this the intended usage for cleanly providing re-usable OwnProps and Props between the container component and the component itself? What is?

mapStateToProps,
mapDispatchToProps
)

export default connector(Link)
40 changes: 40 additions & 0 deletions examples/todos-flow/src/containers/VisibleTodoList.js
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)
Loading

0 comments on commit 85e2368

Please sign in to comment.