react-antd-admin-panel
Version:
Modern TypeScript-first React admin panel builder with Ant Design 6
112 lines • 3.56 kB
TypeScript
import { FieldValues, UseFormReturn, FieldErrors, Path, PathValue } from 'react-hook-form';
/**
* Validation function type
*/
export type ValidateFunction<T extends FieldValues> = (values: T) => Record<string, string> | Promise<Record<string, string>>;
/**
* Options for the useForm hook
*/
export interface UseFormOptions<T extends FieldValues> {
/** Initial form values */
initialValues: T;
/** Submit handler - receives form values */
onSubmit?: (values: T) => void | Promise<void>;
/** Post configuration for automatic submission */
post?: {
url: string;
method?: 'POST' | 'PUT' | 'PATCH';
headers?: Record<string, string>;
onSuccess?: (response: any) => void;
onError?: (error: Error) => void;
};
/** Custom validation function */
validate?: ValidateFunction<T>;
/** Validation mode */
mode?: 'onSubmit' | 'onBlur' | 'onChange' | 'onTouched' | 'all';
}
/**
* Return type for the useForm hook
*/
export interface UseFormResult<T extends FieldValues> {
/** Current form values */
values: T;
/** Form errors by field */
errors: FieldErrors<T>;
/** Set a single field value */
setValue: <K extends Path<T>>(name: K, value: PathValue<T, K>) => void;
/** Set multiple field values */
setValues: (values: Partial<T>) => void;
/** Submit the form */
submit: () => Promise<void>;
/** Whether form is submitting */
submitting: boolean;
/** Reset form to initial values */
reset: (values?: T) => void;
/** Whether form has been modified */
isDirty: boolean;
/** Whether form is valid */
isValid: boolean;
/** Register a field (for uncontrolled inputs) */
register: UseFormReturn<T>['register'];
/** Get field state */
getFieldState: UseFormReturn<T>['getFieldState'];
/** Trigger validation */
trigger: UseFormReturn<T>['trigger'];
/** Clear all errors */
clearErrors: UseFormReturn<T>['clearErrors'];
/** Set an error on a field */
setError: UseFormReturn<T>['setError'];
/** Watch form values */
watch: UseFormReturn<T>['watch'];
}
/**
* useForm - React hook for form state management
*
* Wraps react-hook-form with a simplified API and optional Post integration.
*
* @template T - Form values type
* @param options - Hook options
* @returns Hook result with form state and control functions
*
* @example
* ```tsx
* // Basic usage
* const { values, setValue, submit, submitting } = useForm({
* initialValues: { name: '', email: '' },
* onSubmit: async (values) => {
* await saveUser(values);
* },
* });
*
* // With Post integration
* const { values, submit, submitting } = useForm({
* initialValues: { name: '', email: '' },
* post: {
* url: '/api/users',
* onSuccess: (user) => navigate(`/users/${user.id}`),
* },
* });
*
* // With validation
* const { values, errors, submit } = useForm({
* initialValues: { email: '' },
* validate: (values) => {
* const errors: Record<string, string> = {};
* if (!values.email.includes('@')) {
* errors.email = 'Invalid email';
* }
* return errors;
* },
* });
*
* // In JSX
* <input
* value={values.name}
* onChange={(e) => setValue('name', e.target.value)}
* />
* <span>{errors.name?.message}</span>
* <button onClick={submit} disabled={submitting}>Save</button>
* ```
*/
export declare function useForm<T extends FieldValues>(options: UseFormOptions<T>): UseFormResult<T>;
//# sourceMappingURL=useForm.d.ts.map