-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (57 loc) · 1.5 KB
/
index.js
File metadata and controls
65 lines (57 loc) · 1.5 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
import React from 'react'
import PropTypes from 'prop-types'
import mainStyles from '../../styles.module.css'
const Dropdown = ({ value, label, field, data, placeholder, onChange, isMulti }) => {
const handleChange = (event) => {
if (isMulti) {
const { options } = event.target
const value = []
for (let i = 0; i < options.length; i++) {
if (options[i].selected) value.push(options[i].value)
}
onChange(value, field)
} else {
const { value } = event.target
onChange(value, field)
}
}
return (
<div className={mainStyles.formGroup}>
{label && <label htmlFor='app-dropdown-field'>{label}</label>}
<select
value={value}
className={mainStyles.formControl}
onChange={handleChange}
multiple={isMulti}
>
{
!isMulti &&
<option value=''>{placeholder ? placeholder : 'Select a value'}</option>
}
{data.map((item, key) => (
<option key={key} value={item.value}>
{item.label}
</option>
))}
</select>
</div>
)
}
Dropdown.propTypes = {
value: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
placeholder: PropTypes.string,
data: PropTypes.array,
label: PropTypes.string,
field: PropTypes.string,
onChange: PropTypes.func.isRequired,
isMulti: PropTypes.bool
}
Dropdown.defaultProps = {
value: '',
placeholder: '',
label: '',
field: '',
data: [],
isMulti: false
}
export default Dropdown