Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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