mirror of
https://github.com/UiO-Gaming/name-100-uio-gamers.git
synced 2026-11-05 13:56:53 +01:00
automatically refocus text input field after submission
This commit is contained in:
1 file changed
+18
-2
@@ -1,6 +1,6 @@
|
|||||||
import { useLanguage } from "@/LanguageContext";
|
import { useLanguage } from "@/LanguageContext";
|
||||||
import { translations } from "@/translations";
|
import { translations } from "@/translations";
|
||||||
import React from "react";
|
import React, { useEffect, useRef } from "react";
|
||||||
|
|
||||||
interface GuessFormProps {
|
interface GuessFormProps {
|
||||||
input: string;
|
input: string;
|
||||||
@@ -13,9 +13,25 @@ interface GuessFormProps {
|
|||||||
const GuessForm: React.FC<GuessFormProps> = ({ input, onInputChange, onSubmit, disabled, loading }) => {
|
const GuessForm: React.FC<GuessFormProps> = ({ input, onInputChange, onSubmit, disabled, loading }) => {
|
||||||
const { language } = useLanguage();
|
const { language } = useLanguage();
|
||||||
const t = translations[language];
|
const t = translations[language];
|
||||||
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
const prevInputRef = useRef(input);
|
||||||
|
|
||||||
|
// Focus input when it gets cleared after submission
|
||||||
|
useEffect(() => {
|
||||||
|
if (prevInputRef.current && !input && !loading) {
|
||||||
|
inputRef.current?.focus();
|
||||||
|
}
|
||||||
|
prevInputRef.current = input;
|
||||||
|
}, [input, loading]);
|
||||||
|
|
||||||
|
const handleSubmit = (e: React.FormEvent) => {
|
||||||
|
onSubmit(e);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form onSubmit={onSubmit} className="flex gap-2">
|
<form onSubmit={handleSubmit} className="flex gap-2">
|
||||||
<input
|
<input
|
||||||
|
ref={inputRef}
|
||||||
type="text"
|
type="text"
|
||||||
value={input}
|
value={input}
|
||||||
onChange={onInputChange}
|
onChange={onInputChange}
|
||||||
|
|||||||
Reference in new issue
Block a user