Created
July 9, 2018 10:11
-
-
Save balazimichal/1a1a9749d58dd3f9cd9b9b35a14bae5c to your computer and use it in GitHub Desktop.
React simple one page app with search.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>React Todo List</title> | |
<script src="https://unpkg.com/react@16/umd/react.development.js"></script> | |
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> | |
<script src="https://unpkg.com/[email protected]/babel.min.js"></script> | |
</head> | |
<body> | |
<div id="root"></div> | |
<script type="text/babel"> | |
class App extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
todos: [ | |
{ id: 1, title: 'Learn React', vote: 0 }, | |
{ id: 2, title: 'Learn Vue', vote: 0 }, | |
{ id: 3, title: 'Learn PWA', vote: 0 }, | |
{ id: 4, title: 'Learn Node', vote: 0 }, | |
{ id: 5, title: 'Learn Firebase', vote: 0 } | |
], | |
value: '', | |
query: '' | |
} | |
this.removeTodo = this.removeTodo.bind(this) | |
this.voteUp = this.voteUp.bind(this) | |
this.formOnSubmit = this.formOnSubmit.bind(this) | |
this.formOnChange = this.formOnChange.bind(this) | |
this.searchTodos = this.searchTodos.bind(this) | |
} | |
removeTodo(td) { | |
this.setState((previousState) => ({ | |
todos: previousState.todos.filter((todo)=> { | |
if(td.id !== todo.id) { | |
return todo | |
} | |
}) | |
})) | |
} | |
voteUp(td) { | |
this.setState((prevState) => ({ | |
todos: prevState.todos.map((todo) => { | |
if(td.id === todo.id) { | |
todo.vote = todo.vote + 1 | |
return todo | |
} else { | |
return todo | |
} | |
}) | |
})) | |
} | |
formOnSubmit(e) { | |
e.preventDefault() | |
this.setState({ | |
todos: [...this.state.todos, {id: this.randID(), title: this.state.value, vote: 0}] | |
}) | |
this.setState({ | |
value: '' | |
}) | |
} | |
formOnChange(value) { | |
this.setState({ | |
value: value | |
}) | |
} | |
randID() { | |
return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(2, 10); | |
} | |
searchTodos(query) { | |
this.setState({ | |
query: query | |
}) | |
} | |
editTodo = (td) => { | |
console.log(td) | |
this.setState((prevState) => ({ | |
todos: prevState.todos.map((todo) => { | |
if (td.id === todo.id) { | |
todo.vote = todo.vote + 1 | |
return todo | |
} else { | |
return todo | |
} | |
}) | |
})) | |
} | |
render() { | |
const showingTodos = this.state.query === '' | |
? this.state.todos | |
: this.state.todos.filter((td) => ( | |
td.title.toLowerCase().includes(this.state.query.toLowerCase()) | |
)) | |
return ( | |
<div> | |
<h1>Todo</h1> | |
Search: <input type='text' value={this.state.query} onChange={(event) =>(this.searchTodos(event.target.value))} /> | |
<form onSubmit={this.formOnSubmit}><input onChange={(e) => (this.formOnChange(e.target.value))} type='text' value={this.state.value} /><button>Add TODO</button></form> | |
<ol> | |
{showingTodos.sort((a, b) => a.vote < b.vote).map((todo) => ( | |
<li key={todo.id}>{todo.title} (votes: {todo.vote}) <button onClick={(e) => (this.voteUp(todo))}>+</button> <button onClick={(e) => (this.removeTodo(todo))}>Remove</button></li> | |
))} | |
</ol> | |
</div> | |
); | |
} | |
} | |
ReactDOM.render( | |
<App />, | |
document.getElementById('root') | |
); | |
</script> | |
<!-- | |
Note: this page is a great way to try React but it's not suitable for production. | |
It slowly compiles JSX with Babel in the browser and uses a large development build of React. | |
To set up a production-ready React build environment, follow these instructions: | |
* https://reactjs.org/docs/add-react-to-a-new-app.html | |
* https://reactjs.org/docs/add-react-to-an-existing-app.html | |
You can also use React without JSX, in which case you can remove Babel: | |
* https://reactjs.org/docs/react-without-jsx.html | |
* https://reactjs.org/docs/cdn-links.html | |
--> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment