import React, { useState, useEffect, useRef } from 'react'; import { CheckCircle2, X } from 'lucide-react'; interface EditableFieldProps { value: string | number | boolean; onChange: (val: string) => void; isEditing: boolean; className?: string; placeholder?: string; type?: 'text' | 'number' | 'date' | 'checkbox'; } export const EditableField: React.FC = ({ value, onChange, isEditing, className = "", placeholder, type = 'text' }) => { const [localValue, setLocalValue] = useState(value); const inputRef = useRef(null); const wasFocused = useRef(false); // Синхронизируем локальное значение с внешним useEffect(() => { setLocalValue(value); }, [value]); // Восстанавливаем фокус после обновления (setSelectionRange не поддерживается для type="number" и "date") useEffect(() => { if (wasFocused.current && inputRef.current && isEditing) { const input = inputRef.current; input.focus(); const inputType = (input.type || 'text').toLowerCase(); if (inputType === 'text' || inputType === 'search' || inputType === 'url' || inputType === 'tel' || inputType === 'password') { const cursorPosition = input.selectionStart ?? 0; input.setSelectionRange(cursorPosition, cursorPosition); } wasFocused.current = false; } }); if (type === 'checkbox') { const boolValue = Boolean(value); if (!isEditing) return boolValue ? : ; return ( onChange(String(e.target.checked))} className="w-5 h-5 accent-primary-600 cursor-pointer" /> ); } if (!isEditing) { return {value}; } const handleChange = (e: React.ChangeEvent) => { const newValue = e.target.value; setLocalValue(newValue); wasFocused.current = true; onChange(newValue); }; const handleFocus = () => { wasFocused.current = true; }; return ( e.stopPropagation()} /> ); };