Skip to content

Commit 907bd92

Browse files
committed
Fix checkbox label alignment
1 parent 33ac356 commit 907bd92

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

packages/components/src/ui/checkbox-field.tsx

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,38 +63,47 @@ const CheckboxField = ({
6363
<FormField
6464
control={control}
6565
name={name}
66-
render={({ field, fieldState }) => (
67-
<FormItem className={cn('flex flex-row items-start space-y-0', className)}>
68-
<FormControl Component={components?.FormControl}>
69-
<CheckboxComponent
70-
ref={field.ref}
71-
className={isCustomCheckbox ? undefined : defaultCheckboxClassName}
72-
checked={field.value}
73-
onCheckedChange={field.onChange}
74-
data-slot="checkbox"
75-
{...props}
76-
>
77-
<IndicatorComponent
78-
className={isCustomIndicator ? undefined : defaultIndicatorClassName}
79-
data-slot="checkbox-indicator"
66+
render={({ field, fieldState }) => {
67+
const hasSupportingText = Boolean(description || fieldState.error);
68+
69+
return (
70+
<FormItem
71+
className={cn('flex flex-row gap-3 space-y-0', hasSupportingText ? 'items-start' : 'items-center', className)}
72+
>
73+
<FormControl Component={components?.FormControl}>
74+
<CheckboxComponent
75+
ref={field.ref}
76+
className={isCustomCheckbox ? undefined : cn(defaultCheckboxClassName, hasSupportingText && 'mt-px')}
77+
checked={field.value}
78+
onCheckedChange={field.onChange}
79+
data-slot="checkbox"
80+
{...props}
8081
>
81-
<Check className={cn('h-4 w-4', checkClassName)} />
82-
</IndicatorComponent>
83-
</CheckboxComponent>
84-
</FormControl>
85-
<div className="space-y-1 leading-none">
86-
{label && (
87-
<FormLabel Component={components?.FormLabel} className="!text-inherit">
88-
{label}
89-
</FormLabel>
90-
)}
91-
{description && <FormDescription Component={components?.FormDescription}>{description}</FormDescription>}
92-
{fieldState.error && (
93-
<FormMessage Component={components?.FormMessage}>{fieldState.error.message}</FormMessage>
94-
)}
95-
</div>
96-
</FormItem>
97-
)}
82+
<IndicatorComponent
83+
className={isCustomIndicator ? undefined : defaultIndicatorClassName}
84+
data-slot="checkbox-indicator"
85+
>
86+
<Check className={cn('h-4 w-4', checkClassName)} />
87+
</IndicatorComponent>
88+
</CheckboxComponent>
89+
</FormControl>
90+
<div className={cn(hasSupportingText ? 'relative -top-0.5 space-y-1.5 leading-snug' : 'leading-none')}>
91+
{label && (
92+
<FormLabel
93+
Component={components?.FormLabel}
94+
className={cn('!text-inherit', hasSupportingText ? 'leading-snug' : 'leading-none')}
95+
>
96+
{label}
97+
</FormLabel>
98+
)}
99+
{description && <FormDescription Component={components?.FormDescription}>{description}</FormDescription>}
100+
{fieldState.error && (
101+
<FormMessage Component={components?.FormMessage}>{fieldState.error.message}</FormMessage>
102+
)}
103+
</div>
104+
</FormItem>
105+
);
106+
}}
98107
/>
99108
);
100109
};

0 commit comments

Comments
 (0)