-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathselect.tsx
220 lines (206 loc) · 7.95 KB
/
select.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
'use client';
import * as React from 'react';
import classNames from 'classnames';
import { Select as SelectPrimitive, ScrollArea as ScrollAreaPrimitive } from 'radix-ui';
import { extractProps } from '../helpers/extract-props.js';
import { marginPropDefs } from '../props/margin.props.js';
import { ChevronDownIcon, ThickCheckIcon } from './icons.js';
import {
selectRootPropDefs,
selectTriggerPropDefs,
selectContentPropDefs,
} from './select.props.js';
import { useThemeContext, Theme } from './theme.js';
import type { MarginProps } from '../props/margin.props.js';
import type { GetPropDefTypes } from '../props/prop-def.js';
import type { ComponentPropsWithout, RemovedProps } from '../helpers/component-props.js';
type SelectRootOwnProps = GetPropDefTypes<typeof selectRootPropDefs>;
type SelectContextValue = SelectRootOwnProps;
const SelectContext = React.createContext<SelectContextValue>({});
interface SelectRootProps extends SelectPrimitive.SelectProps, SelectContextValue {}
const SelectRoot: React.FC<SelectRootProps> = (props) => {
const { children, size = selectRootPropDefs.size.default, ...rootProps } = props;
return (
<SelectPrimitive.Root {...rootProps}>
<SelectContext.Provider value={React.useMemo(() => ({ size }), [size])}>
{children}
</SelectContext.Provider>
</SelectPrimitive.Root>
);
};
SelectRoot.displayName = 'Select.Root';
type SelectTriggerElement = React.ElementRef<typeof SelectPrimitive.Trigger>;
type SelectTriggerOwnProps = GetPropDefTypes<typeof selectTriggerPropDefs>;
interface SelectTriggerProps
extends ComponentPropsWithout<typeof SelectPrimitive.Trigger, RemovedProps>,
MarginProps,
SelectTriggerOwnProps {}
const SelectTrigger = React.forwardRef<SelectTriggerElement, SelectTriggerProps>(
(props, forwardedRef) => {
const context = React.useContext(SelectContext);
const { children, className, color, radius, placeholder, ...triggerProps } = extractProps(
// Pass size value from the context to generate styles
{ size: context?.size, ...props },
// Pass size prop def to allow it to be extracted
{ size: selectRootPropDefs.size },
selectTriggerPropDefs,
marginPropDefs
);
return (
<SelectPrimitive.Trigger asChild>
<button
data-accent-color={color}
data-radius={radius}
{...triggerProps}
ref={forwardedRef}
className={classNames('rt-reset', 'rt-SelectTrigger', className)}
>
<span className="rt-SelectTriggerInner">
<SelectPrimitive.Value placeholder={placeholder}>{children}</SelectPrimitive.Value>
</span>
<SelectPrimitive.Icon asChild>
<ChevronDownIcon className="rt-SelectIcon" />
</SelectPrimitive.Icon>
</button>
</SelectPrimitive.Trigger>
);
}
);
SelectTrigger.displayName = 'Select.Trigger';
type SelectContentElement = React.ElementRef<typeof SelectPrimitive.Content>;
type SelectContentOwnProps = GetPropDefTypes<typeof selectContentPropDefs>;
interface SelectContentProps
extends ComponentPropsWithout<typeof SelectPrimitive.Content, RemovedProps>,
SelectContentOwnProps {
container?: React.ComponentPropsWithoutRef<typeof SelectPrimitive.Portal>['container'];
}
const SelectContent = React.forwardRef<SelectContentElement, SelectContentProps>(
(props, forwardedRef) => {
const context = React.useContext(SelectContext);
const { className, children, color, container, ...contentProps } = extractProps(
// Pass size value from the context to generate styles
{ size: context?.size, ...props },
// Pass size prop def to allow it to be extracted
{ size: selectRootPropDefs.size },
selectContentPropDefs
);
const themeContext = useThemeContext();
const resolvedColor = color || themeContext.accentColor;
return (
<SelectPrimitive.Portal container={container}>
<Theme asChild>
<SelectPrimitive.Content
data-accent-color={resolvedColor}
sideOffset={4}
{...contentProps}
asChild={false}
ref={forwardedRef}
className={classNames(
{ 'rt-PopperContent': contentProps.position === 'popper' },
'rt-SelectContent',
className
)}
>
<ScrollAreaPrimitive.Root type="auto" className="rt-ScrollAreaRoot">
<SelectPrimitive.Viewport asChild className="rt-SelectViewport">
<ScrollAreaPrimitive.Viewport
className="rt-ScrollAreaViewport"
style={{ overflowY: undefined }}
>
{children}
</ScrollAreaPrimitive.Viewport>
</SelectPrimitive.Viewport>
<ScrollAreaPrimitive.Scrollbar
className="rt-ScrollAreaScrollbar rt-r-size-1"
orientation="vertical"
>
<ScrollAreaPrimitive.Thumb className="rt-ScrollAreaThumb" />
</ScrollAreaPrimitive.Scrollbar>
</ScrollAreaPrimitive.Root>
</SelectPrimitive.Content>
</Theme>
</SelectPrimitive.Portal>
);
}
);
SelectContent.displayName = 'Select.Content';
type SelectItemElement = React.ElementRef<typeof SelectPrimitive.Item>;
interface SelectItemProps
extends ComponentPropsWithout<typeof SelectPrimitive.Item, RemovedProps> {}
const SelectItem = React.forwardRef<SelectItemElement, SelectItemProps>((props, forwardedRef) => {
const { className, children, ...itemProps } = props;
return (
<SelectPrimitive.Item
{...itemProps}
asChild={false}
ref={forwardedRef}
className={classNames('rt-SelectItem', className)}
>
<SelectPrimitive.ItemIndicator className="rt-SelectItemIndicator">
<ThickCheckIcon className="rt-SelectItemIndicatorIcon" />
</SelectPrimitive.ItemIndicator>
<SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
</SelectPrimitive.Item>
);
});
SelectItem.displayName = 'Select.Item';
type SelectGroupElement = React.ElementRef<typeof SelectPrimitive.Group>;
interface SelectGroupProps
extends ComponentPropsWithout<typeof SelectPrimitive.Group, RemovedProps> {}
const SelectGroup = React.forwardRef<SelectGroupElement, SelectGroupProps>(
({ className, ...props }, forwardedRef) => (
<SelectPrimitive.Group
{...props}
asChild={false}
ref={forwardedRef}
className={classNames('rt-SelectGroup', className)}
/>
)
);
SelectGroup.displayName = 'Select.Group';
type SelectLabelElement = React.ElementRef<typeof SelectPrimitive.Label>;
interface SelectLabelProps
extends ComponentPropsWithout<typeof SelectPrimitive.Label, RemovedProps> {}
const SelectLabel = React.forwardRef<SelectLabelElement, SelectLabelProps>(
({ className, ...props }, forwardedRef) => (
<SelectPrimitive.Label
{...props}
asChild={false}
ref={forwardedRef}
className={classNames('rt-SelectLabel', className)}
/>
)
);
SelectLabel.displayName = 'Select.Label';
type SelectSeparatorElement = React.ElementRef<typeof SelectPrimitive.Separator>;
interface SelectSeparatorProps
extends ComponentPropsWithout<typeof SelectPrimitive.Separator, RemovedProps> {}
const SelectSeparator = React.forwardRef<SelectSeparatorElement, SelectSeparatorProps>(
({ className, ...props }, forwardedRef) => (
<SelectPrimitive.Separator
{...props}
asChild={false}
ref={forwardedRef}
className={classNames('rt-SelectSeparator', className)}
/>
)
);
SelectSeparator.displayName = 'Select.Separator';
export {
SelectRoot as Root,
SelectTrigger as Trigger,
SelectContent as Content,
SelectItem as Item,
SelectGroup as Group,
SelectLabel as Label,
SelectSeparator as Separator,
};
export type {
SelectRootProps as RootProps,
SelectTriggerProps as TriggerProps,
SelectContentProps as ContentProps,
SelectItemProps as ItemProps,
SelectGroupProps as GroupProps,
SelectLabelProps as LabelProps,
SelectSeparatorProps as SeparatorProps,
};