Files
supabase/apps/studio/components/interfaces/Auth/AuthProvidersForm/FormField.tsx
T
Gildas Garcia 18e4ad227e chore: add shadcn input-group components (#44282)
## Screenshots

On a number input with units:
<img width="660" height="162" alt="image"
src="https://github.com/user-attachments/assets/1758a6d9-0836-4d41-80d1-97a03292db91"
/>

focused state:
<img width="651" height="71" alt="image"
src="https://github.com/user-attachments/assets/a92a5c39-2c7e-4c5f-9e4b-eb89810cc45c"
/>

On a textarea:
<img width="989" height="294" alt="image"
src="https://github.com/user-attachments/assets/cc696cb9-3671-4719-bdd8-daa1aea4f041"
/>
2026-03-31 09:14:56 +02:00

369 lines
12 KiB
TypeScript

import { Markdown } from 'components/interfaces/Markdown'
import { format } from 'date-fns'
import { BASE_PATH } from 'lib/constants'
import { CalendarIcon, ExternalLink } from 'lucide-react'
import { type Control } from 'react-hook-form'
import ReactMarkdown from 'react-markdown'
import {
Button,
Calendar,
FormControl_Shadcn_,
FormField_Shadcn_,
Input_Shadcn_,
InputGroup,
InputGroupAddon,
InputGroupInput,
Popover_Shadcn_,
PopoverContent_Shadcn_,
PopoverTrigger_Shadcn_,
Select_Shadcn_,
SelectContent_Shadcn_,
SelectItem_Shadcn_,
SelectTrigger_Shadcn_,
SelectValue_Shadcn_,
Separator,
SheetSection,
Switch,
Textarea,
useWatch_Shadcn_,
} from 'ui'
import { Input as DataInput } from 'ui-patterns/DataInputs/Input'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import type { Enum } from './AuthProvidersForm.types'
interface FormFieldProps {
projectRef: string | undefined
organizationSlug: string | undefined
name: string
properties: any
control: Control
hasAccess: boolean
disabled?: boolean
}
const FormField = ({
projectRef,
name,
properties,
organizationSlug,
control,
hasAccess,
disabled: disabledProp,
}: FormFieldProps) => {
const { description: originalDescription } = properties
let description = originalDescription
if (originalDescription && projectRef) {
description = originalDescription.replace(
/\(\.\.\/auth\/(.*?)\)/g,
`(/project/${projectRef}/auth/$1)`
)
}
const fieldValue = useWatch_Shadcn_({ control, name })
if (!hasAccess) {
const planMessage = organizationSlug
? `Only available on [Pro plan](/org/${organizationSlug}/billing?panel=subscriptionPlan) and above.`
: ''
description = originalDescription ? `${originalDescription} ${planMessage}` : planMessage
}
const disabled =
disabledProp || (properties.type === 'boolean' ? !hasAccess && !fieldValue : !hasAccess)
const showValue = useWatch_Shadcn_({
control,
name: properties.show?.key,
disabled: properties.show == null,
})
if (properties.show) {
if (properties.show.matches) {
if (!properties.show.matches.includes(showValue)) {
return null
}
} else if (!showValue) {
return null
}
}
switch (properties.type) {
case 'datetime':
return (
<>
<SheetSection>
<FormField_Shadcn_
control={control}
name={name}
disabled={disabled}
render={({ field }) => (
<FormItemLayout
layout="horizontal"
label={properties.title}
description={
description ? (
<ReactMarkdown unwrapDisallowed disallowedElements={['p']}>
{description}
</ReactMarkdown>
) : null
}
>
<FormControl_Shadcn_>
<Popover_Shadcn_>
<PopoverTrigger_Shadcn_ asChild>
<Button
type="outline"
className="w-full justify-start text-left font-normal px-3 py-4"
icon={<CalendarIcon className="h-4 w-4" />}
size="small"
>
{field.value ? format(new Date(field.value), 'PPP') : 'Pick a date'}
</Button>
</PopoverTrigger_Shadcn_>
<PopoverContent_Shadcn_ className="w-auto p-0" align="start">
<Calendar
mode="single"
selected={field.value}
onSelect={(date) => {
field.onChange(date?.toISOString())
}}
initialFocus
/>
</PopoverContent_Shadcn_>
</Popover_Shadcn_>
</FormControl_Shadcn_>
</FormItemLayout>
)}
/>
</SheetSection>
<Separator className="w-full" />
</>
)
case 'string':
return (
<>
<SheetSection>
<FormField_Shadcn_
control={control}
name={name}
disabled={disabled}
render={({ field }) => (
<FormItemLayout
layout="horizontal"
label={properties.title}
description={
description ? (
<Markdown content={description} className="text-foreground-lighter" />
) : null
}
>
<FormControl_Shadcn_ className="col-span-6">
{properties.isSecret ? (
<DataInput {...field} id={name} size="small" copy reveal />
) : (
<Input_Shadcn_ {...field} id={name} />
)}
</FormControl_Shadcn_>
</FormItemLayout>
)}
/>
</SheetSection>
<Separator className="w-full" />
</>
)
case 'multiline-string':
return (
<>
<SheetSection>
<FormField_Shadcn_
control={control}
name={name}
disabled={disabled}
render={({ field }) => (
<FormItemLayout
layout="horizontal"
label={properties.title}
description={
description ? (
<Markdown content={description} className="text-foreground-lighter" />
) : null
}
>
<FormControl_Shadcn_ className="col-span-6">
<Textarea
{...field}
id={name}
rows={4}
placeholder="Enter multi-line text"
className="resize-none"
/>
</FormControl_Shadcn_>
</FormItemLayout>
)}
/>
</SheetSection>
<Separator className="w-full" />
</>
)
case 'number':
return (
<>
<SheetSection>
<FormField_Shadcn_
control={control}
name={name}
disabled={disabled}
render={({ field }) => (
<FormItemLayout
layout="horizontal"
label={properties.title}
description={
description ? (
<Markdown content={description} className="text-foreground-lighter" />
) : null
}
>
<FormControl_Shadcn_ className="col-span-6">
{properties.units ? (
<InputGroup>
<InputGroupAddon align="inline-end">
<ReactMarkdown unwrapDisallowed disallowedElements={['p']}>
{properties.units}
</ReactMarkdown>
</InputGroupAddon>
<InputGroupInput
{...field}
id={name}
type="number"
onChange={(e) => field.onChange(Number(e.target.value))}
/>
</InputGroup>
) : (
<Input_Shadcn_
{...field}
id={name}
type="number"
onChange={(e) => field.onChange(Number(e.target.value))}
/>
)}
</FormControl_Shadcn_>
</FormItemLayout>
)}
/>
</SheetSection>
<Separator className="w-full" />
</>
)
case 'boolean':
return (
<>
<SheetSection>
<FormField_Shadcn_
control={control}
name={name}
disabled={disabled}
render={({ field }) => (
<FormItemLayout
layout="horizontal"
label={properties.title}
description={
<div className="flex flex-col gap-1">
{description ? <Markdown content={description} /> : null}
{properties.link && (
<span>
<Button asChild type="default" size="tiny" icon={<ExternalLink />}>
<a href={properties.link} target="_blank" rel="noreferrer noopener">
Documentation
</a>
</Button>
</span>
)}
</div>
}
>
<FormControl_Shadcn_ className="col-span-6">
<Switch
id={name}
checked={field.value}
onCheckedChange={field.onChange}
size="small"
/>
</FormControl_Shadcn_>
</FormItemLayout>
)}
/>
</SheetSection>
<Separator className="w-full" />
</>
)
case 'select':
return (
<>
<SheetSection>
<FormField_Shadcn_
control={control}
name={name}
disabled={disabled}
render={({ field }) => (
<FormItemLayout
layout="horizontal"
label={properties.title}
description={
description ? (
<ReactMarkdown
unwrapDisallowed
disallowedElements={['p']}
className="form-field-markdown"
>
{description}
</ReactMarkdown>
) : null
}
>
<FormControl_Shadcn_ className="col-span-6">
<Select_Shadcn_
defaultValue={properties.enum[0]}
value={field.value}
onValueChange={field.onChange}
>
<SelectTrigger_Shadcn_>
<SelectValue_Shadcn_ placeholder="Select an option" />
</SelectTrigger_Shadcn_>
<SelectContent_Shadcn_>
{properties.enum.map((option: Enum) => (
<SelectItem_Shadcn_ key={option.value} value={option.value}>
<span className="flex gap-2 items-center">
{option.icon ? (
<img
alt={`${option.label} icon`}
className="h-6 w-6"
src={`${BASE_PATH}/img/icons/${option.icon}`}
/>
) : null}
{option.label}
</span>
</SelectItem_Shadcn_>
))}
</SelectContent_Shadcn_>
</Select_Shadcn_>
</FormControl_Shadcn_>
</FormItemLayout>
)}
/>
</SheetSection>
<Separator className="w-full" />
</>
)
default:
return null
}
}
export default FormField