- Published on
๐ ์ปฌ๋ฌ๋ฆฌ์คํธ ํ๋ก์ ํธ #13 Pallete table - ๋ ๊ฐ์ ์ ํ์นธ ๋ถํ
- ๊ธ์ด์ด
๐ ๋ชฉ์ฐจ

๐๐ป
- ์ ํ์นธ ํ
์ด๋ธ์ 2๊ฐ๋ก ๋ถํ ํ๊ณ ๊ฐ๊ฐ์ด ๋ค๋ฅธ ์๊น ๊ฐ์ ๊ฐ๋๋ก ๋ง๋ค๊ธฐ
1-1.
useState์์ ๊ธฐ 1-2.formData.getAll์ ์ฉํ๊ธฐ 1-3. ์ธ๋ฑ์ค ์์๋ก ๋ฐ ๊ฐ๋ผ์ฃผ๊ธฐ
์ ํ์นธ ํ ์ด๋ธ์ 2๊ฐ๋ก ๋ถํ ํ๊ณ ๊ฐ๊ฐ์ด ๋ค๋ฅธ ์๊น ๊ฐ์ ๊ฐ๋๋ก ๋ง๋ค๊ธฐ

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๋ฐฐ์ ๊ธฐ๋ฒ:"}
/>
์ด๋ ๊ฒ ํ๋ฉด ์ผ๋จ ์ ํ์นธ์ ๊ฐ๊ฐ์ ์์ ์ ์ฉํ๊ณ , ์ง์ธ ์ ์๋ ์ํ๊ฐ ๋๋ค.