'use client'; import { useState, useRef } from 'react'; import { DailyCheckboxType } from '@/lib/types'; import { Button } from '@/components/ui/Button'; import { Input } from '@/components/ui/Input'; interface DailyAddFormProps { onAdd: (text: string, type: DailyCheckboxType) => Promise; disabled?: boolean; placeholder?: string; } export function DailyAddForm({ onAdd, disabled = false, placeholder = "Ajouter une tâche..." }: DailyAddFormProps) { const [newCheckboxText, setNewCheckboxText] = useState(''); const [selectedType, setSelectedType] = useState('meeting'); const inputRef = useRef(null); const handleAddCheckbox = () => { if (!newCheckboxText.trim()) return; const text = newCheckboxText.trim(); // Vider et refocus IMMÉDIATEMENT pour l'UX optimiste setNewCheckboxText(''); inputRef.current?.focus(); // Lancer l'ajout en arrière-plan (fire and forget) onAdd(text, selectedType).catch(error => { console.error('Erreur lors de l\'ajout:', error); // En cas d'erreur, on pourrait restaurer le texte // setNewCheckboxText(text); }); }; const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { e.preventDefault(); handleAddCheckbox(); } }; const getPlaceholder = () => { if (placeholder !== "Ajouter une tâche...") return placeholder; return selectedType === 'meeting' ? 'Ajouter une réunion...' : 'Ajouter une tâche...'; }; return (
{/* Sélecteur de type */}
{/* Champ de saisie et options */}
setNewCheckboxText(e.target.value)} onKeyDown={handleKeyPress} disabled={disabled} className="flex-1 min-w-[300px]" />
); }