import { useState, useEffect, useRef } from 'react' import { UserPlus, X } from 'lucide-react' import './InlinePersonForm.css' import PzBusy from './PzBusy.jsx' import { t } from '../i18n/index.js' /** * Mini-formulaire inline pour créer une personne après drop * Affiche le type de lien créé et demande les infos de base * * @param {Object} props * @param {boolean} props.visible - Afficher le formulaire * @param {Object} props.position - Position d'affichage {x, y} * @param {string} props.linkType - Type de lien ('child', 'spouse', 'sibling', null) * @param {Object} props.linkedPerson - Personne liée (si linkType) * @param {Function} props.onSubmit - Callback soumission (formData) * @param {Function} props.onCancel - Callback annulation * @param {boolean} props.loading - État chargement */ function InlinePersonForm({ visible, position, linkType, linkedPerson, onSubmit, onCancel, loading = false, }) { const [formData, setFormData] = useState({ firstName: '', lastName: '', birthDate: '', birthPlace: '', }) const firstInputRef = useRef(null) // Reset form au show useEffect(() => { if (visible) { setFormData({ firstName: '', lastName: '', birthDate: '', birthPlace: '', }) // Focus premier input setTimeout(() => firstInputRef.current?.focus(), 100) } }, [visible]) // Handle ESC useEffect(() => { if (!visible) return const handleEscape = (e) => { if (e.key === 'Escape') onCancel?.() } document.addEventListener('keydown', handleEscape) return () => document.removeEventListener('keydown', handleEscape) }, [visible, onCancel]) if (!visible) return null const handleChange = (field, value) => { setFormData((prev) => ({ ...prev, [field]: value })) } const handleSubmit = (e) => { e.preventDefault() if (!formData.firstName.trim()) return onSubmit?.({ ...formData, linkType, linkedPersonId: linkedPerson?.id, }) } const isValid = formData.firstName.trim().length > 0 // Labels type lien const linkLabels = { child: (name) => t('Enfant de {name}', { name }), spouse: (name) => t('Conjoint de {name}', { name }), sibling: (name) => t('Frère·Sœur de {name}', { name }), } const linkTitle = { child: t('Nouvel enfant'), spouse: t('Nouveau conjoint'), sibling: t('Nouveau frère/sœur'), } const title = linkType ? linkTitle[linkType] : t('Nouvelle personne') return ( <> {/* Overlay */}
{/* Form */}
{/* Header */}

{title}

{/* Badge lien */} {linkType && linkedPerson && (
{linkLabels[linkType]?.([linkedPerson.person?.firstName, linkedPerson.person?.lastName].filter(Boolean).join(' '))}
)} {/* Fields */}
handleChange('firstName', e.target.value)} disabled={loading} />
handleChange('lastName', e.target.value)} disabled={loading} />
handleChange('birthDate', e.target.value)} disabled={loading} />
handleChange('birthPlace', e.target.value)} disabled={loading} />
{/* Actions */}
) } export default InlinePersonForm