-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
60 lines (53 loc) · 1.43 KB
/
index.js
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
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import {
DropdownContainer,
SelectedOption,
Options,
Option,
ArrowIcon
} from './style';
const Dropdown = ({ options, input, ...rest }) => {
const [dropdownOpen, toggleDropdown] = useState(false);
const [selectedValue, updateSelectedValue] = useState(input.value);
const optionKeys = Object.keys(options);
useEffect(() => {
input.onChange(selectedValue);
});
return (
<DropdownContainer onClick={() => toggleDropdown(!dropdownOpen)} {...rest}>
<SelectedOption>
{options[selectedValue]}
<ArrowIcon open={dropdownOpen} />
</SelectedOption>
<Options visible={dropdownOpen}>
{optionKeys.map(key => {
return (
<Option
onClick={() => updateSelectedValue(key)}
key={key}
active={key === selectedValue}
>
{options[key]}
</Option>
);
})}
</Options>
</DropdownContainer>
);
};
Dropdown.propTypes = {
// This totally breaks react - [name] is undefined
// options: PropTypes.shape({
// [name]: PropTypes.string
// }).isRequired,
options: PropTypes.object,
input: PropTypes.shape({
name: PropTypes.string,
onBlur: PropTypes.func,
onChange: PropTypes.func,
onFocus: PropTypes.func,
value: PropTypes.string.isRequired
})
};
export default Dropdown;