tasko/components/form/form-textarea.tsx

81 lines
1.9 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 { useFormStatus } from 'react-dom';
import { KeyboardEventHandler, forwardRef } from 'react';
2024-02-15 02:30:10 +00:00
2024-02-16 01:49:19 +00:00
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { cn } from '@/lib/utils';
2024-02-15 02:30:10 +00:00
2024-02-16 01:49:19 +00:00
import { FormErrors } from './form-errors';
2024-02-15 02:30:10 +00:00
interface FormTextareaProps {
id: string;
label?: string;
placeholder?: string;
required?: boolean;
disabled?: boolean;
errors?: Record<string, string[] | undefined>;
className?: string;
onBlur?: () => void;
onClick?: () => void;
onKeyDown?: KeyboardEventHandler<HTMLTextAreaElement> | undefined;
defaultValue?: string;
}
export const FormTextarea = forwardRef<HTMLTextAreaElement, FormTextareaProps>(
(
{
id,
label,
placeholder,
required,
disabled,
errors,
className,
onBlur,
onClick,
onKeyDown,
defaultValue,
},
ref
) => {
const { pending } = useFormStatus();
return (
2024-02-16 01:49:19 +00:00
<div className='w-full space-y-2'>
<div className='w-full 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}
<Textarea
onKeyDown={onKeyDown}
onBlur={onBlur}
onClick={onClick}
ref={ref}
required={required}
placeholder={placeholder}
name={id}
id={id}
disabled={pending || disabled}
className={cn(
2024-02-16 01:49:19 +00:00
'resize-none shadow-sm outline-none ring-0 focus:ring-0 focus-visible:ring-0 focus-visible:ring-offset-0',
2024-02-15 02:30:10 +00:00
className
)}
aria-describedby={`${id}-error`}
defaultValue={defaultValue}
/>
</div>
<FormErrors id={id} errors={errors} />
</div>
);
}
);
2024-02-16 01:49:19 +00:00
FormTextarea.displayName = 'FormTextarea';