lil.dev
Published on

๐ŸŽ‰ ์ปฌ๋Ÿฌ๋ฆฌ์ŠคํŠธ ํ”„๋กœ์ ํŠธ #13 Pallete table - ๋‘ ๊ฐœ์˜ ์„ ํƒ์นธ ๋ถ„ํ• 

๊ธ€์“ด์ด

    ๐Ÿ“Œ ๋ชฉ์ฐจ

    Welcome

    โœจ ์ปฌ๋Ÿฌ๋ฆฌ์ŠคํŠธ ์‚ฌ์ดํŠธ(์•„์ง ์—†์Œ

    ๐Ÿ’๐Ÿป

    1. ์„ ํƒ์นธ ํ…Œ์ด๋ธ”์„ 2๊ฐœ๋กœ ๋ถ„ํ• ํ•˜๊ณ  ๊ฐ๊ฐ์ด ๋‹ค๋ฅธ ์ƒ‰๊น” ๊ฐ’์„ ๊ฐ–๋„๋ก ๋งŒ๋“ค๊ธฐ 1-1. useState์—†์• ๊ธฐ 1-2. formData.getAll ์ ์šฉํ•˜๊ธฐ 1-3. ์ธ๋ฑ์Šค ์ˆœ์„œ๋กœ ๋ฐ˜ ๊ฐˆ๋ผ์ฃผ๊ธฐ

    ์„ ํƒ์นธ ํ…Œ์ด๋ธ”์„ 2๊ฐœ๋กœ ๋ถ„ํ• ํ•˜๊ณ  ๊ฐ๊ฐ์ด ๋‹ค๋ฅธ ์ƒ‰๊น” ๊ฐ’์„ ๊ฐ–๋„๋ก ๋งŒ๋“ค๊ธฐ

    ![์„ ํƒ์นธ 2๊ฐœ๋กœ ๋ถ„ํ• ](/static/images/์„ ํƒ์นธ 2๊ฐœ๋กœ ๋ถ„ํ• .png)


    1-1. useState์—†์• ๊ธฐ

    ์˜ค๋Š˜์€ ๊ธฐ์กด์˜ ์„ ํƒ์นธ์„ ๋‘ ๊ฐœ๋กœ ๋Š˜๋ฆฌ๊ณ 
    ๊ฐ ์„ ํƒ์นธ์ด ์„œ๋กœ ๋‹ค๋ฅธ ์ƒ‰์ด ์ ์šฉ๋  ์ˆ˜ ์žˆ๋„๋ก
    ์„ ํƒ์นธ์„ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค.

    ๊ธฐ์กด์˜ ์„ ํƒ์นธ์€ 10๊ฐœ์˜ box๋ฅผ ๊ฐ€์ง„ ์นธ์ด์—ˆ๋Š”๋ฐ
    ์ปดํฌ๋„ŒํŠธ ์ž์ฒด๋ฅผ ๋ณต์‚ฌํ•˜๋ฉด ๋‘ ๊ฐœ์˜ ์„ ํƒ์นธ์— ๊ฐ™์€ ์ƒ‰์ด ์ ์šฉ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

    ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด useState๋ฅผ ์‚ฌ์šฉํ–ˆ๋˜ ์ฝ”๋“œ๋ฅผ ์‚ญ์ œํ•˜๊ณ 
    form์˜ ๋ชจ๋“  ๊ฐ’์„ ์ผ๋‹จ ๋‹ค ๊ฐ€์ ธ์˜ค๋Š” formData.getAll์„ ์‚ฌ์šฉํ•ด
    ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ ๋ฐ˜ ์ž˜๋ผ์ฃผ๋Š” ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋‹ค.

    ๊ธฐ์กด์— ์žˆ์—ˆ๋˜ ์ฃผ์กฐ์ƒ‰,๋ณด์กฐ์ƒ‰,๊ฐ•์กฐ์ƒ‰,์„ค๋ช… ์ƒํƒœ๋ฅผ ๋ชจ๋‘ ์—†์•ด๋‹ค.

    //9๋ฒˆ - ๊ธฐ๋ณธ ํ…์ŠคํŠธ๋ฅผ ๋„ฃ์–ด์ฃผ๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ ํ…์ŠคํŠธ๋ฅผ ์ƒํƒœ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.
    const [mainColor, setMainColor] = React.useState('์ฃผ์กฐ์ƒ‰:')
    
    const [subColor, setSubColor] = React.useState('๋ณด์กฐ์ƒ‰:')
    
    const [pointColor, setPointColor] = React.useState('๊ฐ•์กฐ์ƒ‰:')
    
    const [explanation, setExplanation] = React.useState('์ปจ์…‰:\n์ฃผ์กฐ์ƒ‰:\n๋ณด์กฐ์ƒ‰:\n๊ฐ•์กฐ์ƒ‰:\n๋ฐฐ์ƒ‰ ๊ธฐ๋ฒ•:')
    

    ๊ทธ๋ฆฌ๊ณ  ํ•˜๋‚˜์˜ ์„ ํƒ์นธ์ผ๋•Œ colors๋กœ ์ €์žฅํ•ด ์ƒ‰๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ์ฝ”๋“œ๋„ ๋ชจ๋‘ ์‚ญ์ œํ•ด์ฃผ์—ˆ๋‹ค.

    const rawData = new FormData(event.target as HTMLFormElement)
    
    const colors = rawData.get('colors')
    
    if (colors !== null) {
      // ์ „์ฒ˜๋ฆฌ, preprocess
      const data = [
        {
          jubogang: {
            ju: rawData.get('ju'),
            bo: rawData.get('bo'),
            gang: rawData.get('gang'),
          },
          box: JSON.parse(colors as string),
          answer: rawData.get('answer'),
        },
      ]
      saveColors(data as MyColors[])
    }
    

    ๋˜ ์ฃผ์กฐ์ƒ‰,๋ณด์กฐ์ƒ‰,๊ฐ•์กฐ์ƒ‰์„ ๊ฐ๊ฐ์˜ value๋กœ ๊ฐ€์ ธ์™”๋˜ ๊ฐ์ฒด์™€ event์ฝ”๋“œ๋„ ์‚ญ์ œํ–ˆ๋‹ค.

    <input
      id="color-selection"
      placeholder="์ฃผ์กฐ์ƒ‰:"
      value={mainColor}
      onChange={(event) => setMainColor(event.target.value)}
    />
    

    ์ด๋Ÿฐ์‹์œผ๋กœ ๋ณด์กฐ์ƒ‰,๊ฐ•์กฐ์ƒ‰,์„ค๋ช…์˜ input์— ์žˆ๋Š” value์™€ onChange์ฝ”๋“œ๋ฅผ ๋‹ค ์‚ญ์ œํ•ด์ฃผ์—ˆ๋‹ค.

    1-2. formData.getAll ์ ์šฉํ•˜๊ธฐ && 1-3. ์ธ๋ฑ์Šค ์ˆœ์„œ๋กœ ๋ฐ˜ ๊ฐˆ๋ผ์ฃผ๊ธฐ

    ์ƒํƒœ๋ฅผ ์—†์•ด์œผ๋‹ˆ ๊ทธ ๋‹ค์Œ formData.getAll์„ ์‚ฌ์šฉํ•ด input์˜ ๋ชจ๋“  ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ์ฝ”๋“œ๋ฅผ ๋„ฃ์—ˆ๋‹ค.

        <form
            onSubmit={(event) => {
              event.preventDefault();
              const formData = new FormData(event.target as HTMLFormElement);
    
              const colors = formData.get("colors");
              const rawData = {
                box: JSON.parse(colors as string),
                ju: formData.getAll("ju"),
                bo: formData.getAll("bo"),
                gang: formData.getAll("gang"),
                answer: formData.getAll("answer"),
              };
    
              if (colors !== null) {
                // ์ „์ฒ˜๋ฆฌ, preprocess
                const data = [
                  //์ฒซ๋ฒˆ์งธ ์„ ํƒ์นธ์€ 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ๊ฐ’์„ ์ž˜๋ผ ๊ฐ€์ง€๊ณ , ์„ค๋ช…์€ ์ด 2๊ฐœ์ด๋ฏ€๋กœ ์ฒซ๋ฒˆ์งธ ๊ฐ’๋“ค๋งŒ ๊ฐ€์ง„๋‹ค.
                  {
                    box: rawData.box.slice(0, 10),
                    jubogang: {
                      ju: rawData.ju[0],
                      bo: rawData.bo[0],
                      gang: rawData.gang[0],
                    },
                    answer: rawData.answer[0],
                  },
                  //๋‘๋ฒˆ์งธ ์„ ํƒ์นธ์€ 10๋ถ€ํ„ฐ 19๊นŒ์ง€์˜ ๊ฐ’์„ ์ž˜๋ผ ๊ฐ€์ง€๊ณ , ์„ค๋ช…์˜ 2๋ฒˆ์งธ ๊ฐ’๋“ค์„ ๊ฐ€์ง„๋‹ค.
                  {
                    box: rawData.box.slice(10, 20),
                    jubogang: {
                      ju: rawData.ju[1],
                      bo: rawData.bo[1],
                      gang: rawData.gang[1],
                    },
                    answer: rawData.answer[1],
                  },
                ];
                alert(JSON.stringify(data));
                // saveColors(data as MyColors[]);
              }
            }}
          >
    

    ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ค€ ํ›„
    input์—์„œ๋Š” type๊ณผ name์„ ์ž‘์„ฑํ•ด ๊ฐ๊ฐ์˜ formData์— ํ•ด๋‹นํ•˜๋Š” ์ด๋ฆ„์„ ์ผ๋‹ค.

            <label className="color-label" htmlFor="main-color">
              ์ฃผ์กฐ์ƒ‰
            </label>
            <input
              id="color-selection"
              placeholder="์ฃผ์กฐ์ƒ‰:"
              type="text"
              name="ju"
            />
            <label className="color-label" htmlFor="sub-color">
              ๋ณด์กฐ์ƒ‰
            </label>
            <input
              id="color-selection"
              placeholder="๋ณด์กฐ์ƒ‰:"
              type="text"
              name="bo"
            />
            <label className="color-label" htmlFor="point-color">
              ๊ฐ•์กฐ์ƒ‰
            </label>
            <input
              id="color-selection"
              placeholder="๊ฐ•์กฐ์ƒ‰:"
              type="text"
              name="gang"
            />
            <label className="color-label" htmlFor="explanation" id="explanation">
              ๋ฐฐ์ƒ‰ ์„ค๋ช…
            </label>
            <textarea
              id="explanation"
              name="answer"
              placeholder={"์ปจ์…‰:\n์ฃผ์กฐ์ƒ‰:\n๋ณด์กฐ์ƒ‰:\n๊ฐ•์กฐ์ƒ‰:\n๋ฐฐ์ƒ‰ ๊ธฐ๋ฒ•:"}
            />
            <p id="allColor">์ฃผ๋ณด๊ฐ• ๊ธฐ์ž…</p>
            <label className="color-label" htmlFor="main-color">
              ์ฃผ์กฐ์ƒ‰
            </label>
            <input
              id="color-selection"
              type="text"
              name="ju"
              placeholder="์ฃผ์กฐ์ƒ‰:"
            />
            <label className="color-label" htmlFor="sub-color">
              ๋ณด์กฐ์ƒ‰
            </label>
            <input
              id="color-selection"
              type="text"
              name="bo"
              placeholder="๋ณด์กฐ์ƒ‰:"
            />
            <label className="color-label" htmlFor="point-color">
              ๊ฐ•์กฐ์ƒ‰
            </label>
            <input
              id="color-selection"
              type="text"
              name="gang"
              placeholder="๊ฐ•์กฐ์ƒ‰:"
            />
            <label className="color-label" htmlFor="explanation" id="explanation">
              ๋ฐฐ์ƒ‰ ์„ค๋ช…
            </label>
            <textarea
              id="explanation"
              name="answer"
              placeholder={"์ปจ์…‰:\n์ฃผ์กฐ์ƒ‰:\n๋ณด์กฐ์ƒ‰:\n๊ฐ•์กฐ์ƒ‰:\n๋ฐฐ์ƒ‰ ๊ธฐ๋ฒ•:"}
            />
    

    ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ผ๋‹จ ์„ ํƒ์นธ์€ ๊ฐ๊ฐ์˜ ์ƒ‰์„ ์ ์šฉํ•˜๊ณ , ์ง€์šธ ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๊ฐ€ ๋œ๋‹ค.