|
1 | | -import { FormEvent } from "react"; |
| 1 | +import { ChangeEvent, FormEvent, useState } from "react"; |
2 | 2 |
|
3 | 3 | interface SocialLoginEmailProps { |
4 | 4 | adapter: string; |
5 | 5 | handleSocialLoginClick: (params: { adapter: string; loginParams: { loginProvider: string; login_hint?: string } }) => void; |
6 | 6 | } |
7 | 7 | export default function SocialLoginEmail(props: SocialLoginEmailProps) { |
8 | 8 | const { handleSocialLoginClick, adapter } = props; |
| 9 | + const [isValidEmail, setIsValidEmail] = useState(false); |
| 10 | + |
9 | 11 | const handleEmailSubmit = (e: FormEvent<HTMLFormElement>) => { |
10 | 12 | e.preventDefault(); |
11 | 13 | const email = ((e.target as HTMLFormElement)[0] as HTMLInputElement).value; |
12 | 14 | if (email) handleSocialLoginClick({ adapter, loginParams: { loginProvider: "email_passwordless", login_hint: email } }); |
13 | 15 | }; |
| 16 | + const handleEmailChange = (e: ChangeEvent<HTMLInputElement>) => { |
| 17 | + const email = e.target.value; |
| 18 | + const emailValid = email.match(/^([\w.%+-]+)@([\w-]+\.)+([\w]{2,})$/i); |
| 19 | + setIsValidEmail(!!emailValid); |
| 20 | + }; |
14 | 21 | return ( |
15 | 22 | <div className="w3ajs-email-passwordless w3a-group w3a-group--email"> |
16 | 23 | <div className="w3a-group__title">EMAIL</div> |
17 | 24 | <form className="w3ajs-email-passwordless-form" onSubmit={(e) => handleEmailSubmit(e)}> |
18 | | - <input className="w3a-text-field" type="email" name="email" required placeholder="Email" /> |
19 | | - <button className="w3a-button" type="submit"> |
| 25 | + <input className="w3a-text-field" type="email" name="email" required placeholder="Email" onChange={(e) => handleEmailChange(e)} /> |
| 26 | + <button disabled={!isValidEmail} className="w3a-button" type="submit"> |
20 | 27 | Continue with Email |
21 | 28 | </button> |
22 | 29 | </form> |
|
0 commit comments