()
| 39 | }; |
| 40 | |
| 41 | const useFormField = () => { |
| 42 | const fieldContext = React.useContext(FormFieldContext); |
| 43 | const itemContext = React.useContext(FormItemContext); |
| 44 | const { getFieldState } = useFormContext(); |
| 45 | const formState = useFormState({ name: fieldContext.name }); |
| 46 | const fieldState = getFieldState(fieldContext.name, formState); |
| 47 | |
| 48 | if (!fieldContext) { |
| 49 | throw new Error("useFormField should be used within <FormField>"); |
| 50 | } |
| 51 | |
| 52 | const { id } = itemContext; |
| 53 | |
| 54 | return { |
| 55 | id, |
| 56 | name: fieldContext.name, |
| 57 | formItemId: `${id}-form-item`, |
| 58 | formDescriptionId: `${id}-form-item-description`, |
| 59 | formMessageId: `${id}-form-item-message`, |
| 60 | ...fieldState, |
| 61 | }; |
| 62 | }; |
| 63 | |
| 64 | type FormItemContextValue = { |
| 65 | id: string; |
no outgoing calls
no test coverage detected