-
Notifications
You must be signed in to change notification settings - Fork 0
Implementing to do list #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import React, { useState } from 'react'; | ||
|
|
||
| interface Props { | ||
| addTodo: AddTodo; | ||
| } | ||
|
|
||
| export const AddTodoForm: React.FC<Props> = (param) => { | ||
| //const { addTodo } = param; | ||
| const addTodo = param.addTodo; | ||
| const [text, setText] = useState('') ; | ||
|
|
||
| return ( | ||
| <form> | ||
| <input | ||
| value={text} | ||
| type="text" | ||
| onChange={event => { | ||
| setText(event.target.value); | ||
| }} | ||
| /> | ||
| <button | ||
| type="submit" | ||
| onClick={event => { | ||
| event.preventDefault(); | ||
| addTodo(text); | ||
| setText(''); | ||
| }} | ||
| > | ||
| Add Todo | ||
| </button> | ||
| </form> | ||
| ); | ||
| }; | ||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,25 +1,52 @@ | ||||||||||||||||||||||||
| import React from 'react'; | ||||||||||||||||||||||||
| import logo from './logo.svg'; | ||||||||||||||||||||||||
| import './App.css'; | ||||||||||||||||||||||||
| import React, { useState } from 'react' | ||||||||||||||||||||||||
| import { AddTodoForm } from './AddTodoForm'; | ||||||||||||||||||||||||
| import { TodoList } from './TodoList'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const initialTodos: Todo[] = [ | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| text: 'Walk the dog', | ||||||||||||||||||||||||
| complete: false, | ||||||||||||||||||||||||
| id: 'test1' + new Date().getTime(), | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| text: 'Write app', | ||||||||||||||||||||||||
| complete: true, | ||||||||||||||||||||||||
| id: 'test2' + new Date().getTime(), | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| function App() { | ||||||||||||||||||||||||
| const [todos, setTodos] = useState(initialTodos); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const toggleTodo: ToggleTodo = (selectedTodo: Todo) => { | ||||||||||||||||||||||||
| const newTodos = todos.map(todo => { | ||||||||||||||||||||||||
| if (todo === selectedTodo) { | ||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||
| ...todo, | ||||||||||||||||||||||||
| complete: !todo.complete, | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| return todo; | ||||||||||||||||||||||||
|
Comment on lines
+23
to
+29
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, I would recommend the uses of a ternary
Suggested change
|
||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| setTodos(newTodos); | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const addTodo: AddTodo = (text: string) => { | ||||||||||||||||||||||||
| const id = text + new Date().getTime() | ||||||||||||||||||||||||
| const newTodo = { text, complete: false, id: id }; | ||||||||||||||||||||||||
| setTodos([...todos, newTodo]); | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const deleteTodo: DeleteTodo = (todoId: string) => { | ||||||||||||||||||||||||
| const newTodo = todos.filter(selectedItem => selectedItem.id !== todoId) | ||||||||||||||||||||||||
| setTodos(newTodo); | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| function App() { | ||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||
| <div className="App"> | ||||||||||||||||||||||||
| <header className="App-header"> | ||||||||||||||||||||||||
| <img src={logo} className="App-logo" alt="logo" /> | ||||||||||||||||||||||||
| <p> | ||||||||||||||||||||||||
| Edit <code>src/App.tsx</code> and save to reload. | ||||||||||||||||||||||||
| </p> | ||||||||||||||||||||||||
| <a | ||||||||||||||||||||||||
| className="App-link" | ||||||||||||||||||||||||
| href="https://reactjs.org" | ||||||||||||||||||||||||
| target="_blank" | ||||||||||||||||||||||||
| rel="noopener noreferrer" | ||||||||||||||||||||||||
| > | ||||||||||||||||||||||||
| Learn React | ||||||||||||||||||||||||
| </a> | ||||||||||||||||||||||||
| </header> | ||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||
| <> | ||||||||||||||||||||||||
| <TodoList todos={todos} toggleTodo={toggleTodo} deleteTodo={deleteTodo} /> | ||||||||||||||||||||||||
| <AddTodoForm addTodo={addTodo} /> | ||||||||||||||||||||||||
| </> | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import React from 'react'; | ||
| import { TodoListItem } from './TodoListItem'; | ||
|
|
||
| interface Props { | ||
| todos: Todo[]; | ||
| toggleTodo: ToggleTodo; | ||
| deleteTodo: DeleteTodo; | ||
| } | ||
|
|
||
| export const TodoList: React.FC<Props> = ( param ) => { | ||
| const todos = param.todos ; | ||
| const toggleTodo = param.toggleTodo; | ||
| const deleteTodo = param.deleteTodo; | ||
|
|
||
| return ( | ||
| <ul> | ||
| {todos.map(todo => ( | ||
| <TodoListItem key={todo.text} todo={todo} toggleTodo={toggleTodo} deleteTodo={deleteTodo} /> | ||
| ))} | ||
| </ul> | ||
| ); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import React from "react"; | ||
|
|
||
| interface Props { | ||
| todo: Todo; | ||
| toggleTodo: ToggleTodo; | ||
| deleteTodo: DeleteTodo; | ||
| } | ||
|
|
||
| export const TodoListItem: React.FC<Props> = (param) => { | ||
| const todo = param.todo ; | ||
| const toggleTodo = param.toggleTodo; | ||
| const deleteTodo = param.deleteTodo; | ||
| const labelStyle = {textDecoration: todo.complete ? "line-through" : undefined}; | ||
|
|
||
| return ( | ||
| <li> | ||
| <label style={labelStyle} > | ||
| <input | ||
| type="checkbox" | ||
| checked={todo.complete} | ||
| onClick={() => { | ||
| toggleTodo(todo); | ||
| }} | ||
| readOnly | ||
| /> | ||
| {' '} | ||
| {todo.text} | ||
| {' '} | ||
| {' '} | ||
| <button | ||
| type="submit" | ||
| onClick={event => { | ||
| event.preventDefault(); | ||
| deleteTodo(todo.id); | ||
| }} | ||
| > | ||
| X | ||
| </button> | ||
| </label> | ||
| </li> | ||
| ); | ||
| }; |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,10 @@ | ||
| import React from 'react'; | ||
| import ReactDOM from 'react-dom'; | ||
| import './index.css'; | ||
| import App from './App'; | ||
| import reportWebVitals from './reportWebVitals'; | ||
|
|
||
| ReactDOM.render( | ||
| <React.StrictMode> | ||
| <App /> | ||
| </React.StrictMode>, | ||
| document.getElementById('root') | ||
| ); | ||
|
|
||
| // If you want to start measuring performance in your app, pass a function | ||
| // to log results (for example: reportWebVitals(console.log)) | ||
| // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals | ||
| reportWebVitals(); |
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| interface Todo{ | ||
| text: string; | ||
| complete: boolean; | ||
| id: string; | ||
| } | ||
|
|
||
| type ToggleTodo = (selectedTodo: Todo) => void; | ||
|
|
||
| type AddTodo = (text: string) => void; | ||
| type DeleteTodo = (todoId: string) => void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't leave commented code, it's dirty :P
If you leave it for documentation purposes, I'd advise you to put an actual comment with it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I shall make a documentation of it. I wanted to keep track of the possible destructuration here.