feat: add form field wrapper with label and error

This commit is contained in:
rajun 2026-10-27 16:28:50 +05:30
parent 9ccbd9dcce
commit fd36cf889a

View File

@ -0,0 +1,32 @@
import type { ReactNode } from 'react';
import { InlineMessage } from '~/components/ui/inline-message';
import { Label } from '~/components/ui/label';
import { Text } from '~/components/ui/text';
interface FieldProps {
label: string;
htmlFor?: string;
required?: boolean;
hint?: string;
error?: string;
// Shown on the right of the label: "Optional" / "Required"
requirement?: 'optional' | 'required';
action?: ReactNode;
children?: ReactNode;
}
export function Field({ label, htmlFor, required, hint, error, requirement, action, children }: FieldProps) {
return (
<div className='space-y-2'>
<div className='flex min-h-6 items-center justify-between gap-3'>
<Label htmlFor={htmlFor}>
{label}
{required && <Text as='span' variant='label' tone='primary' className='ml-0.5'>*</Text>}
</Label>
{action ?? (requirement && <Text as='span' variant='meta'>{requirement === 'optional' ? 'Optional' : 'Required'}</Text>)}
</div>
{children}
{error ? <InlineMessage tone='danger'>{error}</InlineMessage> : hint && <Text variant='caption'>{hint}</Text>}
</div>
);
}