diff --git a/package.json b/package.json index 73ee06a..fc51894 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "create-turbo-eth", - "version": "0.2.1", + "version": "0.2.2", "description": "Create web3 apps in turbo mode.", "author": "Vitor @marthendalnunes", "license": "MIT", diff --git a/template/base/components/ui/form.tsx b/template/base/components/ui/form.tsx new file mode 100644 index 0000000..1d3b8bc --- /dev/null +++ b/template/base/components/ui/form.tsx @@ -0,0 +1,115 @@ +import * as React from 'react' + +import * as LabelPrimitive from '@radix-ui/react-label' +import { Slot } from '@radix-ui/react-slot' +import { Controller, ControllerProps, FieldPath, FieldValues, FormProvider, useFormContext } from 'react-hook-form' + +import { Label } from '@/components/ui/label' +import { cn } from '@/lib/utils' + +const Form = FormProvider + +type FormFieldContextValue = FieldPath> = { + name: TName +} + +const FormFieldContext = React.createContext({} as FormFieldContextValue) + +const FormField = = FieldPath>({ + ...props +}: ControllerProps) => { + return ( + + + + ) +} + +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 ') + } + + const { id } = itemContext + + return { + id, + name: fieldContext.name, + formItemId: `${id}-form-item`, + formDescriptionId: `${id}-form-item-description`, + formMessageId: `${id}-form-item-message`, + ...fieldState, + } +} + +type FormItemContextValue = { + id: string +} + +const FormItemContext = React.createContext({} as FormItemContextValue) + +const FormItem = React.forwardRef>(({ className, ...props }, ref) => { + const id = React.useId() + + return ( + +
+ + ) +}) +FormItem.displayName = 'FormItem' + +const FormLabel = React.forwardRef, React.ComponentPropsWithoutRef>( + ({ className, ...props }, ref) => { + const { error, formItemId } = useFormField() + + return