react-server675fbba4
react-serverfilesexamplespokemonsrccomponentsFormInput.tsx
examples/pokemon/src/components/FormInput.tsxtsx669 B9d6d65a9
"use client";

import { useEffect, useRef } from "react";

export default function FormInput(
  props: React.InputHTMLAttributes<HTMLInputElement>
) {
  const timeoutId = useRef<number>();
  const formRef = useRef<HTMLFormElement | null>(null);
  const ref = useRef<HTMLInputElement | null>(null);

  useEffect(() => {
    formRef.current = ref?.current?.closest("form") ?? null;
  }, [ref]);

  const handleChange = () => {
    if (timeoutId.current) {
      clearTimeout(timeoutId.current);
    }

    timeoutId.current = setTimeout(() => {
      formRef.current?.requestSubmit();
    }, 200);
  };

  return <input {...props} ref={ref} onChange={handleChange} />;
}