|
1
|
"use client" |
|
2
|
|
|
3
|
import { RadioGroup as RadixRadioGroup } from "radix-ui" |
|
4
|
import * as React from "react" |
|
5
|
|
|
6
|
import { Hint } from "@/components/hint" |
|
7
|
import { Label } from "@/components/label" |
|
8
|
import { clx } from "@/utils/clx" |
|
9
|
|
|
10
|
/** |
|
11
|
* This component is based on the [Radix UI Radio Group](https://www.radix-ui.com/primitives/docs/components/radio-group) primitives. |
|
12
|
*/ |
|
13
|
const Root = React.forwardRef< |
|
14
|
React.ElementRef<typeof RadixRadioGroup.Root>, |
|
15
|
React.ComponentPropsWithoutRef<typeof RadixRadioGroup.Root> |
|
16
|
>(({ className, ...props }, ref) => { |
|
17
|
return ( |
|
18
|
<RadixRadioGroup.Root |
|
19
|
className={clx("grid gap-2", className)} |
|
20
|
{...props} |
|
21
|
ref={ref} |
|
22
|
/> |
|
23
|
) |
|
24
|
}) |
|
25
|
Root.displayName = "RadioGroup" |
|
26
|
|
|
27
|
const Indicator = React.forwardRef< |
|
28
|
React.ElementRef<typeof RadixRadioGroup.Indicator>, |
|
29
|
React.ComponentPropsWithoutRef<typeof RadixRadioGroup.Indicator> |
|
30
|
>(({ className, ...props }, ref) => { |
|
31
|
return ( |
|
32
|
<RadixRadioGroup.Indicator |
|
33
|
ref={ref} |
|
34
|
className={clx("flex items-center justify-center", className)} |
|
35
|
{...props} |
|
36
|
> |
|
37
|
<div |
|
38
|
className={clx( |
|
39
|
"bg-ui-bg-base shadow-details-contrast-on-bg-interactive h-1.5 w-1.5 rounded-full" |
|
40
|
)} |
|
41
|
/> |
|
42
|
</RadixRadioGroup.Indicator> |
|
43
|
) |
|
44
|
}) |
|
45
|
Indicator.displayName = "RadioGroup.Indicator" |
|
46
|
|
|
47
|
const Item = React.forwardRef< |
|
48
|
React.ElementRef<typeof RadixRadioGroup.Item>, |
|
49
|
React.ComponentPropsWithoutRef<typeof RadixRadioGroup.Item> |
|
50
|
>(({ className, ...props }, ref) => { |
|
51
|
return ( |
|
52
|
<RadixRadioGroup.Item |
|
53
|
ref={ref} |
|
54
|
className={clx( |
|
55
|
"group relative flex h-5 w-5 items-center justify-center outline-none", |
|
56
|
className |
|
57
|
)} |
|
58
|
{...props} |
|
59
|
> |
|
60
|
<div |
|
61
|
className={clx( |
|
62
|
"shadow-borders-base bg-ui-bg-base transition-fg flex h-[14px] w-[14px] items-center justify-center rounded-full", |
|
63
|
"group-hover:group-enabled:group-data-[state=unchecked]:bg-ui-bg-base-hover", |
|
64
|
"group-data-[state=checked]:bg-ui-bg-interactive group-data-[state=checked]:shadow-borders-interactive-with-shadow", |
|
65
|
"group-focus-visible:!shadow-borders-interactive-with-focus", |
|
66
|
"group-disabled:cursor-not-allowed group-disabled:opacity-50" |
|
67
|
)} |
|
68
|
> |
|
69
|
<Indicator /> |
|
70
|
</div> |
|
71
|
</RadixRadioGroup.Item> |
|
72
|
) |
|
73
|
}) |
|
74
|
Item.displayName = "RadioGroup.Item" |
|
75
|
|
|
76
|
interface ChoiceBoxProps |
|
77
|
extends React.ComponentPropsWithoutRef<typeof RadixRadioGroup.Item> { |
|
78
|
label: string |
|
79
|
description: string |
|
80
|
} |
|
81
|
|
|
82
|
/** |
|
83
|
* This component is based on the [Radix UI Radio Group Item](https://www.radix-ui.com/primitives/docs/components/radio-group#item) primitives. |
|
84
|
*/ |
|
85
|
const ChoiceBox = React.forwardRef< |
|
86
|
React.ElementRef<typeof RadixRadioGroup.Item>, |
|
87
|
ChoiceBoxProps |
|
88
|
>(({ |
|
89
|
className, |
|
90
|
id, |
|
91
|
/** |
|
92
|
* The label for the radio button. |
|
93
|
*/ |
|
94
|
label, |
|
95
|
/** |
|
96
|
* The description for the radio button. |
|
97
|
*/ |
|
98
|
description, |
|
99
|
/** |
|
100
|
* The value of the radio button. |
|
101
|
*/ |
|
102
|
value, |
|
103
|
...props |
|
104
|
}: ChoiceBoxProps, ref) => { |
|
105
|
const generatedId = React.useId() |
|
106
|
|
|
107
|
if (!id) { |
|
108
|
id = generatedId |
|
109
|
} |
|
110
|
|
|
111
|
const descriptionId = `${id}-description` |
|
112
|
|
|
113
|
return ( |
|
114
|
<RadixRadioGroup.Item |
|
115
|
ref={ref} |
|
116
|
className={clx( |
|
117
|
"shadow-borders-base bg-ui-bg-base focus-visible:shadow-borders-interactive-with-focus transition-fg group flex items-start gap-x-2 rounded-lg p-3 outline-none", |
|
118
|
"hover:enabled:bg-ui-bg-base-hover", |
|
119
|
"data-[state=checked]:shadow-borders-interactive-with-shadow", |
|
120
|
"group-disabled:cursor-not-allowed group-disabled:opacity-50", |
|
121
|
className |
|
122
|
)} |
|
123
|
{...props} |
|
124
|
value={value} |
|
125
|
id={id} |
|
126
|
aria-describedby={descriptionId} |
|
127
|
> |
|
128
|
<div className="flex h-5 w-5 items-center justify-center"> |
|
129
|
<div |
|
130
|
className={clx( |
|
131
|
"shadow-borders-base bg-ui-bg-base group-data-[state=checked]:bg-ui-bg-interactive group-data-[state=checked]:shadow-borders-interactive-with-shadow transition-fg flex h-3.5 w-3.5 items-center justify-center rounded-full", |
|
132
|
"group-hover:group-enabled:group-data-[state=unchecked]:bg-ui-bg-base-hover" |
|
133
|
)} |
|
134
|
> |
|
135
|
<Indicator /> |
|
136
|
</div> |
|
137
|
</div> |
|
138
|
<div className="flex flex-col items-start"> |
|
139
|
<Label |
|
140
|
htmlFor={id} |
|
141
|
size="small" |
|
142
|
weight="plus" |
|
143
|
className="group-disabled:text-ui-fg-disabled cursor-pointer group-disabled:cursor-not-allowed" |
|
144
|
> |
|
145
|
{label} |
|
146
|
</Label> |
|
147
|
<Hint |
|
148
|
className="txt-small text-ui-fg-subtle group-disabled:text-ui-fg-disabled text-left" |
|
149
|
id={descriptionId} |
|
150
|
> |
|
151
|
{description} |
|
152
|
</Hint> |
|
153
|
</div> |
|
154
|
</RadixRadioGroup.Item> |
|
155
|
) |
|
156
|
}) |
|
157
|
ChoiceBox.displayName = "RadioGroup.ChoiceBox" |
|
158
|
|
|
159
|
const RadioGroup = Object.assign(Root, { Item, ChoiceBox }) |
|
160
|
|
|
161
|
export { RadioGroup } |