/
Select.tsx
111 lines (101 loc) · 2.33 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
import React, {forwardRef} from 'react'
import clsx from 'clsx'
import type {BaseProps} from '../../component-helpers'
import type {FormInputSizes, FormValidationStatus} from '../form-types'
import styles from './Select.module.css'
export type SelectProps = {
/**
* Applies full width styling.
*/
fullWidth?: boolean
/**
* Applies a required attribute to the input
*/
required?: boolean
/**
* Applies alternative sizing to the input
*/
size?: FormInputSizes
/**
*
*/
validationStatus?: FormValidationStatus
} & Omit<React.InputHTMLAttributes<HTMLSelectElement>, 'size'> &
BaseProps<HTMLSelectElement>
const _SelectRoot = (
{
children,
className,
disabled,
fullWidth = false,
placeholder,
size = 'medium',
validationStatus,
...rest
}: SelectProps,
ref,
) => {
return (
<span
className={clsx(
styles['Select-wrapper'],
styles[`Select-wrapper--${size}`],
fullWidth && styles['Select-wrapper--fullWidth'],
validationStatus && styles[`Select--${validationStatus}`],
disabled && styles[`Select-wrapper--disabled`],
)}
>
<select
aria-invalid={validationStatus === 'error'}
className={clsx(
styles.Select,
styles[`Select--${size}`],
'Select',
fullWidth && styles['Select--fullWidth'],
className,
)}
disabled={disabled}
placeholder={placeholder}
ref={ref}
{...rest}
>
{children}
</select>
</span>
)
}
const SelectRoot = forwardRef(_SelectRoot)
export type OptionProps = {
value: string
} & React.PropsWithChildren<React.HTMLProps<HTMLOptionElement>>
const Option = ({children, value, ...rest}: OptionProps) => {
return (
<option value={value} {...rest}>
{children}
</option>
)
}
export type OptGroupProps = {
/**
* Specifies that an option-group should be disabled
*/
disabled?: string
/**
* Specifies a label for an option-group
*/
label: string
} & React.PropsWithChildren<React.HTMLProps<HTMLOptGroupElement>>
const OptGroup = ({children, label, ...rest}: OptGroupProps) => {
return (
<optgroup label={label} {...rest}>
{children}
</optgroup>
)
}
/**
* Select
*/
export const Select = Object.assign(SelectRoot, {
Option,
OptGroup,
})