-
-
Save adamaig/a593bb3a33bd7f48bf3d to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/torito
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script src="http://fb.me/react-0.14.3.js"></script> | |
| <script src="http://fb.me/react-dom-0.14.3.js"></script> | |
| <title>JS Bin</title> | |
| <meta charset="utf-8"> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.0.5/redux.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/4.0.6/react-redux.js"></script> | |
| </head> | |
| <body> | |
| <div id="root"></div> | |
| <script id="jsbin-javascript"> | |
| 'use strict'; | |
| var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | |
| function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | |
| var todo = function todo(state, action) { | |
| switch (action.type) { | |
| case 'ADD_TODO': | |
| return { | |
| id: action.id, | |
| text: action.text, | |
| completed: false | |
| }; | |
| case 'TOGGLE_TODO': | |
| if (state.id !== action.id) { | |
| return state; | |
| } | |
| return _extends({}, state, { | |
| completed: !state.completed | |
| }); | |
| default: | |
| return state; | |
| } | |
| }; | |
| var todos = function todos(state, action) { | |
| if (state === undefined) state = []; | |
| switch (action.type) { | |
| case 'ADD_TODO': | |
| return [].concat(_toConsumableArray(state), [todo(undefined, action)]); | |
| case 'TOGGLE_TODO': | |
| return state.map(function (t) { | |
| return todo(t, action); | |
| }); | |
| default: | |
| return state; | |
| } | |
| }; | |
| var visibilityFilter = function visibilityFilter(state, action) { | |
| if (state === undefined) state = 'SHOW_ALL'; | |
| switch (action.type) { | |
| case 'SET_VISIBILITY_FILTER': | |
| return action.filter; | |
| default: | |
| return state; | |
| } | |
| }; | |
| var getVisibleTodos = function getVisibleTodos(todos, filter) { | |
| switch (filter) { | |
| case 'SHOW_ALL': | |
| return todos; | |
| case 'SHOW_COMPLETED': | |
| return todos.filter(function (t) { | |
| return t.completed; | |
| }); | |
| case 'SHOW_ACTIVE': | |
| return todos.filter(function (t) { | |
| return !t.completed; | |
| }); | |
| default: | |
| return todos; | |
| } | |
| }; | |
| var _React = React; | |
| var Component = _React.Component; | |
| var _Redux = Redux; | |
| var createStore = _Redux.createStore; | |
| var combineReducers = _Redux.combineReducers; | |
| // Use connect to generate container components | |
| var _ReactRedux = ReactRedux; | |
| var connect = _ReactRedux.connect; | |
| var todoApp = combineReducers({ | |
| todos: todos, | |
| visibilityFilter: visibilityFilter | |
| }); | |
| var nextTodoId = 0; | |
| var addTodo = function addTodo(text) { | |
| return { | |
| type: 'ADD_TODO', | |
| text: text, | |
| id: nextTodoId++ | |
| }; | |
| }; | |
| var setVisibilityFilter = function setVisibilityFilter(filter) { | |
| return { | |
| type: 'SET_VISIBILITY_FILTER', | |
| filter: filter | |
| }; | |
| }; | |
| var toggleTodo = function toggleTodo(id) { | |
| return { | |
| type: 'TOGGLE_TODO', | |
| id: id | |
| }; | |
| }; | |
| // 2nd param received is props. in this case it is | |
| // destructured to just grab the store | |
| var AddTodo = function AddTodo(_ref) { | |
| var dispatch = _ref.dispatch; | |
| var input = undefined; | |
| return React.createElement( | |
| 'div', | |
| null, | |
| React.createElement('input', { ref: function (node) { | |
| input = node; | |
| } }), | |
| React.createElement( | |
| 'button', | |
| { onClick: function () { | |
| dispatch(addTodo(input.value)); | |
| input.value = ''; | |
| } }, | |
| 'Add Todo' | |
| ) | |
| ); | |
| }; | |
| // No need for the store in this container component, | |
| // so don't provide mapStateToProps in order to avoid subscribing, | |
| // then use default behavior of null for dispatch to get dispatch | |
| // passed as a prop to wrapped container | |
| AddTodo = connect()(AddTodo); | |
| var Link = function Link(_ref2) { | |
| var active = _ref2.active; | |
| var onClick = _ref2.onClick; | |
| var children = _ref2.children; | |
| if (active) { | |
| return React.createElement( | |
| 'span', | |
| null, | |
| children | |
| ); | |
| } | |
| return React.createElement( | |
| 'a', | |
| { href: '#', | |
| onClick: function (e) { | |
| e.preventDefault(); | |
| onClick(); | |
| } | |
| }, | |
| children | |
| ); | |
| }; | |
| var mapStateToLinkProps = function mapStateToLinkProps(state, ownProps) { | |
| return { | |
| active: ownProps.filter === state.visibilityFilter | |
| }; | |
| }; | |
| var mapDispatchToLinkProps = function mapDispatchToLinkProps(dispatch, ownProps) { | |
| return { | |
| onClick: function onClick() { | |
| dispatch(setVisibilityFilter(ownProps.filter)); | |
| } | |
| }; | |
| }; | |
| var FilterLink = connect(mapStateToLinkProps, mapDispatchToLinkProps)(Link); | |
| var Footer = function Footer() { | |
| return React.createElement( | |
| 'p', | |
| null, | |
| 'Show: ', | |
| ' ', | |
| React.createElement( | |
| FilterLink, | |
| { | |
| filter: 'SHOW_ALL' | |
| }, | |
| 'All' | |
| ), | |
| ' ', | |
| React.createElement( | |
| FilterLink, | |
| { | |
| filter: 'SHOW_ACTIVE' | |
| }, | |
| 'Active' | |
| ), | |
| ' ', | |
| React.createElement( | |
| FilterLink, | |
| { | |
| filter: 'SHOW_COMPLETED' | |
| }, | |
| 'Completed' | |
| ) | |
| ); | |
| }; | |
| var Todo = function Todo(_ref3) { | |
| var onClick = _ref3.onClick; | |
| var completed = _ref3.completed; | |
| var text = _ref3.text; | |
| return React.createElement( | |
| 'li', | |
| { | |
| onClick: onClick, | |
| style: { | |
| textDecoration: completed ? 'line-through' : 'none' | |
| } | |
| }, | |
| text | |
| ); | |
| }; | |
| var TodoList = function TodoList(_ref4) { | |
| var todos = _ref4.todos; | |
| var onTodoClick = _ref4.onTodoClick; | |
| return React.createElement( | |
| 'ul', | |
| null, | |
| todos.map(function (todo) { | |
| return React.createElement(Todo, _extends({ | |
| key: todo.id | |
| }, todo, { | |
| onClick: function () { | |
| return onTodoClick(todo.id); | |
| } | |
| })); | |
| }) | |
| ); | |
| }; | |
| var mapStateToTodoListProps = function mapStateToTodoListProps(state) { | |
| return { | |
| todos: getVisibleTodos(state.todos, state.visibilityFilter) | |
| }; | |
| }; | |
| var mapDispatchToTodoListProps = function mapDispatchToTodoListProps(dispatch) { | |
| return { | |
| onTodoClick: function onTodoClick(id) { | |
| dispatch(toggleTodo(id)); | |
| } | |
| }; | |
| }; | |
| // Generate container component | |
| var VisibleTodoList = connect(mapStateToTodoListProps, mapDispatchToTodoListProps)(TodoList); | |
| var _ReactRedux2 = ReactRedux; | |
| var Provider = _ReactRedux2.Provider; | |
| var TodoApp = function TodoApp() { | |
| return React.createElement( | |
| 'div', | |
| null, | |
| React.createElement(AddTodo, null), | |
| React.createElement(VisibleTodoList, null), | |
| React.createElement(Footer, null) | |
| ); | |
| }; | |
| ReactDOM.render(React.createElement( | |
| Provider, | |
| { store: createStore(todoApp) }, | |
| React.createElement(TodoApp, null) | |
| ), document.getElementById('root')); | |
| </script> | |
| <script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script src="//fb.me/react-0.14.3.js"><\/script> | |
| <script src="//fb.me/react-dom-0.14.3.js"><\/script> | |
| <title>JS Bin</title> | |
| <meta charset="utf-8"> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.0.5/redux.js"><\/script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/4.0.6/react-redux.js"><\/script> | |
| </head> | |
| <body> | |
| <div id="root"></div> | |
| </body> | |
| </html></script> | |
| <script id="jsbin-source-javascript" type="text/javascript"> | |
| const todo = (state, action) => { | |
| switch(action.type) { | |
| case 'ADD_TODO': | |
| return { | |
| id: action.id, | |
| text: action.text, | |
| completed: false | |
| }; | |
| case 'TOGGLE_TODO': | |
| if (state.id !== action.id) { | |
| return state; | |
| } | |
| return { | |
| ...state, | |
| completed: !state.completed | |
| }; | |
| default: | |
| return state; | |
| } | |
| }; | |
| const todos = (state = [], action) => { | |
| switch(action.type) { | |
| case 'ADD_TODO': | |
| return [ | |
| ...state, | |
| todo(undefined, action) | |
| ]; | |
| case 'TOGGLE_TODO': | |
| return state.map(t => todo(t, action)); | |
| default: | |
| return state; | |
| } | |
| }; | |
| const visibilityFilter = (state = 'SHOW_ALL', action ) => { | |
| switch (action.type) { | |
| case 'SET_VISIBILITY_FILTER': | |
| return action.filter; | |
| default: | |
| return state; | |
| } | |
| }; | |
| const getVisibleTodos = (todos, filter) => { | |
| switch(filter) { | |
| case 'SHOW_ALL': | |
| return todos; | |
| case 'SHOW_COMPLETED': | |
| return todos.filter(t => t.completed); | |
| case 'SHOW_ACTIVE': | |
| return todos.filter(t => !t.completed); | |
| default: | |
| return todos; | |
| } | |
| }; | |
| const {Component} = React; | |
| const {createStore, combineReducers} = Redux; | |
| // Use connect to generate container components | |
| const {connect} = ReactRedux; | |
| const todoApp = combineReducers({ | |
| todos: todos, | |
| visibilityFilter: visibilityFilter | |
| }); | |
| let nextTodoId = 0; | |
| const addTodo = (text) => { | |
| return { | |
| type: 'ADD_TODO', | |
| text: text, | |
| id: nextTodoId++ | |
| }; | |
| }; | |
| const setVisibilityFilter = (filter) => { | |
| return { | |
| type: 'SET_VISIBILITY_FILTER', | |
| filter: filter | |
| }; | |
| }; | |
| const toggleTodo = (id) => { | |
| return { | |
| type: 'TOGGLE_TODO', | |
| id | |
| }; | |
| }; | |
| // 2nd param received is props. in this case it is | |
| // destructured to just grab the store | |
| let AddTodo = ({dispatch}) => { | |
| let input; | |
| return ( | |
| <div> | |
| <input ref={node => { | |
| input = node | |
| }} /> | |
| <button onClick={() => { | |
| dispatch(addTodo(input.value)); | |
| input.value = ''; | |
| }}> | |
| Add Todo | |
| </button> | |
| </div> | |
| ); | |
| }; | |
| // No need for the store in this container component, | |
| // so don't provide mapStateToProps in order to avoid subscribing, | |
| // then use default behavior of null for dispatch to get dispatch | |
| // passed as a prop to wrapped container | |
| AddTodo = connect()(AddTodo); | |
| const Link = ({active, onClick, children}) => { | |
| if (active) { | |
| return <span>{children}</span>; | |
| } | |
| return ( | |
| <a href="#" | |
| onClick={e => { | |
| e.preventDefault(); | |
| onClick() | |
| }} | |
| > | |
| {children} | |
| </a> | |
| ); | |
| }; | |
| const mapStateToLinkProps = ( | |
| state, | |
| ownProps | |
| ) => { | |
| return { | |
| active: ownProps.filter === state.visibilityFilter | |
| }; | |
| }; | |
| const mapDispatchToLinkProps = ( | |
| dispatch, | |
| ownProps | |
| ) => { | |
| return { | |
| onClick: () => { | |
| dispatch(setVisibilityFilter(ownProps.filter)) | |
| } | |
| }; | |
| }; | |
| const FilterLink = connect( | |
| mapStateToLinkProps, | |
| mapDispatchToLinkProps | |
| )(Link); | |
| const Footer = () => ( | |
| <p> | |
| Show: {' '} | |
| <FilterLink | |
| filter='SHOW_ALL' | |
| > | |
| All | |
| </FilterLink> | |
| {' '} | |
| <FilterLink | |
| filter='SHOW_ACTIVE' | |
| > | |
| Active | |
| </FilterLink> | |
| {' '} | |
| <FilterLink | |
| filter='SHOW_COMPLETED' | |
| > | |
| Completed | |
| </FilterLink> | |
| </p> | |
| ); | |
| const Todo = ({onClick, completed, text}) => ( | |
| <li | |
| onClick={onClick} | |
| style={{ | |
| textDecoration: | |
| completed ? 'line-through' : 'none' | |
| }} | |
| > | |
| {text} | |
| </li> | |
| ); | |
| const TodoList = ({ | |
| todos, | |
| onTodoClick | |
| }) => ( | |
| <ul> | |
| {todos.map(todo => | |
| <Todo | |
| key={todo.id} | |
| {...todo} | |
| onClick={() => onTodoClick(todo.id)} | |
| /> | |
| )} | |
| </ul> | |
| ); | |
| const mapStateToTodoListProps = (state) => { | |
| return { | |
| todos: getVisibleTodos( | |
| state.todos, | |
| state.visibilityFilter | |
| ) | |
| }; | |
| }; | |
| const mapDispatchToTodoListProps = (dispatch) => { | |
| return { | |
| onTodoClick: (id) => { | |
| dispatch(toggleTodo(id)) | |
| } | |
| }; | |
| }; | |
| // Generate container component | |
| const VisibleTodoList = connect( | |
| mapStateToTodoListProps, | |
| mapDispatchToTodoListProps | |
| )(TodoList); | |
| const {Provider} = ReactRedux; | |
| const TodoApp = () => ( | |
| <div> | |
| <AddTodo /> | |
| <VisibleTodoList /> | |
| <Footer /> | |
| </div> | |
| ); | |
| ReactDOM.render( | |
| <Provider store={createStore(todoApp)}> | |
| <TodoApp /> | |
| </Provider>, | |
| document.getElementById('root') | |
| ); | |
| </script></body> | |
| </html> |
This file contains hidden or 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
| 'use strict'; | |
| var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | |
| function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | |
| var todo = function todo(state, action) { | |
| switch (action.type) { | |
| case 'ADD_TODO': | |
| return { | |
| id: action.id, | |
| text: action.text, | |
| completed: false | |
| }; | |
| case 'TOGGLE_TODO': | |
| if (state.id !== action.id) { | |
| return state; | |
| } | |
| return _extends({}, state, { | |
| completed: !state.completed | |
| }); | |
| default: | |
| return state; | |
| } | |
| }; | |
| var todos = function todos(state, action) { | |
| if (state === undefined) state = []; | |
| switch (action.type) { | |
| case 'ADD_TODO': | |
| return [].concat(_toConsumableArray(state), [todo(undefined, action)]); | |
| case 'TOGGLE_TODO': | |
| return state.map(function (t) { | |
| return todo(t, action); | |
| }); | |
| default: | |
| return state; | |
| } | |
| }; | |
| var visibilityFilter = function visibilityFilter(state, action) { | |
| if (state === undefined) state = 'SHOW_ALL'; | |
| switch (action.type) { | |
| case 'SET_VISIBILITY_FILTER': | |
| return action.filter; | |
| default: | |
| return state; | |
| } | |
| }; | |
| var getVisibleTodos = function getVisibleTodos(todos, filter) { | |
| switch (filter) { | |
| case 'SHOW_ALL': | |
| return todos; | |
| case 'SHOW_COMPLETED': | |
| return todos.filter(function (t) { | |
| return t.completed; | |
| }); | |
| case 'SHOW_ACTIVE': | |
| return todos.filter(function (t) { | |
| return !t.completed; | |
| }); | |
| default: | |
| return todos; | |
| } | |
| }; | |
| var _React = React; | |
| var Component = _React.Component; | |
| var _Redux = Redux; | |
| var createStore = _Redux.createStore; | |
| var combineReducers = _Redux.combineReducers; | |
| // Use connect to generate container components | |
| var _ReactRedux = ReactRedux; | |
| var connect = _ReactRedux.connect; | |
| var todoApp = combineReducers({ | |
| todos: todos, | |
| visibilityFilter: visibilityFilter | |
| }); | |
| var nextTodoId = 0; | |
| var addTodo = function addTodo(text) { | |
| return { | |
| type: 'ADD_TODO', | |
| text: text, | |
| id: nextTodoId++ | |
| }; | |
| }; | |
| var setVisibilityFilter = function setVisibilityFilter(filter) { | |
| return { | |
| type: 'SET_VISIBILITY_FILTER', | |
| filter: filter | |
| }; | |
| }; | |
| var toggleTodo = function toggleTodo(id) { | |
| return { | |
| type: 'TOGGLE_TODO', | |
| id: id | |
| }; | |
| }; | |
| // 2nd param received is props. in this case it is | |
| // destructured to just grab the store | |
| var AddTodo = function AddTodo(_ref) { | |
| var dispatch = _ref.dispatch; | |
| var input = undefined; | |
| return React.createElement( | |
| 'div', | |
| null, | |
| React.createElement('input', { ref: function (node) { | |
| input = node; | |
| } }), | |
| React.createElement( | |
| 'button', | |
| { onClick: function () { | |
| dispatch(addTodo(input.value)); | |
| input.value = ''; | |
| } }, | |
| 'Add Todo' | |
| ) | |
| ); | |
| }; | |
| // No need for the store in this container component, | |
| // so don't provide mapStateToProps in order to avoid subscribing, | |
| // then use default behavior of null for dispatch to get dispatch | |
| // passed as a prop to wrapped container | |
| AddTodo = connect()(AddTodo); | |
| var Link = function Link(_ref2) { | |
| var active = _ref2.active; | |
| var onClick = _ref2.onClick; | |
| var children = _ref2.children; | |
| if (active) { | |
| return React.createElement( | |
| 'span', | |
| null, | |
| children | |
| ); | |
| } | |
| return React.createElement( | |
| 'a', | |
| { href: '#', | |
| onClick: function (e) { | |
| e.preventDefault(); | |
| onClick(); | |
| } | |
| }, | |
| children | |
| ); | |
| }; | |
| var mapStateToLinkProps = function mapStateToLinkProps(state, ownProps) { | |
| return { | |
| active: ownProps.filter === state.visibilityFilter | |
| }; | |
| }; | |
| var mapDispatchToLinkProps = function mapDispatchToLinkProps(dispatch, ownProps) { | |
| return { | |
| onClick: function onClick() { | |
| dispatch(setVisibilityFilter(ownProps.filter)); | |
| } | |
| }; | |
| }; | |
| var FilterLink = connect(mapStateToLinkProps, mapDispatchToLinkProps)(Link); | |
| var Footer = function Footer() { | |
| return React.createElement( | |
| 'p', | |
| null, | |
| 'Show: ', | |
| ' ', | |
| React.createElement( | |
| FilterLink, | |
| { | |
| filter: 'SHOW_ALL' | |
| }, | |
| 'All' | |
| ), | |
| ' ', | |
| React.createElement( | |
| FilterLink, | |
| { | |
| filter: 'SHOW_ACTIVE' | |
| }, | |
| 'Active' | |
| ), | |
| ' ', | |
| React.createElement( | |
| FilterLink, | |
| { | |
| filter: 'SHOW_COMPLETED' | |
| }, | |
| 'Completed' | |
| ) | |
| ); | |
| }; | |
| var Todo = function Todo(_ref3) { | |
| var onClick = _ref3.onClick; | |
| var completed = _ref3.completed; | |
| var text = _ref3.text; | |
| return React.createElement( | |
| 'li', | |
| { | |
| onClick: onClick, | |
| style: { | |
| textDecoration: completed ? 'line-through' : 'none' | |
| } | |
| }, | |
| text | |
| ); | |
| }; | |
| var TodoList = function TodoList(_ref4) { | |
| var todos = _ref4.todos; | |
| var onTodoClick = _ref4.onTodoClick; | |
| return React.createElement( | |
| 'ul', | |
| null, | |
| todos.map(function (todo) { | |
| return React.createElement(Todo, _extends({ | |
| key: todo.id | |
| }, todo, { | |
| onClick: function () { | |
| return onTodoClick(todo.id); | |
| } | |
| })); | |
| }) | |
| ); | |
| }; | |
| var mapStateToTodoListProps = function mapStateToTodoListProps(state) { | |
| return { | |
| todos: getVisibleTodos(state.todos, state.visibilityFilter) | |
| }; | |
| }; | |
| var mapDispatchToTodoListProps = function mapDispatchToTodoListProps(dispatch) { | |
| return { | |
| onTodoClick: function onTodoClick(id) { | |
| dispatch(toggleTodo(id)); | |
| } | |
| }; | |
| }; | |
| // Generate container component | |
| var VisibleTodoList = connect(mapStateToTodoListProps, mapDispatchToTodoListProps)(TodoList); | |
| var _ReactRedux2 = ReactRedux; | |
| var Provider = _ReactRedux2.Provider; | |
| var TodoApp = function TodoApp() { | |
| return React.createElement( | |
| 'div', | |
| null, | |
| React.createElement(AddTodo, null), | |
| React.createElement(VisibleTodoList, null), | |
| React.createElement(Footer, null) | |
| ); | |
| }; | |
| ReactDOM.render(React.createElement( | |
| Provider, | |
| { store: createStore(todoApp) }, | |
| React.createElement(TodoApp, null) | |
| ), document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment