-
728x90
๐ท CloneTodo โ - Todomate Clone Project | Team CloneMate
CloneTodo : ๋ง์ ์ฌ๋๋ค์ด ์ฌ๋ํ๋ ํฌ๋๋ฉ์ดํธ๋ฅผ ํด๋ก ํ์ฌ ์น ์๋น์ค๋ฅผ ๋ฐฐํฌํด๋ณด๋ ํ๋ก์ ํธ
์ค๋์ ์์ ๋ถ๋ถ์ ๋ง๋ค ์ฐจ๋ก! ์ค์ ๋ก ์ฑ ์์ ๋์์ ๋ฐ์ ์ ์๋ ๋ถ๋ถ์ ์๋ฒ ์ฐ๊ฒฐ์ด๋ผ๊ณ ์๊ฐํ๋ค.
์ฑ ์์๋ ์์ฒญ ๋จ์ํ ์ฑ์ ๋ง๋ค๊ธฐ ๋๋ฌธ์ด๋ค ใ ใ
์ด์จ๋ ํฌ๋ ๋ฑ๋ก, ์์ , ์ญ์ ๊ฐ ๊ธฐ๋ฅ์ ๋๋ถ๋ถ์ด๋๊น ...
์์ ์๋ ๋ ๊ฐ์ง๊ฐ ์๋ค.
์ฒซ๋ฒ์งธ๋ ํฌ๋ ์ํ ๋ณํ๋ค.
ํฌ๋์ ์ฒดํฌ๋ฅผ ํ๋ฉด ํ ์ผ์ ์๋ฃํ ์ํ์ด๊ธฐ ๋๋ฌธ์ items state์ done ๊ฐ์ด true๊ฐ ๋๊ฒ ํ๋ ๊ฒ๊ณผ
์ด๋ฏธ ๋ฑ๋กํ ํฌ๋๋ฅผ ์์ ํ๋ ๊ฒ์ด๋ค.
๊ทธ๋ฆฌ๊ณ ๋ ํ ๊ฐ์ง๋ ์ด๋ฏธ ๋ฑ๋กํ ํฌ๋๋ฅผ ํด๋ฆญํ๋ฉด ๋ด์ฉ์ ๋ฐ๊ฟ ์ ์๊ฒ ํ๋ ๋จ๊ณ๋ค.
์ด ๋จ๊ณ๊ฐ ์ข ๋ณต์กํ๋ค.
์ฐ์ ๋ฑ๋กํ ๋ชฉํ์ readOnly ๋ผ๋ props๋ก ์ํ๋ฅผ ํํํด์ค์ผ ํ๋๋ฐ, ์ฑ ์ ์ฝ๋๋ฅผ ํจ์ํ์ผ๋ก ์ด๋ป๊ฒ ๋ณํํด์ผ ํ๋์ง ๊ฐ์ด ์์จ๋ค.
let goalItems = props.goal; let addGoal = props.addGoal; let deleteGoal = props.deleteGoal; function deleteEventHandler(e) { console.log( 'id : ', e.target.id) deleteGoal(e.target.id, goalItems) }
์์ ๊ฐ์ด ๋ง๋ค์ด๋๋ props ๋ค์ธ๋ฐ..
์ฑ ์์๋
constructor(props) { super(props) this.state = { item : props.item, readOnly : true }; this.delete = props.delete; }
๊ทธ๋ผ readOnly ๋ณ์๋ฅผ ๋ฐ๋ก ์ค์ผ ํ๋๊ฑด์ง.. goalItems๋ก ๋ง๋ props ๋ฅผ ๊ฐ์ฒด๋ก ๋ง๋ค์ด์ผ ํ๋๊ฑด์ง...
์ ์ง ๋ณ๋๋ก ๋ณ์๋ก ๋ง๋ค์ด ์ค์ผ๊ฒ ์ง..?
์๋ state์ ์ถ๊ฐํด๊ฐ์ง๊ณ props๋ก ๋ง๋ค์ด์ค์ผํ๋๊ฑฐ๊ฒ ์ง..?
์ด ์ ๋ ๊ณต๋ถํ์ผ๋ฉด ์ฐฐ๋ก๊ฐ์ด ์์๋ค์ด์ผํ๋๋ฐ...
์ ๋ฒ์ ๋ง๋ goal state ์๋์ readOnly state๋ฅผ ๋ง๋ค์๋ค.
let [readOnly, readOnlyChange] = useState(true)
๊ทธ๋ฆฌ๊ณ ๋ค์ Goals ์ปดํฌ๋ํธ๋ก ๋๊ฒจ์ฃผ์๋ค.
<Goals goal={goal.items} addGoal={addGoal} deleteGoal={deleteGoal} readOnly={readOnly} />
๊ทธ๋ฆฌ๊ณ offReadOnlyMode ๋ผ๋ ํจ์๋ฅผ ๋ง๋ค์ด ์ค๋ค.
// readOnly ์ํ ๋ณ๊ฒฝ ํจ์ function offReadOnlyMode(){ console.log("Event!", readOnly) readOnlyChange(readOnly = false) }
์ด๊ฒ๋ค์ ๋ props๋ก ๋ค ๋๊ฒจ์ค๋ค.
<Goals goal={goal.items} addGoal={addGoal} deleteGoal={deleteGoal} readOnly={readOnly} readOnlyChange={offReadOnlyMode} />
๊ทธ๋ฆฌ๊ณ Goals.js์์ props๋ก ๋ฐ์์์ ๋ณ์์ ๋ด๊ณ ...
let readOnly = props.readOnly; let readOnlyChange = props.readOnlyChange;
inputBase์ readOnly์ onClick ์ด๋ฒคํธ๋ก ํจ์๋ฅผ ๋๊ฒจ์ค๋ค.
<InputBase inputProps={{ "aria-label": "naked", readOnly: readOnly, }} onClick={readOnlyChange} type="text" id={item.id} name={item.id} value={item.title} multiline={true} fullWidth={true} />
์ด๋ ๊ฒ ์์ฑํด์ฃผ๋ ๋ชฉํ๋ฅผ ํด๋ฆญํ์ ๋, ์ฝ์์ readOnly๊ฐ false๋ก ๋ณํ๋ค!
์ด์ ๋ค์ enter๋ฅผ ๋๋ฅด๋ฉด readOnly๋ฅผ true๋ก ๋ฐ๊ฟ์ฃผ๋ ํจ์๋ฅผ ์์ฑํ๋ค.
์ด์ ์ ์์ฑํ enterKeyEventHandler์ ์ ์ฌํ๋ค.
//readOnly ์ํ true๋ก ๋ณ๊ฒฝ ํจ์ function enterKeyEventHandler(e){ if(e.key === 'Enter'){ readOnlyChange(readOnly = true) } }
๊ทธ๋ผ ์ด ํจ์๋ฅผ ๋ค์ props๋ก ๋ณด๋ด์ค๋ค.
<Goals goal={goal.items} addGoal={addGoal} deleteGoal={deleteGoal} readOnly={readOnly} readOnlyChange={offReadOnlyMode} enterKeyEventHandler={enterKeyEventHandler} />
๋ค์ Goals์์ enterKeyEventHandler๋ฅผ ๋ฐ์์ฃผ๊ณ (์ด์ ์ enterKeyEventHandler ํจ์๋ GoalForm์ ์์!)
onKeyPress๋ก ํจ์๋ฅผ ์ฌ์ฉํด์ค๋ค.
<InputBase inputProps={{ "aria-label": "naked", readOnly: readOnly, }} onClick={readOnlyChange} onKeyPress={enterKeyEventHandler} type="text" id={item.id} name={item.id} value={item.title} multiline={true} fullWidth={true} />
์ด์ readOnly๋ฅผ ๋ง๋ค์ด์คฌ์ผ๋ item์ ์์ ํด๋ณด์.
์ฐ์ ์์ ํจ์๋ฅผ ์์ฑํ๋ค. state๊ฐ ์๋ ๊ณณ์์ ์์ฑํ๋ค.
//items state ์์ ํจ์ function editEventHandler(e){ const thisItems = goal.items; // goal State ์๋ณธ ์นดํผ thisItems.title = e.target.value; goalChange({items: thisItems}); //goalChange๋ฅผ ์ด์ฉํด state ๋ณ๊ฒฝ }
์ด edit ํจ์๋ฅผ props๋ก ์ ๋ฌํด InputBase์ onChange๋ก ์ฐ๊ฒฐํ๋ค.
<Goals goal={goal.items} addGoal={addGoal} deleteGoal={deleteGoal} readOnly={readOnly} readOnlyChange={offReadOnlyMode} enterKeyEventHandler={enterKeyEventHandler} editEventHandler={editEventHandler} />
let goalItems = props.goal; let addGoal = props.addGoal; let deleteGoal = props.deleteGoal; let readOnly = props.readOnly; let readOnlyChange = props.readOnlyChange; let enterKeyEventHandler = props.enterKeyEventHandler; let editEventHandler = props.editEventHandler;
<InputBase inputProps={{ "aria-label": "naked", readOnly: readOnly, }} onClick={readOnlyChange} onKeyPress={enterKeyEventHandler} onChange={editEventHandler} type="text" id={item.id} name={item.id} value={item.title} multiline={true} fullWidth={true} />
์์ด๊ณ ใ ใ ์๋์ด ์๋๋ค....ใ ใ ใ ใ
์ฐ์ ์ ๋ง๋ฌด๋ฆฌ๋ก ์ฒดํฌ๋ฐ์ค๋ฅผ ํด๋ฆญํ๋ฉด done state๊ฐ true๋ก, ํด๋ฆญ๋์์ ๋, ํด๋ฆญ์ ํ๋ฉด false๋ก ๋ฐ๋๋๋ก ํด๋ณธ๋ค.
//์ฒดํฌ๋ฐ์ค ํด๋ฆญ ํจ์ function checkboxEventHandler(e){ const thisItems = goal.items; thisItems.done = !thisItems.done; goalChange({items: thisItems}) }
์ด ํจ์๋ฅผ props๋ก ๋ณด๋ด checkbox ์ ์ฐ๊ฒฐํ๋ค.
<Goals goal={goal.items} addGoal={addGoal} deleteGoal={deleteGoal} readOnly={readOnly} readOnlyChange={offReadOnlyMode} enterKeyEventHandler={enterKeyEventHandler} editEventHandler={editEventHandler} checkboxEventHandler={checkboxEventHandler} />
import React from "react"; import { Checkbox, IconButton, InputBase, ListItem, ListItemText } from "@mui/material"; import GoalForm from "./GoalForm"; import { ListItemSecondaryAction } from "@material-ui/core"; export default function Goals(props) { console.log(props); let goalItems = props.goal; let addGoal = props.addGoal; let deleteGoal = props.deleteGoal; let readOnly = props.readOnly; let readOnlyChange = props.readOnlyChange; let enterKeyEventHandler = props.enterKeyEventHandler; let editEventHandler = props.editEventHandler; let checkboxEventHandler = props.checkboxEventHandler; //๋ชฉํ ์ญ์ ์ด๋ฒคํธํธ๋ค๋ฌ ํจ์ function deleteEventHandler(e) { console.log( 'id : ', e.target.id) deleteGoal(e.target.id, goalItems) } return ( <> <GoalForm addGoal={addGoal} /> { goalItems.map((item, idx) => { return ( <ListItem className="goals-wrap" key={item.id} > { console.log('item: ', item) } <Checkbox checked={item.done} onChange={checkboxEventHandler} /> <ListItemText> <InputBase inputProps={{ "aria-label": "naked", readOnly: readOnly, }} onClick={readOnlyChange} onKeyPress={enterKeyEventHandler} onChange={editEventHandler} type="text" id={item.id} name={item.id} value={item.title} multiline={true} fullWidth={true} /> </ListItemText> <ListItemSecondaryAction> <IconButton aria-label="Delete Todo" id={item.id} onClick={deleteEventHandler} sx={{ fontSize: '14px' }}> ์ญ์ </IconButton> </ListItemSecondaryAction> </ListItem> ); }) } </> ); }
์๋๋ค...
์ ์ง.. ์ญ์ ๊ตฌํํ์ ๋์ ๋ฌธ์ ์ฒ๋ผ ํด๋์คํ์์๋ this๋ฅผ ์จ์ ์ด๋ฒคํธ๋ฅผ ์ก์๋ด๋๋ฐ this๊ฐ ๋น ์ ธ๋ฒ๋ฆฌ๋ฉด์ ์ด๋ค๊ฒ ํด๋ฆญ๋์๋์ง ์ ๋๋ก ์์กํ์ ๊ทธ๋ฐ ๊ฒ ๊ฐ๋ค.
(e)๊ฐ ์๋๋ฐ๋...
๋ ๋ค ๋น์ทํ ๋ฌธ์ ์ธ ๊ฒ ๊ฐ์ผ๋ ๋์ค์ ์์ ํด์ผ๊ฒ ๋ค ใ ใ ใ ใ ใ
์ด๊ฑฐ ํด๊ฒฐํ๋ฉด ์ ๋ ฅ ๋ถ๋ถ๊ณผ ์ถ๋ ฅ ๋ถ๋ถ ํ๋ฉด์ ๋ฐ๋ก ๋ถ๋ฆฌ์์ผ์ผ๊ฒ ์!!
728x90'WEB Dev > ToyProject' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ToyProject-Todomate] ํฌ๋๋ฉ์ดํธ ํด๋ก ํ๋ก์ ํธ 25 (0) 2022.01.13 [ToyProject-Todomate] ํฌ๋๋ฉ์ดํธ ํด๋ก ํ๋ก์ ํธ 24 (0) 2022.01.11 [ToyProject-Todomate] ํฌ๋๋ฉ์ดํธ ํด๋ก ํ๋ก์ ํธ 22 (0) 2022.01.05 [ToyProject-Todomate] ํฌ๋๋ฉ์ดํธ ํด๋ก ํ๋ก์ ํธ 21 (0) 2022.01.04 [ToyProject-Todomate] ํฌ๋๋ฉ์ดํธ ํด๋ก ํ๋ก์ ํธ 20 (0) 2022.01.02