2024-03-05 16:56:17 +00:00
|
|
|
import { Label } from "@radix-ui/react-label"
|
|
|
|
import { Input } from "./ui/input"
|
2024-03-22 13:27:40 +00:00
|
|
|
import { type FieldName, useInputControl } from "@conform-to/react"
|
2024-03-05 16:56:17 +00:00
|
|
|
import { useId } from "react"
|
|
|
|
import { cn } from "~/utils"
|
|
|
|
import { Checkbox } from "~/components/ui/checkbox"
|
2024-03-25 07:59:35 +00:00
|
|
|
import { Textarea } from "./ui/textarea"
|
2024-03-05 16:56:17 +00:00
|
|
|
|
|
|
|
export const Field = ({
|
|
|
|
inputProps,
|
|
|
|
labelProps,
|
|
|
|
errors,
|
|
|
|
className
|
|
|
|
}: {
|
|
|
|
inputProps: {
|
|
|
|
name: FieldName<string>
|
|
|
|
} & React.HTMLProps<HTMLInputElement>
|
|
|
|
labelProps: React.LabelHTMLAttributes<HTMLLabelElement>
|
|
|
|
errors?: ListOfErrors
|
|
|
|
className?: string
|
|
|
|
}) => {
|
|
|
|
const fallbackId = useId()
|
|
|
|
const id = inputProps.id ?? fallbackId
|
|
|
|
const errorId = errors?.length ? `${id}-error` : undefined
|
|
|
|
return (
|
|
|
|
<div className={className}>
|
2024-03-14 01:50:01 +00:00
|
|
|
<Label {...labelProps} htmlFor={id} className="font-semibold text-sm" />
|
2024-03-05 16:56:17 +00:00
|
|
|
<Input
|
|
|
|
{...inputProps}
|
2024-03-14 01:50:01 +00:00
|
|
|
className="mt-4"
|
2024-03-05 16:56:17 +00:00
|
|
|
id={id}
|
|
|
|
aria-invalid={errorId ? true : undefined}
|
|
|
|
aria-describedby={errorId}
|
|
|
|
/>
|
|
|
|
<div className="min-h-[32px] px-4 pb-3 pt-1">
|
|
|
|
{errorId ? <ErrorList id={errorId} errors={errors} /> : null}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const FieldCheckbox = ({
|
|
|
|
inputProps,
|
|
|
|
labelProps,
|
|
|
|
errors,
|
|
|
|
className
|
|
|
|
}: {
|
|
|
|
inputProps: {
|
2024-03-05 16:59:16 +00:00
|
|
|
name: string
|
|
|
|
form: string
|
|
|
|
value?: string
|
2024-03-05 16:56:17 +00:00
|
|
|
} & React.ComponentPropsWithoutRef<typeof Checkbox>
|
|
|
|
labelProps: React.LabelHTMLAttributes<HTMLLabelElement>
|
|
|
|
errors?: ListOfErrors
|
|
|
|
className?: string
|
|
|
|
}) => {
|
2024-03-05 16:59:16 +00:00
|
|
|
const { key, defaultChecked, ...checkboxProps } = inputProps
|
|
|
|
const checkedValue = inputProps.value ?? "on"
|
|
|
|
const input = useInputControl({
|
|
|
|
key,
|
|
|
|
name: inputProps.name,
|
2024-03-22 11:31:31 +00:00
|
|
|
formId: inputProps.form,
|
2024-03-05 16:59:16 +00:00
|
|
|
initialValue: defaultChecked ? checkedValue : undefined
|
|
|
|
})
|
2024-03-05 16:56:17 +00:00
|
|
|
const fallbackId = useId()
|
|
|
|
const id = inputProps.id ?? fallbackId
|
|
|
|
const errorId = errors?.length ? `${id}-error` : undefined
|
|
|
|
return (
|
2024-03-07 14:04:59 +00:00
|
|
|
<>
|
|
|
|
<div
|
|
|
|
className={cn("space-x-2 flex items-center text-primary-2", className)}
|
|
|
|
>
|
|
|
|
<Checkbox
|
|
|
|
{...checkboxProps}
|
|
|
|
id={id}
|
|
|
|
aria-invalid={errorId ? true : undefined}
|
|
|
|
aria-describedby={errorId}
|
|
|
|
checked={input.value === checkedValue}
|
|
|
|
onCheckedChange={(state) => {
|
|
|
|
input.change(state.valueOf() ? checkedValue : "")
|
|
|
|
inputProps.onCheckedChange?.(state)
|
|
|
|
}}
|
|
|
|
onFocus={(event) => {
|
|
|
|
input.focus()
|
|
|
|
inputProps.onFocus?.(event)
|
|
|
|
}}
|
|
|
|
onBlur={(event) => {
|
|
|
|
input.blur()
|
|
|
|
inputProps.onBlur?.(event)
|
|
|
|
}}
|
|
|
|
type="button"
|
|
|
|
/>
|
|
|
|
<Label {...labelProps} htmlFor={id} />
|
|
|
|
</div>
|
2024-03-05 16:56:17 +00:00
|
|
|
<div className="min-h-[32px] px-4 pb-3 pt-1">
|
|
|
|
{errorId ? <ErrorList id={errorId} errors={errors} /> : null}
|
|
|
|
</div>
|
2024-03-07 14:04:59 +00:00
|
|
|
</>
|
2024-03-05 16:56:17 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-03-25 07:59:35 +00:00
|
|
|
export function TextareaField({
|
|
|
|
labelProps,
|
|
|
|
textareaProps,
|
|
|
|
errors,
|
|
|
|
className,
|
|
|
|
}: {
|
|
|
|
labelProps: React.LabelHTMLAttributes<HTMLLabelElement>
|
|
|
|
textareaProps: React.TextareaHTMLAttributes<HTMLTextAreaElement>
|
|
|
|
errors?: ListOfErrors
|
|
|
|
className?: string
|
|
|
|
}) {
|
|
|
|
const fallbackId = useId()
|
|
|
|
const id = textareaProps.id ?? textareaProps.name ?? fallbackId
|
|
|
|
const errorId = errors?.length ? `${id}-error` : undefined
|
|
|
|
return (
|
|
|
|
<div className={className}>
|
|
|
|
<Label htmlFor={id} {...labelProps} />
|
|
|
|
<Textarea
|
|
|
|
id={id}
|
|
|
|
aria-invalid={errorId ? true : undefined}
|
|
|
|
aria-describedby={errorId}
|
|
|
|
{...textareaProps}
|
|
|
|
/>
|
|
|
|
<div className="min-h-[32px] pb-1 pt-1">
|
|
|
|
{errorId ? <ErrorList id={errorId} errors={errors} /> : null}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-03-05 16:56:17 +00:00
|
|
|
export type ListOfErrors = Array<string | null | undefined> | null | undefined
|
|
|
|
export function ErrorList({
|
|
|
|
id,
|
|
|
|
errors
|
|
|
|
}: {
|
|
|
|
errors?: ListOfErrors
|
|
|
|
id?: string
|
|
|
|
}) {
|
|
|
|
const errorsToRender = errors?.filter(Boolean)
|
|
|
|
if (!errorsToRender?.length) return null
|
|
|
|
return (
|
|
|
|
<ul id={id} className="flex flex-col gap-1">
|
|
|
|
{errorsToRender.map((e) => (
|
2024-03-07 14:04:59 +00:00
|
|
|
<li key={e} className="text-[12px] text-destructive-foreground">
|
2024-03-05 16:56:17 +00:00
|
|
|
{e}
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
)
|
|
|
|
}
|