-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathdropdown.tsx
More file actions
155 lines (136 loc) · 3.97 KB
/
dropdown.tsx
File metadata and controls
155 lines (136 loc) · 3.97 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
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
/**
* A generic dropdown component. It takes the children of the component
* and hosts it in the component. When the component is selected, it
* drops-down the contentComponent and applies the contentProps.
*/
import React, { useEffect, useRef, useState } from "react";
import { useDidUpdateEffect } from "../hooks/use-did-update-effect";
import { useKey } from "../hooks/use-key";
import { useMultiSelect } from "../hooks/use-multi-select";
import { KEY } from "../lib/constants";
import SelectPanel from "../select-panel";
import { Cross } from "../select-panel/cross";
import { Arrow } from "./arrow";
import { DropdownHeader } from "./header";
import { Loading } from "./loading";
const Dropdown = () => {
const {
t,
onMenuToggle,
ArrowRenderer,
shouldToggleOnHover,
isLoading,
disabled,
onChange,
onBlur,
labelledBy,
value,
isOpen,
defaultIsOpen,
ClearSelectedIcon,
closeOnChangedValue,
} = useMultiSelect();
useEffect(() => {
if (closeOnChangedValue) {
setExpanded(false);
}
}, [value]);
const [isInternalExpand, setIsInternalExpand] = useState(true);
const [expanded, setExpanded] = useState(defaultIsOpen);
const [hasFocus, setHasFocus] = useState(false);
const FinalArrow = ArrowRenderer || Arrow;
const wrapper: any = useRef();
useDidUpdateEffect(() => {
onMenuToggle && onMenuToggle(expanded);
}, [expanded]);
useEffect(() => {
if (defaultIsOpen === undefined && typeof isOpen === "boolean") {
setIsInternalExpand(false);
setExpanded(isOpen);
}
}, [isOpen]);
const handleKeyDown = (e) => {
// allows space and enter when focused on input/button
if (
["text", "button"].includes(e.target.type) &&
[KEY.SPACE, KEY.ENTER].includes(e.code)
) {
return;
}
if (isInternalExpand) {
if (e.code === KEY.ESCAPE) {
setExpanded(false);
wrapper?.current?.focus();
} else {
setExpanded(true);
}
}
e.preventDefault();
};
useKey([KEY.ENTER, KEY.ARROW_DOWN, KEY.SPACE, KEY.ESCAPE], handleKeyDown, {
target: wrapper,
});
const handleHover = (iexpanded: boolean) => {
isInternalExpand && shouldToggleOnHover && setExpanded(iexpanded);
};
const handleFocus = () => !hasFocus && setHasFocus(true);
const handleBlur = (e) => {
if (!e.currentTarget.contains(e.relatedTarget) && isInternalExpand) {
setHasFocus(false);
setExpanded(false);
onBlur?.(e);
}
};
const handleMouseEnter = () => handleHover(true);
const handleMouseLeave = () => handleHover(false);
const toggleExpanded = () => {
isInternalExpand && setExpanded(isLoading || disabled ? false : !expanded);
};
const handleClearSelected = (e) => {
e.stopPropagation();
onChange([]);
isInternalExpand && setExpanded(false);
};
return (
<div
tabIndex={0}
className="dropdown-container"
aria-labelledby={labelledBy}
aria-expanded={expanded}
aria-readonly={true}
aria-disabled={disabled}
ref={wrapper}
onFocus={handleFocus}
onBlur={handleBlur}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
<div className="dropdown-heading" onClick={toggleExpanded}>
<div className="dropdown-heading-value">
<DropdownHeader />
</div>
{isLoading && <Loading />}
{value.length > 0 && ClearSelectedIcon !== null && (
<button
type="button"
className="clear-selected-button"
onClick={handleClearSelected}
disabled={disabled}
aria-label={t("clearSelected")}
>
{ClearSelectedIcon || <Cross />}
</button>
)}
<FinalArrow expanded={expanded} />
</div>
{expanded && (
<div className="dropdown-content">
<div className="panel-content">
<SelectPanel />
</div>
</div>
)}
</div>
);
};
export default Dropdown;