-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathRadioGroup.tsx
53 lines (49 loc) · 2.21 KB
/
RadioGroup.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import React, { ReactNode } from 'react';
import { Radio as RACRadio, RadioGroup as RACRadioGroup, RadioGroupProps as RACRadioGroupProps, RadioProps, ValidationResult } from 'react-aria-components';
import { tv } from 'tailwind-variants';
import { Description, FieldError, Label } from './Field';
import { composeTailwindRenderProps, focusRing } from './utils';
export interface RadioGroupProps extends Omit<RACRadioGroupProps, 'children'> {
label?: string,
children?: ReactNode,
description?: string;
errorMessage?: string | ((validation: ValidationResult) => string);
}
export function RadioGroup(props: RadioGroupProps) {
return (
<RACRadioGroup {...props} className={composeTailwindRenderProps(props.className, 'group flex flex-col gap-2')}>
<Label>{props.label}</Label>
<div className="flex group-orientation-vertical:flex-col gap-2 group-orientation-horizontal:gap-4">
{props.children}
</div>
{props.description && <Description>{props.description}</Description>}
<FieldError>{props.errorMessage}</FieldError>
</RACRadioGroup>
);
}
const styles = tv({
extend: focusRing,
base: 'w-5 h-5 rounded-full border-2 bg-white dark:bg-zinc-900 transition-all',
variants: {
isSelected: {
false: 'border-gray-400 dark:border-zinc-400 group-pressed:border-gray-500 dark:group-pressed:border-zinc-300',
true: 'border-[7px] border-gray-700 dark:border-slate-300 forced-colors:!border-[Highlight] group-pressed:border-gray-800 dark:group-pressed:border-slate-200'
},
isInvalid: {
true: 'border-red-700 dark:border-red-600 group-pressed:border-red-800 dark:group-pressed:border-red-700 forced-colors:!border-[Mark]'
},
isDisabled: {
true: 'border-gray-200 dark:border-zinc-700 forced-colors:!border-[GrayText]'
}
}
});
export function Radio(props: RadioProps) {
return (
<RACRadio {...props} className={composeTailwindRenderProps(props.className, 'flex gap-2 items-center group text-gray-800 disabled:text-gray-300 dark:text-zinc-200 dark:disabled:text-zinc-600 forced-colors:disabled:text-[GrayText] text-sm transition')}>
{renderProps => <>
<div className={styles(renderProps)} />
{props.children}
</>}
</RACRadio>
);
}