-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1536f7
commit ab9039e
Showing
9 changed files
with
399 additions
and
147 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react'; | ||
import { useTodos } from '../../context/context'; | ||
import { SelectedBy } from '../../types/SelectedBy'; | ||
import classNames from 'classnames'; | ||
|
||
const Footer: React.FC = () => { | ||
const { todos, selectedBy, setSelectedBy, clearAllCompletedTodos } = | ||
useTodos(); | ||
const activeTodos = todos.filter(todo => !todo.completed); | ||
|
||
return ( | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{activeTodos.length} items left | ||
</span> | ||
|
||
<nav className="filter" data-cy="Filter"> | ||
{Object.values(SelectedBy).map(value => ( | ||
<a | ||
key={value} | ||
href={`#/${value}`} | ||
className={classNames('filter__link', { | ||
selected: selectedBy === value, | ||
})} | ||
data-cy={`FilterLink${value}`} | ||
onClick={() => setSelectedBy(value)} | ||
> | ||
{value} | ||
</a> | ||
))} | ||
</nav> | ||
|
||
<button | ||
type="button" | ||
className="todoapp__clear-completed" | ||
data-cy="ClearCompletedButton" | ||
disabled={activeTodos.length === todos.length} | ||
onClick={clearAllCompletedTodos} | ||
> | ||
Clear completed | ||
</button> | ||
</footer> | ||
); | ||
}; | ||
|
||
export default Footer; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React, { useState } from 'react'; | ||
import { useTodos } from '../../context/context'; | ||
import classNames from 'classnames'; | ||
|
||
const Header: React.FC = () => { | ||
const { todos, addTodo, mainRef, toggleAllTodos } = useTodos(); | ||
const [title, setTitle] = useState(''); | ||
const addNewTodo = (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
|
||
if (title.trim()) { | ||
addTodo(title.trim()); | ||
setTitle(''); | ||
} | ||
}; | ||
|
||
return ( | ||
<header className="todoapp__header"> | ||
{todos.length > 0 && ( | ||
<button | ||
type="button" | ||
className={classNames('todoapp__toggle-all', { | ||
active: todos.every(todo => todo.completed), | ||
})} | ||
data-cy="ToggleAllButton" | ||
onClick={toggleAllTodos} | ||
/> | ||
)} | ||
|
||
<form onSubmit={addNewTodo}> | ||
<input | ||
data-cy="NewTodoField" | ||
type="text" | ||
className="todoapp__new-todo" | ||
placeholder="What needs to be done?" | ||
value={title} | ||
onChange={e => setTitle(e.target.value)} | ||
ref={mainRef} | ||
/> | ||
</form> | ||
</header> | ||
); | ||
}; | ||
|
||
export default Header; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* eslint-disable jsx-a11y/label-has-associated-control */ | ||
import React, { useRef, useState } from 'react'; | ||
import { Todo } from '../../types/Todo'; | ||
import classNames from 'classnames'; | ||
import { useTodos } from '../../context/context'; | ||
|
||
const TodoItem: React.FC<{ todo: Todo }> = ({ todo }) => { | ||
const { deleteTodo, tempTodo, toggleTodo, editTodo } = useTodos(); | ||
const [isOpenEditForm, setIsOpenEditForm] = useState(false); | ||
const editInputRef = useRef<HTMLInputElement>(null); | ||
|
||
const handleDobleClick = () => { | ||
setIsOpenEditForm(true); | ||
|
||
setTimeout(() => { | ||
editInputRef.current?.focus(); | ||
}, 0); | ||
}; | ||
|
||
const editTitle = (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
|
||
const form = new FormData(e.currentTarget); | ||
const newTitle = form.get('TodoTitleField') as string; | ||
|
||
if (!newTitle.trim()) { | ||
deleteTodo(todo.id); | ||
} | ||
|
||
if (newTitle !== todo.title) { | ||
editTodo(newTitle.trim(), todo.id); | ||
} | ||
|
||
setTimeout(() => { | ||
setIsOpenEditForm(false); | ||
}, 100); | ||
}; | ||
|
||
const handleTitleBlur = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const newTitle = e.target.value.trim(); | ||
|
||
if (!newTitle) { | ||
deleteTodo(todo.id); | ||
} | ||
|
||
if (newTitle !== todo.title) { | ||
editTodo(newTitle, todo.id); | ||
} | ||
|
||
setIsOpenEditForm(false); | ||
}; | ||
|
||
const handleKeyUp = (e: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (e.key === 'Escape') { | ||
setIsOpenEditForm(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<div | ||
data-cy="Todo" | ||
className={classNames('todo', { | ||
completed: todo.completed, | ||
})} | ||
> | ||
<label className="todo__status-label"> | ||
<input | ||
data-cy="TodoStatus" | ||
type="checkbox" | ||
className="todo__status" | ||
defaultChecked={todo.completed} | ||
onChange={() => toggleTodo(todo.id)} | ||
/> | ||
</label> | ||
|
||
{!isOpenEditForm ? ( | ||
tempTodo?.id === todo.id ? ( | ||
<span data-cy="TodoTitle" className="todo__title"> | ||
Todo is being saved now | ||
</span> | ||
) : ( | ||
<span | ||
data-cy="TodoTitle" | ||
className="todo__title" | ||
onDoubleClick={handleDobleClick} | ||
> | ||
{todo.title} | ||
</span> | ||
) | ||
) : ( | ||
<form onSubmit={editTitle}> | ||
<input | ||
data-cy="TodoTitleField" | ||
name="TodoTitleField" | ||
type="text" | ||
className="todo__title-field" | ||
placeholder="Empty todo will be deleted" | ||
defaultValue={todo.title} | ||
ref={editInputRef} | ||
onBlur={handleTitleBlur} | ||
onKeyUp={handleKeyUp} | ||
/> | ||
</form> | ||
)} | ||
|
||
{!isOpenEditForm && ( | ||
<button | ||
type="button" | ||
className="todo__remove" | ||
data-cy="TodoDelete" | ||
onClick={() => deleteTodo(todo.id)} | ||
> | ||
× | ||
</button> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default TodoItem; |
Oops, something went wrong.