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

2021. 12. 15. 00:41ใ†WEB Dev/ToyProject

728x90


๐Ÿ”ท 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