import React, { useEffect, useRef, useState } from 'react' import type { FieldProps } from './field-types' const TextField: React.FC> = ({ value, onSave, readOnly }) => { const [isEditing, setIsEditing] = useState(false) const [localValue, setLocalValue] = useState(value || '') const inputRef = useRef(null) // Sync with prop updates useEffect(() => { setLocalValue(value || '') }, [value]) useEffect(() => { if (isEditing && inputRef.current) { inputRef.current.focus() } }, [isEditing]) const handleStartEdit = () => { if (!readOnly) { setIsEditing(true) } } const handleBlur = () => { setIsEditing(false) onSave(localValue) } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { inputRef.current?.blur() } } if (isEditing) { return ( setLocalValue(e.target.value)} onBlur={handleBlur} onKeyDown={handleKeyDown} className="w-full px-2 py-1 border border-ring rounded bg-background text-sm outline-none shadow-sm" /> ) } return (
{value || Empty}
) } export default TextField