'use client'; import { useState, useRef, useEffect } from 'react'; interface Props { value: string; onSave: (newValue: string) => void; className?: string; as?: 'input' | 'textarea'; placeholder?: string; multiline?: boolean; } export default function InlineEditField({ value, onSave, className = '', as = 'input', placeholder = '', multiline = false, }: Props) { const [editing, setEditing] = useState(false); const [editValue, setEditValue] = useState(value); const inputRef = useRef(null); useEffect(() => { setEditValue(value); }, [value]); useEffect(() => { if (editing && inputRef.current) { inputRef.current.focus(); // Select all text if ('setSelectionRange' in inputRef.current) { inputRef.current.setSelectionRange(0, inputRef.current.value.length); } } }, [editing]); function handleBlur() { setEditing(false); const trimmed = editValue.trim(); if (trimmed !== value && trimmed !== '') { onSave(trimmed); } else { setEditValue(value); } } function handleKeyDown(e: React.KeyboardEvent) { if (e.key === 'Enter' && !multiline) { e.preventDefault(); (e.target as HTMLElement).blur(); } if (e.key === 'Escape') { setEditValue(value); setEditing(false); } } if (editing) { if (as === 'textarea' || multiline) { return (