import React, { useMemo, useState } from 'react'; import { IonButton, IonButtons, IonContent, IonHeader, IonInput, IonItem, IonLabel, IonModal, IonTextarea, IonTitle, IonToolbar, } from '@ionic/react'; interface HabitFormModalProps { isOpen: boolean; onDismiss: () => void; onSubmit: (values: { name: string; targetDescription: string; color: string; }) => Promise; } const COLORS = [ '#7c3aed', '#0f766e', '#ea580c', '#dc2626', '#2563eb', '#059669', ]; const HabitFormModal: React.FC = ({ isOpen, onDismiss, onSubmit, }) => { const [name, setName] = useState(''); const [targetDescription, setTargetDescription] = useState(''); const [color, setColor] = useState(COLORS[0]); const [saving, setSaving] = useState(false); const [error, setError] = useState(''); const canSubmit = useMemo( () => name.trim().length > 0 && name.trim().length <= 80, [name] ); const handleSubmit = async () => { setError(''); setSaving(true); try { await onSubmit({ name: name.trim(), targetDescription: targetDescription.trim(), color, }); setName(''); setTargetDescription(''); setColor(COLORS[0]); onDismiss(); } catch (err) { setError(err instanceof Error ? err.message : 'Unable to save habit.'); } finally { setSaving(false); } }; return ( New Habit Close
setName(e.detail.value ?? '')} /> setTargetDescription(e.detail.value ?? '')} />
Color
{COLORS.map((swatch) => (
{error ?

{error}

: null} {saving ? 'Saving…' : 'Create Habit'}
); }; export default HabitFormModal;