[ToyProject-Todomate] ํˆฌ๋‘๋ฉ”์ดํŠธ ํด๋ก  ํ”„๋กœ์ ํŠธ 28

2022. 1. 16. 01:51ใ†WEB Dev/ToyProject

728x90


๐Ÿ”ท CloneTodo โ˜‘ - Todomate Clone Project  |  Team CloneMate

 

CloneTodo : ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์‚ฌ๋ž‘ํ•˜๋Š” ํˆฌ๋‘๋ฉ”์ดํŠธ๋ฅผ ํด๋ก ํ•˜์—ฌ ์›น ์„œ๋น„์Šค๋ฅผ ๋ฐฐํฌํ•ด๋ณด๋Š” ํ”„๋กœ์ ํŠธ

 

 


 

์ด์ œ ํ•ด๋ณด์•„์•ผ ํ•  ๊ฒƒ์€ ์ด๋Ÿฐ ๊ฒƒ์ด๋‹ค.

 

 

์กฐ๊ฑด๋ฌธ์œผ๋กœ ์–ด๋–ค ์ƒํƒœ๊ฐ€ add ์ด๋ฉด, <GoalForm /> ์ปดํฌ๋„ŒํŠธ๊ฐ€ Goal ์ปดํฌ๋„ŒํŠธ ์œ„ ์ž๋ฆฌ์— ์žˆ๊ณ ,

์ƒํƒœ๊ฐ€ edit์œผ๋กœ ๋ฐ”๋€Œ๋ฉด <GoalEditForm /> ์ด Goal ์ปดํฌ๋„ŒํŠธ ์œ„์— ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

 

 

 

 

์™œ ์ด๋ ‡๊ฒŒ ์ƒ๊ฐํ–ˆ๋ƒ๋ฉด document.querySelector๋กœ ๋‚ด๋ถ€ ๋‚ด์šฉ์„ ๋ฐ”๊พผ๋‹ค๊ณ  ํ•ด๋„, ํ™•์ธ ๋ฒ„ํŠผ์ด ์ด๋ฏธ onClick={onButtonClick}์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ˆ˜์ •ํ•  ๋•Œ ์–ป์€ ์•„์ด๋”” ๊ฐ’์„ ๋งž๊ฒŒ ๋ฐ”๊พธ๊ธฐ ์–ด๋ ค์›Œ์„œ ์ด๋‹ค.

 

 

App์— State ๋งŒ๋“ค๊ธฐ

 

 

์ฒ˜์Œ ๊น€๋‹ค์ •๋‹˜ ์ฑ… ๋”ฐ๋ผ ๋งŒ๋“ค์—ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ readOnly๋ผ๋Š” state๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ๊ธฐ๋ณธ๊ฐ’์„ read๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.

๊ฐ’์ด read์ผ ๋•Œ๋Š” addGoal์ด ์ž‘๋™ํ•˜๋Š” GoalForm ์ปดํฌ๋„ŒํŠธ๊ฐ€

๊ฐ’์ด edit์ผ ๋•Œ๋Š” editGoal์ด ์ž‘๋™ํ•˜๋Š” editGoalForm ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‚˜ํƒ€๋‚˜๋„๋ก ํ•ด๋ณด์ž

 

 

  let [readOnly, readOnlyChange] = useState('read');

 

 

๊ทธ๋Ÿฌ๋ ค๋ฉด ๋ชฉํ‘œ๋ฅผ ํด๋ฆญํ• ๋•Œ ํ•ด๋‹น state์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค.

๋ชฉํ‘œ ์ˆ˜์ • ์ด๋ฒคํŠธํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜์—์„œ state๊ฐ€ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋„๋ก readOnly์™€ readOnlyChange ํ•จ์ˆ˜๋ฅผ props๋กœ ๋‚ด๋ ค์ค€๋‹ค.

 

 

