form.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import * as React from 'react'
  2. import * as LabelPrimitive from '@radix-ui/react-label'
  3. import { Slot } from '@radix-ui/react-slot'
  4. import {
  5. Controller,
  6. ControllerProps,
  7. FieldPath,
  8. FieldValues,
  9. FormProvider,
  10. useFormContext,
  11. useFormState,
  12. useForm,
  13. } from 'react-hook-form'
  14. // @ts-ignore
  15. import { cn, useId } from '@/lib/utils'
  16. import { Label } from './label'
  17. const Form = FormProvider
  18. type FormFieldContextValue<
  19. TFieldValues extends FieldValues = FieldValues,
  20. TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
  21. > = {
  22. name: TName
  23. }
  24. const FormFieldContext = React.createContext<FormFieldContextValue>(
  25. {} as FormFieldContextValue
  26. )
  27. const FormField = <
  28. TFieldValues extends FieldValues = FieldValues,
  29. TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
  30. >({
  31. ...props
  32. }: ControllerProps<TFieldValues, TName>) => {
  33. return (
  34. <FormFieldContext.Provider value={{ name: props.name }}>
  35. <Controller {...props} />
  36. </FormFieldContext.Provider>
  37. )
  38. }
  39. const useFormField = () => {
  40. const fieldContext = React.useContext(FormFieldContext)
  41. const itemContext = React.useContext(FormItemContext)
  42. const { getFieldState, formState } = useFormContext()
  43. const fieldState = getFieldState(fieldContext.name, formState)
  44. if (!fieldContext) {
  45. throw new Error('useFormField should be used within <FormField>')
  46. }
  47. const { id } = itemContext
  48. return {
  49. id,
  50. name: fieldContext.name,
  51. formItemId: `${id}-form-item`,
  52. formDescriptionId: `${id}-form-item-description`,
  53. formMessageId: `${id}-form-item-message`,
  54. ...fieldState,
  55. }
  56. }
  57. type FormItemContextValue = {
  58. id: string
  59. }
  60. const FormItemContext = React.createContext<FormItemContextValue>(
  61. {} as FormItemContextValue
  62. )
  63. const FormItem = React.forwardRef<
  64. HTMLDivElement,
  65. React.HTMLAttributes<HTMLDivElement>
  66. >(({ className, ...props }, ref) => {
  67. const id = useId()
  68. return (
  69. <FormItemContext.Provider value={{ id }}>
  70. <div ref={ref} className={cn('ui__form-item space-y-2', className)} {...props} />
  71. </FormItemContext.Provider>
  72. )
  73. })
  74. FormItem.displayName = 'FormItem'
  75. const FormLabel = React.forwardRef<
  76. React.ElementRef<typeof LabelPrimitive.Root>,
  77. React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root>
  78. >(({ className, ...props }, ref) => {
  79. const { error, formItemId } = useFormField()
  80. return (
  81. <Label
  82. ref={ref}
  83. className={cn('ui__form-label', error && 'text-destructive', className)}
  84. htmlFor={formItemId}
  85. {...props}
  86. />
  87. )
  88. })
  89. FormLabel.displayName = 'FormLabel'
  90. const FormControl = React.forwardRef<
  91. React.ElementRef<typeof Slot>,
  92. React.ComponentPropsWithoutRef<typeof Slot>
  93. >(({ ...props }, ref) => {
  94. const { error, formItemId, formDescriptionId, formMessageId } = useFormField()
  95. return (
  96. <Slot
  97. ref={ref}
  98. id={formItemId}
  99. aria-describedby={
  100. !error
  101. ? `${formDescriptionId}`
  102. : `${formDescriptionId} ${formMessageId}`
  103. }
  104. aria-invalid={!!error}
  105. {...props}
  106. />
  107. )
  108. })
  109. FormControl.displayName = 'FormControl'
  110. const FormDescription = React.forwardRef<
  111. HTMLParagraphElement,
  112. React.HTMLAttributes<HTMLParagraphElement>
  113. >(({ className, ...props }, ref) => {
  114. const { formDescriptionId } = useFormField()
  115. return (
  116. <p
  117. ref={ref}
  118. id={formDescriptionId}
  119. className={cn('ui__form-description text-sm text-muted-foreground', className)}
  120. {...props}
  121. />
  122. )
  123. })
  124. FormDescription.displayName = 'FormDescription'
  125. const FormMessage = React.forwardRef<
  126. HTMLParagraphElement,
  127. React.HTMLAttributes<HTMLParagraphElement>
  128. >(({ className, children, ...props }, ref) => {
  129. const { error, formMessageId } = useFormField()
  130. const body = error ? String(error?.message) : children
  131. if (!body) {
  132. return null
  133. }
  134. return (
  135. <p
  136. ref={ref}
  137. id={formMessageId}
  138. className={cn('ui__form-message text-sm font-medium text-destructive', className)}
  139. {...props}
  140. >
  141. {body}
  142. </p>
  143. )
  144. })
  145. FormMessage.displayName = 'FormMessage'
  146. export {
  147. useForm,
  148. useFormContext,
  149. useFormState,
  150. useFormField,
  151. Form,
  152. FormItem,
  153. FormLabel,
  154. FormControl,
  155. FormDescription,
  156. FormMessage,
  157. FormField,
  158. }