form.tsx 3.6 KB

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