Refactor: Disable signUp button when form is invalid
This commit is contained in:
parent
8a416c235b
commit
3cbe046e08
1 changed files with 11 additions and 1 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import * as Form from "@radix-ui/react-form";
|
import * as Form from "@radix-ui/react-form";
|
||||||
import { FormEvent, useContext, useState } from "react";
|
import { FormEvent, useContext, useEffect, useState } from "react";
|
||||||
import { Link, useNavigate } from "react-router-dom";
|
import { Link, useNavigate } from "react-router-dom";
|
||||||
import InputComponent from "../../components/inputComponent";
|
import InputComponent from "../../components/inputComponent";
|
||||||
import { Button } from "../../components/ui/button";
|
import { Button } from "../../components/ui/button";
|
||||||
|
|
@ -20,6 +20,8 @@ export default function SignUp(): JSX.Element {
|
||||||
const [inputState, setInputState] =
|
const [inputState, setInputState] =
|
||||||
useState<signUpInputStateType>(CONTROL_INPUT_STATE);
|
useState<signUpInputStateType>(CONTROL_INPUT_STATE);
|
||||||
|
|
||||||
|
const [isDisabled, setDisableBtn] = useState<boolean>(true)
|
||||||
|
|
||||||
const { password, cnfPassword, username } = inputState;
|
const { password, cnfPassword, username } = inputState;
|
||||||
const { setErrorData, setSuccessData } = useContext(alertContext);
|
const { setErrorData, setSuccessData } = useContext(alertContext);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
@ -30,6 +32,13 @@ export default function SignUp(): JSX.Element {
|
||||||
setInputState((prev) => ({ ...prev, [name]: value }));
|
setInputState((prev) => ({ ...prev, [name]: value }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (password !== cnfPassword) return setDisableBtn(true);
|
||||||
|
if (password === "" || cnfPassword === "") return setDisableBtn(true);
|
||||||
|
if (username === "") return setDisableBtn(true);
|
||||||
|
setDisableBtn(false);
|
||||||
|
}, [password, cnfPassword, username, handleInput]);
|
||||||
|
|
||||||
function handleSignup(): void {
|
function handleSignup(): void {
|
||||||
const { username, password } = inputState;
|
const { username, password } = inputState;
|
||||||
const newUser: UserInputType = {
|
const newUser: UserInputType = {
|
||||||
|
|
@ -158,6 +167,7 @@ export default function SignUp(): JSX.Element {
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
<Form.Submit asChild>
|
<Form.Submit asChild>
|
||||||
<Button
|
<Button
|
||||||
|
disabled={isDisabled}
|
||||||
type="submit"
|
type="submit"
|
||||||
className="mr-3 mt-6 w-full"
|
className="mr-3 mt-6 w-full"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue