-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable to update saves name on click
- Loading branch information
1 parent
411ef03
commit 5bf8a9b
Showing
4 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Input, Typography } from "@material-tailwind/react"; | ||
import { useState } from "react"; | ||
import { type SubmitHandler, useForm } from "react-hook-form"; | ||
import { type GameSingle } from "@/hooks/useGame"; | ||
|
||
type NameInputProps = { | ||
name: string; | ||
onSubmit: (data: FormInputs) => void; | ||
}; | ||
|
||
type FormInputs = Pick<GameSingle, "Name">; | ||
|
||
const NameInput = (props: NameInputProps) => { | ||
const [edit, setEdit] = useState<boolean>(false); | ||
const { register, handleSubmit, setFocus } = useForm<FormInputs>(); | ||
|
||
const onSubmit: SubmitHandler<FormInputs> = (data) => { | ||
setEdit(false); | ||
props.onSubmit(data); | ||
}; | ||
|
||
const handleEdit = () => { | ||
setEdit(true); | ||
setTimeout(() => { | ||
setFocus("Name"); | ||
}, 300); | ||
}; | ||
|
||
if (edit) { | ||
return ( | ||
<form | ||
className="col-span-5 !h-[40px] w-0 grow" | ||
onSubmit={handleSubmit(onSubmit)} | ||
> | ||
<Input | ||
className="!p-0" | ||
variant="standard" | ||
label={props.name} | ||
{...register("Name", { | ||
onBlur: () => { | ||
setEdit(false); | ||
}, | ||
})} | ||
/> | ||
</form> | ||
); | ||
} | ||
|
||
return ( | ||
<div className="col-span-5 flex h-[40px] w-0 grow items-center "> | ||
<Typography onClick={() => handleEdit()} className="h-6 w-full"> | ||
{props.name} | ||
</Typography> | ||
</div> | ||
); | ||
}; | ||
|
||
export default NameInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters