-
-
Notifications
You must be signed in to change notification settings - Fork 871
Expand file tree
/
Copy pathRadio.tsx
More file actions
110 lines (101 loc) · 2.87 KB
/
Copy pathRadio.tsx
File metadata and controls
110 lines (101 loc) · 2.87 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import React from 'react';
import type {
RadioGroupProps as AriaRadioGroupProps,
RadioProps as AriaRadioProps,
ValidationResult,
} from 'react-aria-components';
import {
RadioGroup as AriaRadioGroup,
Radio as AriaRadio,
composeRenderProps,
} from 'react-aria-components';
import { tv } from 'tailwind-variants';
import { composeTailwindRenderProps } from '../utils';
import { Description, FieldError, Label } from '../Field/Field';
interface RadioGroupProps extends Omit<AriaRadioGroupProps, 'children'> {
label?: string;
children?: React.ReactNode;
description?: string;
errorMessage?: string | ((validation: ValidationResult) => string);
ref?: React.Ref<HTMLDivElement>;
}
const RadioGroup = (props: RadioGroupProps) => {
return (
<AriaRadioGroup
{...props}
className={composeTailwindRenderProps(
props.className,
'flex flex-col gap-2',
)}
>
<Label>{props.label}</Label>
{props.children}
{props.description && <Description>{props.description}</Description>}
<FieldError>{props.errorMessage}</FieldError>
</AriaRadioGroup>
);
};
const radioStyles = tv({
base: 'bg-muted size-4 shrink-0 rounded-full border transition',
variants: {
isSelected: {
false: 'border-input',
true: 'border-primary border-[4.5px]',
},
isFocused: {
true: [
'border-ring bg-primary/20 ring-primary/20 ring-4',
'group-invalid:border-danger/70 group-invalid:bg-danger/20 group-invalid:ring-danger/20',
],
},
isInvalid: {
true: 'border-danger/70 bg-danger/20 text-red-700',
},
isDisabled: {
true: 'opacity-50',
},
},
});
const radioBaseStyles = tv({
base: 'group flex items-center gap-2 text-sm transition [&[data-disabled]]:opacity-50',
});
interface RadioProps extends AriaRadioProps {
description?: string;
label?: string;
ref?: React.Ref<HTMLLabelElement>;
}
const Radio = (props: RadioProps) => {
return (
<AriaRadio
{...props}
className={composeRenderProps(props.className, (className, renderProps) =>
radioBaseStyles({ ...renderProps, className }),
)}
>
{(renderProps) => (
<div className="flex gap-2">
<div
className={radioStyles({
...renderProps,
className: 'description' in props ? 'mt-1' : 'mt-0.5',
})}
/>
<div className="flex flex-col gap-1">
{props.label || props.description ? (
<>
{<Label>{props.label}</Label>}
{props.description && (
<Description>{props.description}</Description>
)}
</>
) : (
(props.children as React.ReactNode)
)}
</div>
</div>
)}
</AriaRadio>
);
};
export type { RadioGroupProps, RadioProps };
export { Radio, RadioGroup };