@foundrykit/components 1.0.19 → 1.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,132 @@
1
+ 'use client';
2
+
3
+ import * as React from 'react';
4
+ import * as LabelPrimitive from '@radix-ui/react-label';
5
+ import { Slot } from '@radix-ui/react-slot';
6
+ import {
7
+ Controller,
8
+ FormProvider,
9
+ type ControllerProps,
10
+ type FieldPath,
11
+ type FieldValues,
12
+ } from 'react-hook-form';
13
+
14
+ import { cn } from '@foundrykit/utils';
15
+ import { Label } from '@foundrykit/primitives';
16
+
17
+ const Form = FormProvider;
18
+
19
+ const FormField = <
20
+ TFieldValues extends FieldValues = FieldValues,
21
+ TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
22
+ >({
23
+ ...props
24
+ }: ControllerProps<TFieldValues, TName>) => {
25
+ return (
26
+ <FormFieldContext.Provider value={{ name: props.name }}>
27
+ <Controller {...props} />
28
+ </FormFieldContext.Provider>
29
+ );
30
+ };
31
+
32
+ import {
33
+ useFormField,
34
+ FormFieldContext,
35
+ FormItemContext,
36
+ } from '@foundrykit/hooks';
37
+
38
+ // FormItemContext is now provided by hooks package
39
+
40
+ function FormItem({ className, ...props }: React.ComponentProps<'div'>) {
41
+ const id = React.useId();
42
+
43
+ return (
44
+ <FormItemContext.Provider value={{ id }}>
45
+ <div
46
+ data-slot='form-item'
47
+ className={cn('grid gap-2', className)}
48
+ {...props}
49
+ />
50
+ </FormItemContext.Provider>
51
+ );
52
+ }
53
+
54
+ function FormLabel({
55
+ className,
56
+ ...props
57
+ }: React.ComponentProps<typeof LabelPrimitive.Root>) {
58
+ const { error, formItemId } = useFormField();
59
+
60
+ return (
61
+ <Label
62
+ data-slot='form-label'
63
+ data-error={!!error}
64
+ className={cn('data-[error=true]:text-destructive', className)}
65
+ htmlFor={formItemId}
66
+ {...props}
67
+ />
68
+ );
69
+ }
70
+
71
+ function FormControl({ ...props }: React.ComponentProps<typeof Slot>) {
72
+ const { error, formItemId, formDescriptionId, formMessageId } =
73
+ useFormField();
74
+
75
+ return (
76
+ <Slot
77
+ data-slot='form-control'
78
+ id={formItemId}
79
+ aria-describedby={
80
+ !error
81
+ ? `${formDescriptionId}`
82
+ : `${formDescriptionId} ${formMessageId}`
83
+ }
84
+ aria-invalid={!!error}
85
+ {...props}
86
+ />
87
+ );
88
+ }
89
+
90
+ function FormDescription({ className, ...props }: React.ComponentProps<'p'>) {
91
+ const { formDescriptionId } = useFormField();
92
+
93
+ return (
94
+ <p
95
+ data-slot='form-description'
96
+ id={formDescriptionId}
97
+ className={cn('text-muted-foreground text-sm', className)}
98
+ {...props}
99
+ />
100
+ );
101
+ }
102
+
103
+ function FormMessage({ className, ...props }: React.ComponentProps<'p'>) {
104
+ const { error, formMessageId } = useFormField();
105
+ const body = error ? String(error?.message ?? '') : props.children;
106
+
107
+ if (!body) {
108
+ return null;
109
+ }
110
+
111
+ return (
112
+ <p
113
+ data-slot='form-message'
114
+ id={formMessageId}
115
+ className={cn('text-destructive text-sm', className)}
116
+ {...props}
117
+ >
118
+ {body}
119
+ </p>
120
+ );
121
+ }
122
+
123
+ export {
124
+ useFormField,
125
+ Form,
126
+ FormItem,
127
+ FormLabel,
128
+ FormControl,
129
+ FormDescription,
130
+ FormMessage,
131
+ FormField,
132
+ };
package/src/index.ts CHANGED
@@ -32,6 +32,16 @@ export {
32
32
  SearchItem,
33
33
  AdvancedSearchBarWrapper,
34
34
  } from './components/advanced-search-bar/index.js';
35
+ export {
36
+ useFormField,
37
+ Form,
38
+ FormItem,
39
+ FormLabel,
40
+ FormControl,
41
+ FormDescription,
42
+ FormMessage,
43
+ FormField,
44
+ } from './components/form/index.js';
35
45
 
36
46
  // Export types from types package
37
47
  export type {