Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

2.4 KB
Raw Blame
import { HelpCircleIcon } from 'lucide-react'
import Link from 'next/link'
import type { Control, FieldPath, FieldValues } from 'react-hook-form'
import { cn, FormControl, FormField, Input, InputVariants, Textarea } from 'ui'
import { Input as PasswordInput } from 'ui-patterns/DataInputs/Input'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'

import type { ServerOption } from './Wrappers.types'

interface InputFieldProps<TFieldValues extends FieldValues = FieldValues> {
  option: ServerOption
  control: Control<TFieldValues>
  loading?: boolean
  placeholder?: string
  layout?: 'vertical' | 'horizontal'
}

export const InputField = <
  TFieldValues extends FieldValues = FieldValues,
  TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
>({
  control,
  option,
  loading = false,
  layout = 'horizontal',
  placeholder,
}: InputFieldProps<TFieldValues>) => {
  return (
    <FormField
      control={control}
      name={option.name as TName}
      defaultValue={(option.defaultValue ?? '') as any}
      render={({ field }) => (
        <FormItemLayout
          layout={layout}
          label={
            <div className="flex items-center space-x-2">
              <p>{option.label}</p>
              {option.urlHelper !== undefined && (
                <Link href={option.urlHelper} target="_blank" rel="noreferrer">
                  <span className="sr-only">Documentation</span>
                  <HelpCircleIcon
                    strokeWidth={2}
                    size={14}
                    className="text-foreground-light hover:text-foreground cursor-pointer transition"
                  />
                </Link>
              )}
            </div>
          }
          labelOptional={!option.required ? 'Optional' : undefined}
          description={option.description}
        >
          <FormControl>
            {loading ? (
              <span className={cn(InputVariants({ size: 'small' }))}>
                Fetching value from Vault...
              </span>
            ) : option.isTextArea ? (
              <Textarea {...field} rows={6} className="input-mono resize-none" />
            ) : option.secureEntry ? (
              <PasswordInput copy reveal {...field} />
            ) : (
              <Input {...field} placeholder={placeholder} />
            )}
          </FormControl>
        </FormItemLayout>
      )}
    />
  )
}