-
Notifications
You must be signed in to change notification settings - Fork 82
/
Dropdown.js
121 lines (108 loc) · 3.33 KB
/
Dropdown.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
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
import React from 'react';
import styled from '@emotion/styled';
import { LIB_NAME } from '../constants';
import NoData from '../components/NoData';
import Item from '../components/Item';
import { valueExistInSelected, hexToRGBA, isomorphicWindow } from '../util';
const dropdownPosition = (props, methods) => {
const DropdownBoundingClientRect = methods.getSelectRef().getBoundingClientRect();
const dropdownHeight =
DropdownBoundingClientRect.bottom + parseInt(props.dropdownHeight, 10) + parseInt(props.dropdownGap, 10);
if (props.dropdownPosition !== 'auto') {
return props.dropdownPosition;
}
if (dropdownHeight > isomorphicWindow().innerHeight && dropdownHeight > DropdownBoundingClientRect.top) {
return 'top';
}
return 'bottom';
};
const Dropdown = ({ props, state, methods }) => (
<DropDown
tabIndex="-1"
aria-expanded="true"
role="list"
dropdownPosition={dropdownPosition(props, methods)}
selectBounds={state.selectBounds}
portal={props.portal}
dropdownGap={props.dropdownGap}
dropdownHeight={props.dropdownHeight}
className={`${LIB_NAME}-dropdown ${LIB_NAME}-dropdown-position-${dropdownPosition(
props,
methods
)}`}>
{props.dropdownRenderer ? (
props.dropdownRenderer({ props, state, methods })
) : (
<React.Fragment>
{props.create && state.search && !valueExistInSelected(state.search, state.values, props) && (
<AddNew
className={`${LIB_NAME}-dropdown-add-new`}
color={props.color}
onClick={() => methods.createNew(state.search)}>
{props.createNewLabel.replace('{search}', `"${state.search}"`)}
</AddNew>
)}
{methods.searchResults().length === 0 ? (
<NoData
className={`${LIB_NAME}-no-data`}
state={state}
props={props}
methods={methods}
/>
) : (
methods
.searchResults()
.map((item, itemIndex) => (
<Item
key={item[props.valueField]}
item={item}
itemIndex={itemIndex}
state={state}
props={props}
methods={methods}
/>
))
)}
</React.Fragment>
)}
</DropDown>
);
const DropDown = styled.div`
position: absolute;
${({ selectBounds, dropdownGap, dropdownPosition }) =>
dropdownPosition === 'top'
? `bottom: ${selectBounds.height + 2 + dropdownGap}px`
: `top: ${selectBounds.height + 2 + dropdownGap}px`};
${({ selectBounds, dropdownGap, portal }) =>
portal
? `
position: fixed;
top: ${selectBounds.bottom + dropdownGap}px;
left: ${selectBounds.left - 1}px;`
: 'left: -1px;'};
border: 1px solid #ccc;
width: ${({ selectBounds }) => selectBounds.width}px;
padding: 0;
display: flex;
flex-direction: column;
background: #fff;
border-radius: 2px;
box-shadow: 0 0 10px 0 ${() => hexToRGBA('#000000', 0.2)};
max-height: ${({ dropdownHeight }) => dropdownHeight};
overflow: auto;
z-index: 9;
:focus {
outline: none;
}
}
`;
const AddNew = styled.div`
color: ${({ color }) => color};
padding: 5px 10px;
:hover {
background: ${({ color }) => color && hexToRGBA(color, 0.1)};
outline: none;
cursor: pointer;
}
`;
export default Dropdown;