refactoring majeur (code dupliqué, mort, ...)
- Économie : ~1240 lignes de code dupliqué - Réduction : ~60% du code modal - Amélioration : Cohérence et maintenabilité
This commit is contained in:
32
src/hooks/useFormState.ts
Normal file
32
src/hooks/useFormState.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
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
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user