feat: add form field wrapper with label and error
This commit is contained in:
parent
9ccbd9dcce
commit
fd36cf889a
32
app/components/ui/field.tsx
Normal file
32
app/components/ui/field.tsx
Normal 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>
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user