feat: attempt to fix the crud with auth
This commit is contained in:
29
hooks/useFormField.tsx
Normal file
29
hooks/useFormField.tsx
Normal file
@@ -0,0 +1,29 @@
|
||||
import { FormFieldContext } from '@contexts/FormField/FormFieldContext';
|
||||
import { FormItemContext } from '@contexts/FormItem/FormItemContext';
|
||||
import * as React from 'react';
|
||||
import { useFormContext } from 'react-hook-form';
|
||||
|
||||
const useFormField = () => {
|
||||
const fieldContext = React.useContext(FormFieldContext);
|
||||
const itemContext = React.useContext(FormItemContext);
|
||||
const { getFieldState, formState } = useFormContext();
|
||||
|
||||
const fieldState = getFieldState(fieldContext.name, formState);
|
||||
|
||||
if (!fieldContext) {
|
||||
throw new Error('useFormField should be used within <FormField>');
|
||||
}
|
||||
|
||||
const { id } = itemContext;
|
||||
|
||||
return {
|
||||
id,
|
||||
name: fieldContext.name,
|
||||
formItemId: `${id}-form-item`,
|
||||
formDescriptionId: `${id}-form-item-description`,
|
||||
formMessageId: `${id}-form-item-message`,
|
||||
...fieldState
|
||||
};
|
||||
};
|
||||
|
||||
export { useFormField };
|
||||
Reference in New Issue
Block a user