tasko/components/form/form-input.tsx

74 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-02-16 01:49:19 +00:00
'use client';
2024-02-15 02:30:10 +00:00
2024-02-16 01:49:19 +00:00
import { forwardRef } from 'react';
import { useFormStatus } from 'react-dom';
2024-02-15 02:30:10 +00:00
2024-02-16 01:49:19 +00:00
import { cn } from '@/lib/utils';
import { Label } from '@/components/ui/label';
import { Input } from '@/components/ui/input';
import { FormErrors } from './form-errors';
2024-02-15 02:30:10 +00:00
interface FormInputProps {
id: string;
label?: string;
type?: string;
placeholder?: string;
required?: boolean;
disabled?: boolean;
errors?: Record<string, string[] | undefined>;
className?: string;
defaultValue?: string;
onBlur?: () => void;
}
export const FormInput = forwardRef<HTMLInputElement, FormInputProps>(
(
{
id,
label,
type,
placeholder,
required,
disabled,
errors,
className,
2024-02-16 01:49:19 +00:00
defaultValue = '',
2024-02-15 02:30:10 +00:00
onBlur,
},
ref
) => {
const { pending } = useFormStatus();
return (
2024-02-16 01:49:19 +00:00
<div className='space-y-2'>
<div className='space-y-1'>
2024-02-15 02:30:10 +00:00
{label ? (
<Label
htmlFor={id}
2024-02-16 01:49:19 +00:00
className='text-xs font-semibold text-neutral-700'
2024-02-15 02:30:10 +00:00
>
{label}
</Label>
) : null}
<Input
onBlur={onBlur}
defaultValue={defaultValue}
ref={ref}
required={required}
name={id}
id={id}
placeholder={placeholder}
type={type}
disabled={pending ?? disabled}
2024-02-16 01:49:19 +00:00
className={cn('h-7 px-2 py-1 text-sm', className)}
2024-02-15 02:30:10 +00:00
aria-describedby={`${id}-error`}
/>
</div>
<FormErrors id={id} errors={errors} />
</div>
);
}
);
2024-02-16 01:49:19 +00:00
FormInput.displayName = 'FormInput';