-
[ToyProject-Todomate] ํฌ๋๋ฉ์ดํธ ํด๋ก ํ๋ก์ ํธ 10WEB Dev/ToyProject 2021. 12. 15. 00:41728x90
๐ท CloneTodo โ - Todomate Clone Project | Team CloneMate
CloneTodo : ๋ง์ ์ฌ๋๋ค์ด ์ฌ๋ํ๋ ํฌ๋๋ฉ์ดํธ๋ฅผ ํด๋ก ํ์ฌ ์น ์๋น์ค๋ฅผ ๋ฐฐํฌํด๋ณด๋ ํ๋ก์ ํธ
์ค๋์ ๋ก๊ทธ์ธ ํ๋ฉด์ ๊ตฌํํด๋ณธ๋ค.
์ด์ ์์ ํ ๋ผ์ฐํฐ๋ฅผ ํตํด /signin ๊ฒฝ๋ก๋ก ๋ค์ด๊ฐ๋ค.
๋ก๊ทธ์ธ ํ๋ฉด์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๋ ฅํ ์ ์๋ input ํ๊ทธ์ ์ ๋ ฅํ ๊ฐ์ ์ ์กํ๋ button์ผ๋ก ์ด๋ฃจ์ด์ ธ ์๊ณ , ์ด๋ค์ form ํ๊ทธ๋ก ๊ฐ์ธ์ผ ํ๋ค.
1. input ํ๊ทธ 2๊ฐ ๋ฃ๊ธฐ
MUI ์ปดํฌ๋ํธ์์ ์ ๋ ฅ์ ํ ์ ์๋ input ํ๊ทธ์ ๊ฐ์ ํ ์คํธํ๋ ์ปดํฌ๋ํธ๋ฅผ ํ๋ฉด์ ๋ฃ์ด๋ณธ๋ค.
๋ ๊ฐ์ input ํ๊ทธ ์ค ํ ๊ฐ์ง๋ password์ด๊ธฐ ๋๋ฌธ์ password์ ๊ด๋ จ๋ ์ปดํฌ๋ํธ๋ฅผ ์ฐพ์์ ์ฌ์ฉํด๋ณธ๋ค.
์์ password๊ฐ ๋ง์์ ๋ค์ด์ ํด๋น ์ฝ๋๋ฅผ ์ฐพ์ Signin ์ปดํฌ๋ํธ์ ๋ถ์ฌ ๋ฃ์๋ค.
<TextField id="standard-password-input" label="Password" type="password" autoComplete="current-password" variant="standard" />
์ฌ๊ธฐ์ TextField ๋ผ๋ ์ปดํฌ๋ํธ๊ฐ ํ์ํ๊ธฐ ๋๋ฌธ์ ์๋์์ฑ์ ํตํด ์๋จ์ import ๋ ์ ์๋๋ก ํด์คฌ๋ค.
๊ทธ๋ผ ์ด TextField์ id์ label ๋ฑ์ ์ค์ ํ๋ฉด email๋ ๋ง๋ค ์ ์์ง ์์๊น?
<form> ํ๊ทธ๋ฅผ ์ต์๋จ ์ปดํฌ๋ํธ๋ก ํด์ TextField ์ปดํฌ๋ํธ๋ฅผ ๋ ๊ฐ ์ฐ์์ผ๋ก ์ฝ์ ํ๋ค.
import React from 'react'; import { ThemeProvider, createTheme } from '@mui/material/styles'; import { TextField } from '@mui/material'; function Signin(){ return ( <form> <TextField id="standard-email-input" label="Email" type="email" autoComplete="current-email" variant="standard" /> <TextField id="standard-password-input" label="Password" type="password" autoComplete="current-password" variant="standard" /> </form>) } export default Signin;
์ฌ๊ธฐ์ TextField์ props๋ค์ ์ฃผ์ด์ ์ฌ๋ฌ๊ฐ์ง ํํ๋ฅผ ๋ง๋ค ์ ์๋ค
๊ฐ์ฅ ํฐ ํํ๋ฅผ ์ค์ ํ๋ ๊ฒ์ variant props์ธ๋ฐ ์๋์ ๊ทธ๋ฆผ๊ณผ ๊ฐ๋ค. ๋ด๊ฐ ์ ํํ ๊ฒ์ด Standard ํ์ ์ด๋ค.
๊ทธ๋ฆฌ๊ณ ๋๋ ํ๋ฉด์ ๊ฑฐ์ ๊ฐ๋ํ input์์ญ์ ๋ง๋ค๊ณ ์ถ๊ธฐ ๋๋ฌธ์ fullWidth props๋ฅผ ๋ฃ์ด์ค๋ค.
๊ทธ๋ผ ์ต์๋จ form์ ํฌ๊ธฐ์ ๋ง๊ฒ ๊ฐ๋ ์ฐจ๊ฒ ๋๋ค. CSS๋ฅผ ์ด์ฉํด form ํฌ๊ธฐ๋ฅผ ์ก์์ค๋ค.
margin ๊ฐ์ margin props๋ฅผ ์ด์ฉํ๋ค.
๊ทธ๋ฆฌ๊ณ HelperText๋ผ๋ props๋ฅผ ์ด์ฉํด placeholder ๋์ ์ ํ๋จ์ ์ค๋ช ์ ๋์์ค๋ค.
import React from 'react'; import '../stylesheets/Signin.css'; import { TextField } from '@mui/material'; function Signin(){ return ( <form> <TextField id="standard-email-input" label="Email" type="email" autoComplete="current-email" variant="standard" helperText="Please enter Email" margin="dense" fullWidth /> <TextField id="standard-password-input" label="Password" type="password" autoComplete="current-password" variant="standard" helperText="Please enter Password" margin="dense" fullWidth /> </form>) } export default Signin;
์ด๋ ๊ฒ ์์ฑํ ์ ์๊ณ ๊ตฌํ๋ ํ๋ฉด์ ์๋์ ๊ฐ๋ค.
์์ฃผ ๊ทธ๋ด์ธํ๊ตฐ 2. ๋ฒํผ๊ณผ form ๊ธฐ๋ฅ์ ์ํด React-Hook-Form ์ค์น
ํ๋จ์ ๋ฒํผ์ ์ด์ฉํด form ์ ์ถ์ ํด์ผ ํ๋ค.
์๊ฐํด๋ณด๋ React-Hook-Form ์ด๋ผ๋ 3rd Party ๊ธฐ๋ฅ์ ์ฌ์ฉํ๊ธฐ๋ก ํด์ React-Hook-Form์์ ์ฝ๋๋ฅผ ๊ฐ์ง๊ณ ์ค๊ธฐ๋ก ํ๋ค.
npm install react-hook-form ์ ์ด์ฉํด react-hook-form์ ์ค์นํ๋ค.
๊ทธ๋ฆฌ๊ณ ์์ ์ ๊ฐ์ด hook ์ ์ ๋ ฅํด์ค๋ค.
๊ทธ๋ฆฌ๊ณ submit ๋ฒํผ๊ณผ ๋น๋ฐ๋ฒํธ๋ฅผ ์์๋ค๋ฉด? ์ ๊ตฌํํด์ฃผ๊ณ
todomate ์ ๋ก๊ทธ์ธ ํ๋ฉด๊ณผ ๊ฐ๊ฒ placeholder๋ฅผ ํ๊ตญ์ด๋ก ๋ฐ๊ฟ์ฃผ๋ฉด ์๋์ ๊ฐ์ ํ๋ฉด์ด ๋์จ๋ค.
(๋ณ๋๋ก CSS๋ import)
์ฝ๋๋ ํ๋จ์!
import React from 'react'; import { useState } from "react"; import { useForm } from "react-hook-form"; import '../stylesheets/Signin.css'; import { TextField } from '@mui/material'; import { Link } from 'react-router-dom'; function Signin(){ const { register, handleSubmit } = useForm(); const [result, setResult] = useState(""); const onSubmit = (data) => setResult(JSON.stringify(data)); return ( <form onSubmit={handleSubmit(onSubmit)}> <TextField {...register("Email")} id="standard-email-input" label="์ด๋ฉ์ผ" type="email" autoComplete="current-email" variant="standard" helperText="Please enter Email" margin="dense" fullWidth /> <TextField {...register("Password")} id="standard-password-input" label="๋น๋ฐ๋ฒํธ" type="password" autoComplete="current-password" variant="standard" helperText="Please enter Password" margin="dense" fullWidth /> <input className="login_btn" type="submit" value={"๋ก๊ทธ์ธ"}/> <Link to="/"><p className='forgotPwd'>๋น๋ฐ๋ฒํธ๋ฅผ ์์๋ค๋ฉด?</p></Link> </form>) } export default Signin;
728x90'WEB Dev > ToyProject' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ToyProject-Todomate] ํฌ๋๋ฉ์ดํธ ํด๋ก ํ๋ก์ ํธ 12 (0) 2021.12.19 [ToyProject-Todomate] ํฌ๋๋ฉ์ดํธ ํด๋ก ํ๋ก์ ํธ 11 (0) 2021.12.19 [ToyProject-Todomate] ํฌ๋๋ฉ์ดํธ ํด๋ก ํ๋ก์ ํธ 09 (0) 2021.12.14 [ToyProject-Todomate] ํฌ๋๋ฉ์ดํธ ํด๋ก ํ๋ก์ ํธ 08 (0) 2021.12.13 [ToyProject-Todomate] ํฌ๋๋ฉ์ดํธ ํด๋ก ํ๋ก์ ํธ 07 (0) 2021.12.10