- Économie : ~1240 lignes de code dupliqué - Réduction : ~60% du code modal - Amélioration : Cohérence et maintenabilité
33 lines
771 B
TypeScript
33 lines
771 B
TypeScript
import { useState } from 'react';
|
|
|
|
export function useFormState<T>(initialData: T) {
|
|
const [formData, setFormData] = useState<T>(initialData);
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState('');
|
|
|
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
|
|
setFormData(prev => ({ ...prev, [e.target.name]: e.target.value }));
|
|
};
|
|
|
|
const resetForm = () => {
|
|
setFormData(initialData);
|
|
setError('');
|
|
};
|
|
|
|
const setFieldValue = (field: keyof T, value: any) => {
|
|
setFormData(prev => ({ ...prev, [field]: value }));
|
|
};
|
|
|
|
return {
|
|
formData,
|
|
setFormData,
|
|
loading,
|
|
setLoading,
|
|
error,
|
|
setError,
|
|
handleChange,
|
|
resetForm,
|
|
setFieldValue
|
|
};
|
|
}
|