1- import React , { useState } from 'react' ;
2- import { useSelector , useDispatch } from 'react-redux' ;
3- import {
4- increment ,
5- incrementAsync ,
6- selectCount ,
7- } from '../authSlice' ;
8- import { Link } from 'react-router-dom' ;
9-
1+ import React , { useState } from "react" ;
2+ import { useSelector , useDispatch } from "react-redux" ;
3+ import { increment , incrementAsync } from "../authSlice" ;
4+ import { Link } from "react-router-dom" ;
5+ import { useForm } from "react-hook-form" ;
106export default function Signup ( ) {
11- const count = useSelector ( selectCount ) ;
127 const dispatch = useDispatch ( ) ;
8+ const {
9+ register,
10+ handleSubmit,
11+ watch,
12+ formState : { errors } ,
13+ } = useForm ( ) ;
1314
15+ console . log ( errors ) ;
1416
1517 return (
1618 < >
17-
1819 < div className = "flex min-h-full flex-1 flex-col justify-center px-6 py-12 lg:px-8" >
1920 < div className = "sm:mx-auto sm:w-full sm:max-w-sm" >
2021 < img
@@ -28,61 +29,103 @@ export default function Signup() {
2829 </ div >
2930
3031 < div className = "mt-10 sm:mx-auto sm:w-full sm:max-w-sm" >
31- < form className = "space-y-6" action = "#" method = "POST" >
32+ < form
33+ noValidate
34+ className = "space-y-6"
35+ onSubmit = { handleSubmit ( ( data ) => {
36+ console . log ( data ) ;
37+ } ) }
38+ >
3239 < div >
33- < label htmlFor = "email" className = "block text-sm font-medium leading-6 text-gray-900" >
40+ < label
41+ htmlFor = "email"
42+ className = "block text-sm font-medium leading-6 text-gray-900"
43+ >
3444 Email address
3545 </ label >
3646 < div className = "mt-2" >
3747 < input
3848 id = "email"
39- name = "email"
49+ { ...register ( "email" , {
50+ required : "email is required" ,
51+ pattern : {
52+ value : / \b [ \w \. - ] + @ [ \w \. - ] + \. \w { 2 , 4 } \b / gi,
53+ message : "email is not valid" ,
54+ } ,
55+ } ) }
4056 type = "email"
41- autoComplete = "email"
42- required
4357 className = "block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
4458 />
59+ { errors . email && (
60+ < p className = "text-red-500" > { errors . email . message } </ p >
61+ ) }
4562 </ div >
4663 </ div >
4764
4865 < div >
4966 < div className = "flex items-center justify-between" >
50- < label htmlFor = "password" className = "block text-sm font-medium leading-6 text-gray-900" >
67+ < label
68+ htmlFor = "password"
69+ className = "block text-sm font-medium leading-6 text-gray-900"
70+ >
5171 Password
5272 </ label >
5373 < div className = "text-sm" >
54- < a href = "#" className = "font-semibold text-indigo-600 hover:text-indigo-500" >
74+ < a
75+ href = "#"
76+ className = "font-semibold text-indigo-600 hover:text-indigo-500"
77+ >
5578 Forgot password?
5679 </ a >
5780 </ div >
5881 </ div >
5982 < div className = "mt-2" >
6083 < input
6184 id = "password"
62- name = "password"
85+ { ...register ( "password" , {
86+ required : "password is required" ,
87+ pattern : {
88+ value :
89+ / ^ (? = .* \d ) (? = .* [ a - z ] ) (? = .* [ A - Z ] ) (? = .* [ a - z A - Z ] ) .{ 8 , } $ / gm,
90+ message : `- at least 8 characters\n
91+ - must contain at least 1 uppercase letter, 1 lowercase letter, and 1 number\n
92+ - Can contain special characters` ,
93+ } ,
94+ } ) }
6395 type = "password"
64- autoComplete = "current-password"
65- required
6696 className = "block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
6797 />
98+ { errors . password && (
99+ < p className = "text-red-500" > { errors . password . message } </ p >
100+ ) }
68101 </ div >
69102 </ div >
70103
71104 < div >
72105 < div className = "flex items-center justify-between" >
73- < label htmlFor = "password" className = "block text-sm font-medium leading-6 text-gray-900" >
106+ < label
107+ htmlFor = "password"
108+ className = "block text-sm font-medium leading-6 text-gray-900"
109+ >
74110 Confirm Password
75111 </ label >
76-
77112 </ div >
78113 < div className = "mt-2" >
79114 < input
80- id = "confirm-password"
81- name = "confirm-password"
115+ id = "confirmPassword"
116+ { ...register ( "confirmPassword" , {
117+ required : "confirm password is required" ,
118+ validate : ( value , formValues ) =>
119+ value === formValues . password || "password not matching" ,
120+ } ) }
82121 type = "password"
83- required
84122 className = "block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
85123 />
124+ { errors . confirmPassword && (
125+ < p className = "text-red-500" >
126+ { errors . confirmPassword . message }
127+ </ p >
128+ ) }
86129 </ div >
87130 </ div >
88131
@@ -97,8 +140,11 @@ export default function Signup() {
97140 </ form >
98141
99142 < p className = "mt-10 text-center text-sm text-gray-500" >
100- Already a Member?{ ' ' }
101- < Link to = "/login" className = "font-semibold leading-6 text-indigo-600 hover:text-indigo-500" >
143+ Already a Member?{ " " }
144+ < Link
145+ to = "/login"
146+ className = "font-semibold leading-6 text-indigo-600 hover:text-indigo-500"
147+ >
102148 Log In
103149 </ Link >
104150 </ p >
0 commit comments