Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 2x 2x 2x 2x 2x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x | /**
* @file useEditSupplierFormState.ts
* @module dialogs/EditSupplierDialog/useEditSupplierFormState
*
* @summary
* Hook for managing supplier edit form state.
* Handles form initialization, validation, and lifecycle.
*
* @enterprise
* - React Hook Form with Zod validation
* - Form reset and setValue management
* - Supplier data pre-fill on selection
* - Type-safe form methods
*/
import * as React from 'react';
import { useForm, type UseFormReturn } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import type { Resolver } from 'react-hook-form';
import { editSupplierSchema, type EditSupplierForm } from '../../../../api/suppliers';
import type { SupplierRow } from '../../../../api/suppliers/types';
/**
* Hook return type for form state.
*
* @interface UseEditSupplierFormStateReturn
*/
export interface UseEditSupplierFormStateReturn {
/** Register function for form fields */
register: UseFormReturn<EditSupplierForm>['register'];
/** Control object for controlled components */
control: UseFormReturn<EditSupplierForm>['control'];
/** Form state (errors, isDirty, isSubmitting, etc) */
formState: UseFormReturn<EditSupplierForm>['formState'];
/** Submit handler wrapper */
handleSubmit: UseFormReturn<EditSupplierForm>['handleSubmit'];
/** Set individual form field values */
setValue: UseFormReturn<EditSupplierForm>['setValue'];
/** Reset form to default values */
reset: () => void;
/** Populate form with supplier data */
populateWithSupplier: (supplier: SupplierRow) => void;
}
/**
* Hook for managing supplier edit form state.
*
* Manages:
* - Form initialization with validation
* - Form field registration
* - Form state tracking
* - Supplier data pre-fill
* - Form reset
*
* @returns Form methods and state
*
* @example
* ```ts
* const { register, control, handleSubmit, formState } = useEditSupplierFormState();
* ```
*/
export const useEditSupplierFormState = (): UseEditSupplierFormStateReturn => {
const {
register,
control,
handleSubmit,
formState,
reset,
setValue,
} = useForm<EditSupplierForm>({
resolver: zodResolver(editSupplierSchema) as Resolver<EditSupplierForm>,
defaultValues: {
supplierId: '',
contactName: '',
phone: '',
email: '',
},
});
/**
* Populate form with supplier data.
* Pre-fills form fields when supplier is selected.
*/
const populateWithSupplier = React.useCallback(
(supplier: SupplierRow) => {
setValue('supplierId', supplier.id);
setValue('contactName', supplier.contactName || '');
setValue('phone', supplier.phone || '');
setValue('email', supplier.email || '');
},
[setValue]
);
return {
register,
control,
handleSubmit,
formState,
setValue,
reset,
populateWithSupplier,
};
};
|