forked from git.gladyson/openmonetis
Substitui non-null assertions (!) por type assertions ou optional chaining com guards. Troca any por unknown/tipos explícitos. - drizzle.config: DATABASE_URL! → as string - use-form-state: Record<string, any> → Record<string, unknown> - actions: catch (e: any) → catch (e), model tipado explicitamente - pagadores/data: row: any → Record<string, unknown> - note-dialog: result tipado explicitamente - bulk-import: payload as any removido - Map.get()! → optional chaining + guards em relatórios e dashboard Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
53 lines
1.1 KiB
TypeScript
53 lines
1.1 KiB
TypeScript
import { useState } from "react";
|
|
|
|
/**
|
|
* Hook for managing form state with type-safe field updates
|
|
*
|
|
* @param initialValues - Initial form values
|
|
* @returns Object with formState, updateField, resetForm, setFormState
|
|
*
|
|
* @example
|
|
* ```tsx
|
|
* const { formState, updateField, resetForm } = useFormState({
|
|
* name: '',
|
|
* email: ''
|
|
* });
|
|
*
|
|
* updateField('name', 'John');
|
|
* ```
|
|
*/
|
|
export function useFormState<T extends Record<string, unknown>>(
|
|
initialValues: T,
|
|
) {
|
|
const [formState, setFormState] = useState<T>(initialValues);
|
|
|
|
/**
|
|
* Updates a single field in the form state
|
|
*/
|
|
const updateField = <K extends keyof T>(field: K, value: T[K]) => {
|
|
setFormState((prev) => ({ ...prev, [field]: value }));
|
|
};
|
|
|
|
/**
|
|
* Resets form to initial values
|
|
*/
|
|
const resetForm = () => {
|
|
setFormState(initialValues);
|
|
};
|
|
|
|
/**
|
|
* Updates multiple fields at once
|
|
*/
|
|
const updateFields = (updates: Partial<T>) => {
|
|
setFormState((prev) => ({ ...prev, ...updates }));
|
|
};
|
|
|
|
return {
|
|
formState,
|
|
updateField,
|
|
updateFields,
|
|
resetForm,
|
|
setFormState,
|
|
};
|
|
}
|