-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathebay-select.tsx
164 lines (146 loc) · 5.36 KB
/
ebay-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
import React, { ChangeEvent, cloneElement, ComponentProps, FC, useState, FocusEvent } from 'react'
import classNames from 'classnames'
import EbaySelectOption from './ebay-select-option'
import { EbayIcon } from '../ebay-icon'
import { filterByType, withForwardRef } from '../common/component-utils'
import { useFloatingLabel } from '../common/floating-label-utils/hooks'
import { EbayChangeEventHandler } from '../common/event-utils/types'
const isControlled = value => typeof value !== 'undefined'
type SelectValue = string | ReadonlyArray<string> | number;
export type ChangeEventProps = { index: number, selected: string[] }
export type EbaySelectProps = Omit<ComponentProps<'select'>, 'onChange'> & {
borderless?: boolean;
defaultValue?: SelectValue;
onChange?: EbayChangeEventHandler<HTMLSelectElement, ChangeEventProps>;
floatingLabel?: string;
forwardedRef?: React.Ref<HTMLSelectElement>;
inputSize?: 'default' | 'large';
invalid?: boolean;
};
const EbaySelect: FC<EbaySelectProps> = ({
value: controlledValue,
defaultValue,
className,
borderless,
name,
disabled,
onChange = () => {},
onBlur = () => {},
onFocus = () => {},
floatingLabel,
forwardedRef,
children,
inputSize,
invalid,
...rest
}) => {
const isFieldInvalid = invalid || rest['aria-invalid'] === 'true'
const [value, setValue] = useState<SelectValue>(defaultValue)
const {
label,
Container,
onBlur: onFloatingLabelBlur,
onFocus: onFloatingLabelFocus,
ref
} = useFloatingLabel({
ref: forwardedRef,
inputId: rest.id,
className: className,
disabled: disabled,
label: floatingLabel,
inputValue: controlledValue,
inputSize: inputSize,
invalid: isFieldInvalid
})
const handleSelectChange = (e: ChangeEvent<HTMLSelectElement>) => {
const { value: newValue, selectedIndex } = e.target
if (!isControlled(controlledValue)) {
setValue(newValue)
}
onChange(e, { index: selectedIndex, selected: [newValue] })
}
const handleBlur = (event: FocusEvent<HTMLSelectElement>) => {
onBlur(event)
onFloatingLabelBlur()
}
const handleFocus = (event: FocusEvent<HTMLSelectElement>) => {
onFocus(event)
onFloatingLabelFocus()
}
const selectClassName = classNames('select', className, {
'select--borderless': borderless,
'select--large': inputSize === `large`
})
return (
<Container>
{label}
<span className={selectClassName}>
<select
{...rest}
name={name}
value={isControlled(controlledValue) ? controlledValue : value}
disabled={disabled}
onChange={handleSelectChange}
onBlur={handleBlur}
onFocus={handleFocus}
ref={ref}
>
{options(children)}
</select>
<EbayIcon name="chevronDown12" height="8" width="8" />
</span>
</Container>
)
}
export default withForwardRef<EbaySelectProps>(EbaySelect)
function optionGroups(data) {
const optGroups = {}
data.forEach(opt => {
const option = opt.props
if (option.optgroup) {
if (!Object.prototype.hasOwnProperty.call(optGroups, option.optgroup)) {
optGroups[option.optgroup] = []
}
optGroups[option.optgroup].push(option)
}
})
return optGroups
}
function options(children) {
const renderedGroups = []
const allOptions = []
let optGroups = {}
let withinGroup = false
const childrenOpts = filterByType(children, EbaySelectOption).map(c => cloneElement(c, {}))
if (childrenOpts) {
optGroups = optionGroups(childrenOpts)
let currentGroupName
childrenOpts.forEach((option, idx) => {
const { value, optionClassName, children: optionChildren, optgroup } = option.props
withinGroup = optgroup && renderedGroups.indexOf(optgroup) === -1
if (withinGroup) { // This will always be true when the very first group is encountered.
currentGroupName = optgroup
const currentGroupOptions = optGroups[currentGroupName]
const opts = currentGroupOptions.map((groupOption) => (
<EbaySelectOption
key={`opt-${groupOption.value}`}
value={groupOption.value}
className={groupOption.optionClassName}>
{groupOption.children}
</EbaySelectOption>))
allOptions.push(<optgroup key={idx} label={optgroup}>{opts}</optgroup>)
renderedGroups.push(optgroup)
} else if (!optgroup) {
/**
* The check below is necessary because we could still be in a group which has already
* been added to the renderedGroups array. In that case it will be skipped.
*/
allOptions.push(
<EbaySelectOption key={idx} value={value} className={optionClassName}>
{optionChildren}
</EbaySelectOption>)
}
})
return allOptions
}
}