-
-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathSelectNative.tsx
More file actions
85 lines (75 loc) · 2.13 KB
/
Copy pathSelectNative.tsx
File metadata and controls
85 lines (75 loc) · 2.13 KB
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
import React, { forwardRef, useCallback } from 'react';
import { noOp } from '../common/utils';
import { StyledInner, StyledNativeSelect } from './Select.styles';
import { SelectCommonProps } from './Select.types';
import { useSelectCommon } from './useSelectCommon';
type SelectNativeProps = SelectCommonProps<string> &
Omit<
React.SelectHTMLAttributes<HTMLSelectElement>,
'defaultValue' | 'name' | 'onChange' | 'style' | 'value'
>;
const SelectNative = forwardRef<HTMLSelectElement, SelectNativeProps>(
(
{
className,
defaultValue,
disabled,
onChange,
options: optionsProp,
readOnly,
style,
value: valueProp,
variant,
width,
...otherProps
},
ref
) => {
const { isEnabled, options, setValue, value, DropdownButton, Wrapper } =
useSelectCommon<string>({
defaultValue,
disabled,
native: true,
onChange,
options: optionsProp,
readOnly,
value: valueProp,
variant
});
const handleChange = useCallback(
(event: React.ChangeEvent<HTMLSelectElement>) => {
const selectedOption = options.find(
option => option.value === event.target.value
);
if (!selectedOption) {
return;
}
setValue(selectedOption.value);
onChange?.(selectedOption, { fromEvent: event });
},
[onChange, options, setValue]
);
return (
<Wrapper className={className} style={{ ...style, width }}>
<StyledInner>
<StyledNativeSelect
{...otherProps}
disabled={disabled}
onChange={isEnabled ? handleChange : noOp}
ref={ref}
value={value}
>
{options.map((option, index) => (
<option key={`${option.value}-${index}`} value={option.value}>
{option.label ?? option.value}
</option>
))}
</StyledNativeSelect>
{DropdownButton}
</StyledInner>
</Wrapper>
);
}
);
SelectNative.displayName = 'SelectNative';
export { SelectNative, SelectNativeProps };