From df7bff4a737638ce9b06d613a79ec7588304559c Mon Sep 17 00:00:00 2001 From: pahan Date: Sun, 17 Apr 2022 21:28:08 +0300 Subject: [PATCH] lab8 finally --- lab8/src/App.css | 23 ++++++++++++++- lab8/src/ToDoList.jsx | 69 ++++++++++++++++++++++++++++++------------- 2 files changed, 70 insertions(+), 22 deletions(-) diff --git a/lab8/src/App.css b/lab8/src/App.css index 9779d87..d964771 100644 --- a/lab8/src/App.css +++ b/lab8/src/App.css @@ -66,12 +66,17 @@ margin-top: 16px; } +.item > input { + position: relative; + left: 0; +} + .item-edit > * { color: black; text-shadow: none; } -.checked, .unchecked { +.checked, .unchecked{ position: relative; left: 16px; } @@ -86,6 +91,22 @@ margin: 0; } +.editted { + width: 80%; + display: flex; + flex-direction: row; + align-items: baseline; +} + +.editted > * { + color: black; + text-shadow: none; +} + +.editted > input { + width: 70%; +} + .item-edit { margin-top: 8px; } diff --git a/lab8/src/ToDoList.jsx b/lab8/src/ToDoList.jsx index 40f9dbf..2ba00d8 100644 --- a/lab8/src/ToDoList.jsx +++ b/lab8/src/ToDoList.jsx @@ -2,51 +2,78 @@ import React, { useReducer, useState } from "react"; export function ToDoList() { const [items, editItems] = useReducer((curItems, action) => { - console.log(action); if (action.type === 'remove') return curItems.filter((item) => !action.payload.includes(item)); if (action.type === 'add') - return curItems.concat(action.payload); + return curItems.concat(action.payload.reduce((result, item) => { + let lastId = result.length === 0 ? + curItems[curItems.length - 1].id : result[result.length - 1].id; + lastId++; + result.push({ text: item, id: lastId }); + return result; + }, [])); + if (action.type === 'edit') { + let index = + curItems.findIndex((item) => action.payload[0].id === item.id); + curItems[index].text = action.payload[0].text; + return curItems; + } return curItems; - }, ['make a list']); + }, [{ text: 'make a list', id: 0 }]); return (

Yesterday you said tomorrow. So just DO it!!!! MAKE. YOUR DREAMS. COME TRUE!!!

- +
); } -export function ItemList({ itemList, editMode }) { +export function ItemList({ itemList, parentListEditor }) { return (
- {itemList.map((item) => )} + {itemList.map((item) => + + )}
); } -export function Item({ textContent, mode }) { +export function Item({ itemId, textContent, parentListEditor }) { + const [id, setId] = useState(itemId); + const [edit, setEdit] = useState(false); const [text, setText] = useState(textContent); const [state, setState] = useState(false); - console.log(`${textContent} created`); + const editParent = parentListEditor; return ( -
- setState((prevState) => !prevState) - }> - - {state ? ( - - {text} - ) : ( - - {text} - - )} +
+ {!edit ? ( + setState((prevState) => !prevState)}> + ) : (<>)} + {!edit ? + state ? ( + + {text} + ) : ( + setEdit(true)}> + {text} + + ) : ( + + setText(e.target.value)}> + + + + )}
); }