<Route exact path="/goals">
<BasicNavBar/>
  <GoalForm addGoal={addGoal} editTmpGoal={editTmpGoal} />
  <Goals goal={goal.items} addGoal={addGoal} deleteGoal={deleteGoal}
  editToGoalForm={editToGoalForm} readOnly={readOnly} readOnlyChange={readOnlyChange}  />
</Route>

 

๊ทธ๋ฆฌ๊ณ  ๊ฐ๊ฐ props๋กœ ๋ฐ›์•„์ค€ ๋‹ค์Œ

 

 

  let readOnly = props.readOnly;
  let readOnlyChange = props.readOnlyChange;

 

 

๋ชฉํ‘œ ์ˆ˜์ • ์ด๋ฒคํŠธํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜์— readOnly๊ฐ€ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๊ณ  console.log(readOnly)๋กœ ์ž˜ ๋ฐ”๋€Œ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

 

 

  //๋ชฉํ‘œ ์ˆ˜์ • ์ด๋ฒคํŠธํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜
  function editEventHandler(e){
    const { id } = e.target;
    readOnlyChange(readOnly='edit');
    console.log(readOnly)
  }

 

 

 

state๊ฐ€ ์ž˜ ๋ณ€๊ฒฝ๋˜์—ˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๊ทธ๋Ÿผ ์ด์ œ ์กฐ๊ฑด๋ฌธ์„ ํ†ตํ•ด readOnly state๊ฐ€ edit์ด๋ฉด GoalEditForm ์ด๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋“ฑ์žฅํ•˜๋„๋ก ํ•ด์ฃผ์ž.

 

 

GoalEditForm ์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ

 

 

์•„์ง GoalEditForm ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์ค€๋‹ค.

GoalEditForm.js๋ผ๋Š” ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  GoalForm ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ๋‹ค ๋ถ™์—ฌ๋„ฃ์–ด์ค€๋‹ค.

 

 

import { Button, Grid, Box, TextField } from "@mui/material";
import React, { useState } from "react";


export default function GoalEditForm(props){

    /* props ์„ ์–ธ ์‹œ์ž‘ */
    const TextField_Ref = props.TextField_Ref;
    const addGoal = props.addGoal;

    /* props ์„ ์–ธ ๋ */


    /* Hook ์„ ์–ธ ์‹œ์ž‘ */


    let [goalTodo, goalTodoChange] = useState({item : { title : '' }}, props.addGoal);

    /* Hook ์„ ์–ธ ๋ */

    /* ํ•จ์ˆ˜ ์„ ์–ธ ์‹œ์ž‘ */


    function onInputChange(e) {
        const thisGoal = goalTodo.item;
        thisGoal.title = e.target.value;
        goalTodoChange({item : thisGoal})
        console.log(thisGoal)
    }

    function onButtonClick(){
        addGoal(goalTodo.item);
        goalTodoChange({item : {title: ''}});
    }
    
    function enterKeyEventHandler(e){
        if(e.key === 'Enter'){
            onButtonClick();
        }
    
      }

    /* ํ•จ์ˆ˜ ์„ ์–ธ ๋ */


    return(
        <Box style={{ margin : 16, padding: 16, position:'relative' }}>
        <p>์ˆ˜์ •</p>
            <Grid container>
                <Grid xs={11} md={11} item style={{paddingRight: 12}}>
                    <p className="goalform_title" style={{fontSize: '12px', color: '#aeaeae'}}>์ œ๋ชฉ</p>
                    <TextField fullWidth id="goalform_textfield" variant="standard" onChange={onInputChange} ref={TextField_Ref} /> 
                </Grid>
                <Grid xs={1} md={1} item >
                    <Button fullWidth color="secondary" onClick={onButtonClick} 
                    value={goalTodo.item.title} onKeyPress={enterKeyEventHandler}
                    style={{position:'relative', top: '-85px',  fontSize: '14px', fontWeight: '600', color: '#111'}} >
                        ํ™•์ธ
                    </Button>    
                </Grid>
            </Grid>
        </Box>
    )

}

 

 

