'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('task'); const [addingCheckbox, setAddingCheckbox] = useState(false); const inputRef = useRef(null); const handleAddCheckbox = async () => { if (!newCheckboxText.trim()) return; setAddingCheckbox(true); try { await onAdd(newCheckboxText.trim(), selectedType); // Pas de taskId lors de l'ajout setNewCheckboxText(''); // Garder le type sélectionné pour enchaîner les créations du même type // setSelectedType('task'); // <- Supprimé pour garder la sélection // Garder le focus sur l'input pour enchainer les entrées setTimeout(() => { inputRef.current?.focus(); }, 100); } finally { setAddingCheckbox(false); } }; 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={addingCheckbox || disabled} className="flex-1 min-w-[300px]" />
); }