-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-field.tsx
More file actions
122 lines (116 loc) · 3.63 KB
/
Copy pathtext-field.tsx
File metadata and controls
122 lines (116 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import type * as React from 'react';
import type { Control, FieldPath, FieldValues } from 'react-hook-form';
import {
type FieldComponents,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from './form';
import { type InputProps, TextInput } from './text-input';
import { cn } from './utils';
export const FieldPrefix = ({
children,
className,
}: {
children: React.ReactNode;
className?: string;
}) => {
return (
<div
className={cn(
'flex h-full text-base items-center pl-3 pr-0 text-gray-500 group-focus-within:text-gray-700 transition-colors duration-200 border-y border-l border-input rounded-l-md bg-background',
className,
)}
>
<span className="whitespace-nowrap">{children}</span>
</div>
);
};
export const FieldSuffix = ({
children,
className,
}: {
children: React.ReactNode;
className?: string;
}) => {
return (
<div
className={cn(
'flex h-full text-base items-center pr-3 pl-0 text-gray-500 group-focus-within:text-gray-700 transition-colors duration-200 border-y border-r border-input rounded-r-md bg-background',
className,
)}
>
<span className="whitespace-nowrap">{children}</span>
</div>
);
};
// Create a specific interface for the input props that includes className explicitly
export interface TextInputProps extends Omit<InputProps, 'prefix' | 'suffix'> {
control?: Control<FieldValues>;
name: FieldPath<FieldValues>;
label?: string;
description?: string;
components?: Partial<FieldComponents> & {
Input?: React.ComponentType<InputProps & React.RefAttributes<HTMLInputElement>>;
};
prefix?: React.ReactNode;
suffix?: React.ReactNode;
className?: string;
}
export const TextField = function TextField({
control,
name,
label,
description,
className,
components,
prefix,
suffix,
ref,
...props
}: TextInputProps & { ref?: React.Ref<HTMLInputElement> }) {
// Use the custom Input component if provided, otherwise use the default TextInput
const InputComponent = components?.Input || TextInput;
return (
<FormField
control={control}
name={name}
render={({ field, fieldState }) => {
return (
<FormItem className={className}>
{label && <FormLabel Component={components?.FormLabel}>{label}</FormLabel>}
<div
className={cn('flex group transition-all duration-200 rounded-md', {
'field__input--with-prefix': prefix,
'field__input--with-suffix': suffix,
'focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2 focus-within:ring-offset-background': true,
})}
>
{prefix && <FieldPrefix>{prefix}</FieldPrefix>}
<FormControl Component={components?.FormControl}>
<InputComponent
{...field}
{...props}
ref={ref}
className={cn('focus-visible:ring-0 focus-visible:ring-offset-0 border-input', {
'rounded-l-none border-l-0': prefix,
'rounded-r-none border-r-0': suffix,
})}
/>
</FormControl>
{suffix && <FieldSuffix>{suffix}</FieldSuffix>}
</div>
{description && <FormDescription Component={components?.FormDescription}>{description}</FormDescription>}
{fieldState.error && (
<FormMessage Component={components?.FormMessage}>{fieldState.error.message}</FormMessage>
)}
</FormItem>
);
}}
/>
);
};
TextField.displayName = 'TextField';