๋ฐ•์Šค ์•ˆ์˜ <p>์ˆ˜์ •</p> ์€ ์ปดํฌ๋„ŒํŠธ ๋ชจ์–‘์ด ๊ฐ™์•„ ๋ˆˆ์œผ๋กœ ๋ณ€ํ™”๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋„ฃ์–ด๋ณด์•˜๋‹ค. 

 

๋‹ค์‹œ App.js๋กœ ์ด๋™ํ•˜์ž.

 

 

 

์กฐ๊ฑด๋ฌธ์œผ๋กœ ์ปดํฌ๋„ŒํŠธ ๋…ธ์ถœํ•˜๊ธฐ

 

 

์˜ˆ์ „์— php ๋ฐฐ์› ์„ ๋•Œ๋Š” HTML ํƒœ๊ทธ ์‚ฌ์ด์— ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋„ฃ์–ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๊ฒŒ ์—„์ฒญ ์ข‹์•„๋ณด์˜€๋Š”๋ฐ

๋ฆฌ์•กํŠธ๋„ ๋…ธ๋“œ๋„ HTML ๋‚ด๋ถ€์—์„œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

php๋งŒ์˜ ์žฅ์ ์ธ ์ค„ ์•Œ์•˜๋Š”๋ฐ...

 

 

๋‚ด๊ฐ€ ์กฐ๊ฑด๋ฌธ์„ ์จ์•ผ ํ•˜๋Š” ๊ณณ์€ Route /goals๋กœ ๊ตฌ๋ถ„๋œ ๊ณณ์ด๋‹ค.

BasicNavBar ๊นŒ์ง€๋Š” ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š์•„๋„ ๋˜๊ณ  ์˜ค์ง GoalForm ๋ถ€๋ถ„๋งŒ ๋ฐ”๋€Œ๋ฉด ๋œ๋‹ค.

๋ฆฌ์•กํŠธ์—์„œ๋Š” ๋ณ€์ˆ˜ ์“ฐ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ค‘๊ฐ„์— ๋„ฃ์œผ๋ ค๊ณ  ํ•  ๋•Œ๋„ ์ค‘๊ด„ํ˜ธ { } ๋ฅผ ์ด์šฉํ•œ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  if else ๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ณ„๋„๋กœ function์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 

์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ณต์žกํ•˜์ง€ ์•Š๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 <Route exact path="/goals">
    <BasicNavBar/>
    {
     readOnly === 'read' ? <GoalForm addGoal={addGoal} /> : <GoalEditForm editGoal={editGoal} />
    }
  <Goals goal={goal.items} addGoal={addGoal} deleteGoal={deleteGoal} editToGoalForm={editToGoalForm} readOnly={readOnly} readOnlyChange={readOnlyChange}  />
</Route>

 

 

readOnly๊ฐ€ read ์ƒํƒœ, ์ฆ‰ ๊ธฐ๋ณธ ์ƒํƒœ์ด๋ฉด GoalForm์œผ๋กœ addGoal์ด ์ž‘๋™ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๊ณ ,

readOnly๊ฐ€ read ๊ฐ€ ์•„๋‹ˆ๋ฉด GoalEditForm์œผ๋กœ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.

 

 

๊ทธ๋ฆฌ๊ณ  ํด๋ฆญํ•ด๋ณด๋ฉด 

 

 

 

 

๋‘๋ฒˆ์งธ๋ฅผ ํด๋ฆญํ–ˆ์„ ๋•Œ, readOnly๊ฐ€ edit์œผ๋กœ ๋ฐ”๋€Œ๊ณ  '์ˆ˜์ •'์ด๋ผ๊ณ  ์ ํžŒ GoalEditForm ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์ด ๋ณด์ธ๋‹ค.

 

 

๊ทธ๋Ÿผ ์ด์ œ GoalEditForm์ผ ๋•Œ ์ œ๋ชฉ ๋ถ€๋ถ„์— ๊ฐ’์ด ๋…ธ์ถœ๋˜๋„๋ก ํ•ด๋ณด์ž.

 

 

 

