Compare commits

..

3 Commits

2 changed files with 82 additions and 57 deletions
+66 -54
View File
@@ -1,67 +1,79 @@
import React, { useState, useReducer, useRef, memo } from 'react' import { useState, createContext, useContext } from 'react'
import styles from './App.module.scss'
function tasksReducer(state, action) { const UserContext = createContext('')
switch(action.type) {
case 'ADD_TASK': function UserProvider({ children }) {
return [...state, action.payload] const [name,setName] = useState('');
case 'TOGGLE_TASK':
return state.map(task => task.id === action.payload ? {...task, complite: !task.complite} : task) const toggleName = (newName) => {
case 'REMOVE_TASK': setName(newName);
return state.filter(task => task.id !== action.payload)
default:
return state
} }
return (
<UserContext.Provider value={{name, toggleName}}>
{children}
</UserContext.Provider>
)
} }
const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) { function Form() {
console.log("Rendred") const {name, toggleName} = useContext(UserContext)
const [newName,setNewName] = useState('');
const handleSubmit = (event) => {
event.preventDefault();
toggleName(event.target[0].value)
}
const handlerChange = (event) => {
setNewName(event.target.value);
}
return ( return (
<ul> <form onSubmit={handleSubmit}>
{tasks.map(({id, text, complite}) => ( <input type='text' value={newName} onChange={handlerChange} />
<li key={id} <button type='submit'>Войти</button>
style={{textDecoration: complite ? 'line-through' : 'none' }}> </form>
<input type="checkbox" checked={complite} onChange={()=>onToggle(id)} />
{text}
<button onClick={()=> onRemove(id)} style={{marginLeft: 10}}>
Удалить
</button>
</li>
))}
</ul>
) )
}) }
function ExitButton() {
const {name, toggleName} = useContext(UserContext)
return (
<button onClick={() => toggleName('')}>Выход</button>
)
}
function Toolbar() {
const {name, toggleName} = useContext(UserContext)
return(
<div className={styles.container}>
<h3>Панель пользователя</h3>
{name && `Привет ${name}`}
{name ? <ExitButton /> : <Form /> }
</div>
)
}
function Page() {
return (
<div>
<h1 style={{textAlign: 'center'}}>Главная страница</h1>
<Toolbar />
</div>
)
}
function App() { function App() {
const [tasks, dispatch] = useReducer(tasksReducer,[])
const [input,setInput] = useState('')
const [error,setError] = useState('')
const inputRef = useRef(null)
const addTask = () => {
if (input.trim() === '') {
setError('Пустой текст задачи')
return
}
setError('')
dispatch({
type: 'ADD_TASK',
payload: {id: Date.now(), text: input.trim(), complite: false }
})
setInput('')
inputRef.current.focus()
}
return ( return (
<div style={{padding: 20}}> <>
<h1>Список дел</h1> {/* Добавьте тестовый контент для проверки */}
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} /> <h1>Hello, React!</h1>
<button onClick={addTask}>Добавить</button> <UserProvider>
{error && <p style={{color: 'red'}}>{error}</p>} <Page />
</UserProvider>
<TaskList tasks={tasks} </>
onToggle={id => dispatch({type: 'TOGGLE_TASK', payload: id})}
onRemove={id => dispatch({type: 'REMOVE_TASK', payload: id})} />
</div>
) )
} }
+13
View File
@@ -0,0 +1,13 @@
.container {
height: 500px;
border: 1px solid black;
border-radius: 10px;
background-color: lightblue;
}
.item {
font-size: 16pt;
border: 1px dotted gray;
/*margin: 5px 10px;
padding: 10px;*/
border-radius: 5px;
}