import { useEffect, useState } from "react"; // react-hook-form import { Controller, useForm } from "react-hook-form"; // types import { Props } from "./types"; export const CustomUrlAttribute: React.FC = ({ attributeDetails, onChange, value, }) => { const [isEditing, setIsEditing] = useState(false); const { control, handleSubmit, reset, setFocus } = useForm({ defaultValues: { url: "" } }); const handleFormSubmit = (data: { url: string }) => { setIsEditing(false); onChange(data.url); }; useEffect(() => { if (isEditing) { setFocus("url"); } }, [isEditing, setFocus]); useEffect(() => { reset({ url: value?.toString() }); }, [reset, value]); useEffect(() => { const handleEscKeyPress = (e: KeyboardEvent) => { if (e.key === "Escape") setIsEditing(false); }; document.addEventListener("keydown", handleEscKeyPress); return () => { document.removeEventListener("keydown", handleEscKeyPress); }; }, []); return (
{!isEditing && (
setIsEditing(true)}> {value ?? "Empty"}
)} {isEditing && (
( )} /> )}
); };