1
+ import { useLabelContext } from '@radix-ui/react-label'
1
2
import {
2
3
Root ,
3
4
Trigger ,
@@ -12,36 +13,34 @@ import {
12
13
Group ,
13
14
Separator ,
14
15
ScrollDownButton ,
15
- Label
16
16
} from '@radix-ui/react-select'
17
17
import React , { ComponentProps , ElementRef , forwardRef } from 'react'
18
18
import { CSSProps , styled } from '../../stitches.config'
19
19
import { ChevronDown , ChevronUp , Check } from '../Icons'
20
20
import { inputStyles } from '../Input/Input'
21
+ import { Label } from '../Label'
21
22
22
23
const StyledTrigger = styled ( Trigger , inputStyles , {
23
- // all: 'unset',
24
24
display : 'inline-flex' ,
25
25
alignItems : 'center' ,
26
26
justifyContent : 'space-between' ,
27
- // padding: '$2 $3 $2 $6',
28
27
cursor : 'pointer' ,
29
- alignContent : 'center'
28
+ alignContent : 'center' ,
30
29
} )
31
30
32
31
const StyledValue = styled ( Value , {
33
- flexGrow : 1
32
+ flexGrow : 1 ,
34
33
} )
35
34
36
35
const StyledContent = styled ( Content , {
37
36
overflow : 'hidden' ,
38
37
backgroundColor : '$colors$brandBackground' ,
39
38
borderRadius : '$default' ,
40
- boxShadow : '$2'
39
+ boxShadow : '$2' ,
41
40
} )
42
41
43
42
const StyledViewport = styled ( Viewport , {
44
- padding : 5
43
+ padding : 5 ,
45
44
} )
46
45
47
46
const StyledItem = styled ( Item , {
@@ -63,25 +62,25 @@ const StyledItem = styled(Item, {
63
62
64
63
'&:focus' : {
65
64
background : '$selection' ,
66
- cursor : 'pointer'
65
+ cursor : 'pointer' ,
67
66
} ,
68
67
'&[data-disabled]' : {
69
68
color : '$grey9' ,
70
- pointerEvents : 'none'
71
- }
69
+ pointerEvents : 'none' ,
70
+ } ,
72
71
} )
73
72
74
73
const StyledLabel = styled ( Label , {
75
74
padding : '$0 $2' ,
76
75
fontSize : '$0' ,
77
76
lineHeight : '$2' ,
78
- color : '$grey10'
77
+ color : '$grey10' ,
79
78
} )
80
79
81
80
const StyledSeparator = styled ( Separator , {
82
81
height : 1 ,
83
82
backgroundColor : '$grey7' ,
84
- margin : '$1'
83
+ margin : '$1' ,
85
84
} )
86
85
87
86
const StyledItemIndicator = styled ( ItemIndicator , {
@@ -90,7 +89,7 @@ const StyledItemIndicator = styled(ItemIndicator, {
90
89
width : '$4' ,
91
90
display : 'inline-flex' ,
92
91
alignItems : 'center' ,
93
- justifyContent : 'center'
92
+ justifyContent : 'center' ,
94
93
} )
95
94
96
95
const scrollButtonStyles = {
@@ -99,50 +98,67 @@ const scrollButtonStyles = {
99
98
justifyContent : 'center' ,
100
99
height : '$5' ,
101
100
color : '$grey7' ,
102
- cursor : 'default'
101
+ cursor : 'default' ,
103
102
}
104
103
105
104
const StyledScrollUpButton = styled ( ScrollUpButton , scrollButtonStyles )
106
105
107
106
const StyledScrollDownButton = styled ( ScrollDownButton , scrollButtonStyles )
108
107
109
- type AbstractSelectProps = ComponentProps < typeof Root > & CSSProps
108
+ type SelectProps = ComponentProps < typeof Root > &
109
+ CSSProps & {
110
+ /** Add a label to the select */
111
+ label ?: string
112
+ /** Add id to the select */
113
+ id ?: string
114
+ }
110
115
111
- const AbstractedSelect = forwardRef <
112
- ElementRef < typeof Root > ,
113
- AbstractSelectProps
114
- > ( ( { children, ...props } , forwardedRef ) => {
115
- return (
116
- < Root { ...props } >
117
- < StyledTrigger ref = { forwardedRef } >
118
- < StyledValue />
119
- < ChevronDown />
120
- </ StyledTrigger >
121
- < StyledContent >
122
- < StyledScrollUpButton >
123
- < ChevronUp />
124
- </ StyledScrollUpButton >
125
- < StyledViewport > { children } </ StyledViewport >
126
- < StyledScrollDownButton >
127
- < ChevronDown />
128
- </ StyledScrollDownButton >
129
- </ StyledContent >
130
- </ Root >
131
- )
132
- } )
116
+ export const Select = forwardRef < ElementRef < typeof Root > , SelectProps > (
117
+ ( { label, id, children, ...props } , forwardedRef ) => {
118
+ const labelId = useLabelContext ( )
119
+ return (
120
+ < >
121
+ { label && (
122
+ < Label variant = "above" htmlFor = { id || labelId } >
123
+ { label }
124
+ </ Label >
125
+ ) }
126
+ < Root { ...props } >
127
+ < StyledTrigger
128
+ aria-labelledby = { labelId }
129
+ id = { id || label }
130
+ ref = { forwardedRef }
131
+ >
132
+ < StyledValue />
133
+ < ChevronDown />
134
+ </ StyledTrigger >
135
+ < StyledContent >
136
+ < StyledScrollUpButton >
137
+ < ChevronUp />
138
+ </ StyledScrollUpButton >
139
+ < StyledViewport > { children } </ StyledViewport >
140
+ < StyledScrollDownButton >
141
+ < ChevronDown />
142
+ </ StyledScrollDownButton >
143
+ </ StyledContent >
144
+ </ Root >
145
+ </ >
146
+ )
147
+ }
148
+ )
133
149
134
- type AbstractSelectItemProps = ComponentProps < typeof Item >
150
+ type SelectItemProps = ComponentProps < typeof Item >
135
151
136
- const AbstractedSelectItem = forwardRef <
152
+ export const SelectItem = forwardRef <
137
153
ElementRef < typeof StyledItem > ,
138
- AbstractSelectItemProps
154
+ SelectItemProps
139
155
> ( ( { children, ...props } , forwardedRef ) => {
140
156
return (
141
157
< StyledItem { ...props } ref = { forwardedRef } >
142
- < ItemText > { children } </ ItemText >
143
158
< StyledItemIndicator >
144
159
< Check />
145
160
</ StyledItemIndicator >
161
+ < ItemText > { children } </ ItemText >
146
162
</ StyledItem >
147
163
)
148
164
} )
@@ -155,14 +171,12 @@ const AbstractedSelectItem = forwardRef<
155
171
*
156
172
* Based on [Radix Select](https://www.radix-ui.com/docs/primitives/components/select).
157
173
*/
158
- export const Select = AbstractedSelect
159
174
export const SelectTrigger = StyledTrigger
160
175
export const SelectValue = StyledValue
161
176
export const SelectIcon = Icon
162
177
export const SelectContent = StyledContent
163
178
export const SelectViewport = StyledViewport
164
179
export const SelectGroup = Group
165
- export const SelectItem = AbstractedSelectItem
166
180
export const SelectItemText = ItemText
167
181
export const SelectItemIndicator = StyledItemIndicator
168
182
export const SelectLabel = StyledLabel
0 commit comments