-
Notifications
You must be signed in to change notification settings - Fork 187
/
radio-group.tsx
142 lines (129 loc) · 4.94 KB
/
radio-group.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use client';
import * as React from 'react';
import classNames from 'classnames';
import { createContextScope } from '@radix-ui/react-context';
import * as RadioGroupPrimitive from '@radix-ui/react-radio-group';
import { createRadioGroupScope } from '@radix-ui/react-radio-group';
import { radioGroupRootPropDefs } from './radio-group.props.js';
import { Text } from './text.js';
import { extractProps } from '../helpers/extract-props.js';
import { marginPropDefs } from '../props/margin.props.js';
import type { Scope } from '@radix-ui/react-context';
import type { ComponentPropsWithout, RemovedProps } from '../helpers/component-props.js';
import type { MarginProps } from '../props/margin.props.js';
import type { GetPropDefTypes } from '../props/prop-def.js';
const RADIO_GROUP_NAME = 'RadioGroup';
type ScopedProps<P> = P & { __scopeRadioGroup?: Scope };
const [createRadioGroupContext] = createContextScope(RADIO_GROUP_NAME, [createRadioGroupScope]);
const useRadioGroupScope = createRadioGroupScope();
type RadioGroupRootOwnProps = GetPropDefTypes<typeof radioGroupRootPropDefs>;
type RadioGroupContextValue = RadioGroupRootOwnProps;
const [RadioGroupProvider, useRadioGroupContext] =
createRadioGroupContext<RadioGroupContextValue>(RADIO_GROUP_NAME);
type RadioGroupRootElement = React.ElementRef<typeof RadioGroupPrimitive.Root>;
interface RadioGroupRootProps
extends ComponentPropsWithout<
typeof RadioGroupPrimitive.Root,
'asChild' | 'color' | 'defaultChecked'
>,
MarginProps,
RadioGroupRootOwnProps {}
const RadioGroupRoot = React.forwardRef<RadioGroupRootElement, RadioGroupRootProps>(
(
{
color = radioGroupRootPropDefs.color.default,
highContrast = radioGroupRootPropDefs.highContrast.default,
size = radioGroupRootPropDefs.size.default,
variant = radioGroupRootPropDefs.variant.default,
...props
}: ScopedProps<RadioGroupRootProps>,
forwardedRef
) => {
const { __scopeRadioGroup, className, ...rootProps } = extractProps(props, marginPropDefs);
const radioGroupScope = useRadioGroupScope(__scopeRadioGroup);
return (
<RadioGroupProvider
scope={__scopeRadioGroup}
color={color}
highContrast={highContrast}
size={size}
variant={variant}
>
<RadioGroupPrimitive.Root
{...radioGroupScope}
{...rootProps}
ref={forwardedRef}
className={classNames('rt-RadioGroupRoot', className)}
/>
</RadioGroupProvider>
);
}
);
RadioGroupRoot.displayName = 'RadioGroup.Root';
type RadioGroupItemElement = React.ElementRef<typeof RadioGroupItemRadio>;
interface RadioGroupItemProps
extends ComponentPropsWithout<typeof RadioGroupItemRadio, RemovedProps>,
MarginProps {}
const RadioGroupItem = React.forwardRef<RadioGroupItemElement, RadioGroupItemProps>(
(_props: ScopedProps<RadioGroupItemProps>, forwardedRef) => {
const { __scopeRadioGroup, children, className, style, ...props } = _props;
const { size } = useRadioGroupContext('RadioGroupItem', __scopeRadioGroup);
// Render `<Text as="label">` if children are provided, otherwise render
// the solo radio button to allow building out your custom layouts with it.
if (children) {
return (
<Text
as="label"
size={size}
className={classNames('rt-RadioGroupItem', className)}
style={style}
>
<RadioGroupItemRadio
__scopeRadioGroup={__scopeRadioGroup}
{...props}
ref={forwardedRef}
/>
{children && <span className="rt-RadioGroupItemInner">{children}</span>}
</Text>
);
}
return (
<RadioGroupItemRadio
__scopeRadioGroup={__scopeRadioGroup}
{...props}
ref={forwardedRef}
className={className}
style={style}
/>
);
}
);
RadioGroupItem.displayName = 'RadioGroup.Item';
type RadioGroupItemRadioElement = React.ElementRef<typeof RadioGroupPrimitive.Item>;
interface RadioGroupItemRadioProps
extends React.ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Item> {}
const RadioGroupItemRadio = React.forwardRef<
RadioGroupItemRadioElement,
ScopedProps<RadioGroupItemRadioProps>
>(({ __scopeRadioGroup, ...props }, forwardedRef) => {
const context = useRadioGroupContext('RadioGroupItemRadio', __scopeRadioGroup);
const radioGroupScope = useRadioGroupScope(__scopeRadioGroup);
const { color, className } = extractProps(
{ ...props, ...context },
radioGroupRootPropDefs,
marginPropDefs
);
return (
<RadioGroupPrimitive.Item
{...radioGroupScope}
data-accent-color={color}
{...props}
asChild={false}
ref={forwardedRef}
className={classNames('rt-reset', 'rt-BaseRadioRoot', className)}
/>
);
});
RadioGroupItemRadio.displayName = 'RadioGroup.ItemRadio';
export { RadioGroupRoot as Root, RadioGroupItem as Item };
export type { RadioGroupRootProps as RootProps, RadioGroupItemProps as ItemProps };