์ œ๋ชฉ์— title ๋…ธ์ถœ๋˜๋„๋ก ํ•˜๊ธฐ

 

 

document.querySelector ๋ฅผ ์‚ฌ์šฉํ• ๊ฑด๋ฐ GoalEditForm์œผ๋กœ ์žฌ๋ Œ๋”๋ง ๋˜๋ฉด์„œ ์ˆœ๊ฐ„์ ์œผ๋กœ DOM์„ ์ฐพ์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

TextFieldControl์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ  editToGoalForm์— ์ฝœ๋ฐฑํ•จ์ˆ˜๋กœ ๋„ฃ์–ด์ค€๋‹ค.

 

 

 

  //๋ชฉํ‘œ GoalForm์œผ๋กœ ๋ณด๋‚ด๊ธฐ ํ•จ์ˆ˜
  function editToGoalForm(id){
    const thisItems = goal.items; // goal State ์›๋ณธ ์นดํ”ผ
    const currentEditItems = thisItems.filter(e => e.id === id)
    TextFieldControl(currentEditItems);
  }

  //TextField value ๋ณ€๊ฒฝ ํ•จ์ˆ˜

  function TextFieldControl(currentEditItems) {
    const TextField = document.querySelector("#goaleditform_textfield");
    TextField.value = currentEditItems[0].title;
  }

 

 

๊ทธ๋Ÿฐ๋ฐ ์ด๋Œ€๋กœ ํด๋ฆญํ•ด๋„ ์—ฌ์ „ํžˆ value๊ฐ’์„ ๋ชป ์žก๋Š”๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋–ด๋Š”๋ฐ, readOnly๊ฐ€ edit์œผ๋กœ ๋ณ€๊ฒฝ๋˜๋ฉด์„œ ์žฌ๋ Œ๋”๋ง ๋˜๋Š” ์‚ฌ์ด์— ๋‹ค์Œ ํ•จ์ˆ˜๋“ค์ด ์ฃผ๋ฅด๋ฅต ์‹คํ–‰๋˜์–ด ๋ฒ„๋ ค์„œ DOM ๊ฐ’์„ ๋ชป ์žก๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

๊ทธ๋ž˜์„œ ๋‹ค์‹œ Goals ์ปดํฌ๋„ŒํŠธ๋กœ ๊ฐ€์„œ  editEventHandler๋ฅผ ๋งŒ์ ธ์ฃผ๊ธฐ๋กœ ํ–ˆ๋‹ค.

async await๋ฅผ ์‚ฌ์šฉํ•ด์„œ readOnlyChange๊ฐ€ ์‹คํ–‰๋˜๊ณ , ๋‹ค์Œ์— editToGoalForm์œผ๋กœ id ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋Š” ์‹์œผ๋กœ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰๋˜๋„๋ก ํ–ˆ๋‹ค.

 

 

  //๋ชฉํ‘œ ์ˆ˜์ • ์ด๋ฒคํŠธํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜
  async function editEventHandler(e){
    const { id } = e.target;
    await readOnlyChange(readOnly='edit');
    console.log(readOnly)
    await editToGoalForm(id)
  }

 

 

๊ทธ๋žฌ๋”๋‹ˆ 

 

 

์ง 

 

 

 

 

 

 

์ด์ œ ๋‹ค์Œ ํ•  ์ผ์€ ์ €๋ ‡๊ฒŒ title์ด ๋ณ€๊ฒฝ๋˜๋ฉด add์ฒ˜๋Ÿผ onChange์˜ ๋ณ€๊ฒฝ๊ฐ’์„ title ์— ๋‹ด๊ณ 

id๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๊ฐ€์ง€๊ณ  ์™€์„œ ๊ธฐ์กด goal state์— ๊ฐ™์€ id ๊ฐ’์„ ์ฐพ์•„์„œ title์„ ๋ณ€๊ฒฝํ•˜๊ฒŒ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค!

 

 

์ ์  ๊ธธ์ด ๋ณด์ธ๋‹ค..!

ํ™”์ดํŒ…!

 

 

728x90