InputField.tsx71 lines · main
1import { InputVariants } from '@ui/components/shadcn/ui/input'
2import { HelpCircle } from 'lucide-react'
3import Link from 'next/link'
4import type { Control, FieldPath, FieldValues } from 'react-hook-form'
5import { cn, FormControl, FormField, Input, Textarea } from 'ui'
6import { Input as PasswordInput } from 'ui-patterns/DataInputs/Input'
7import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
8
9import type { ServerOption } from './Wrappers.types'
10
11interface InputFieldProps<TFieldValues extends FieldValues = FieldValues> {
12 option: ServerOption
13 control: Control<TFieldValues>
14 loading?: boolean
15}
16
17const InputField = <
18 TFieldValues extends FieldValues = FieldValues,
19 TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
20>({
21 control,
22 option,
23 loading = false,
24}: InputFieldProps<TFieldValues>) => {
25 return (
26 <FormField
27 control={control}
28 name={option.name as TName}
29 defaultValue={(option.defaultValue ?? '') as any}
30 render={({ field }) => (
31 <FormItemLayout
32 name={option.name}
33 layout="vertical"
34 label={
35 <div className="flex items-center space-x-2">
36 <p>{option.label}</p>
37 {option.urlHelper !== undefined && (
38 <Link href={option.urlHelper} target="_blank" rel="noreferrer">
39 <span className="sr-only">Documentation</span>
40 <HelpCircle
41 strokeWidth={2}
42 size={14}
43 className="text-foreground-light hover:text-foreground cursor-pointer transition"
44 />
45 </Link>
46 )}
47 </div>
48 }
49 labelOptional={!option.required ? 'Optional' : undefined}
50 description={option.description}
51 >
52 <FormControl>
53 {loading ? (
54 <span className={cn(InputVariants({ size: 'small' }))}>
55 Fetching value from Vault...
56 </span>
57 ) : option.isTextArea ? (
58 <Textarea {...field} id={option.name} rows={6} className="input-mono resize-none" />
59 ) : option.secureEntry ? (
60 <PasswordInput copy reveal {...field} id={option.name} />
61 ) : (
62 <Input {...field} id={option.name} />
63 )}
64 </FormControl>
65 </FormItemLayout>
66 )}
67 />
68 )
69}
70
71export default InputField