-
Notifications
You must be signed in to change notification settings - Fork 27
/
FilterPopup.tsx
200 lines (183 loc) · 5.33 KB
/
FilterPopup.tsx
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import React, {
Dispatch,
FunctionComponent,
Fragment,
ReactNode,
useRef,
useMemo,
SetStateAction,
} from "react";
import styled from "@emotion/styled";
import { some } from "lodash";
import { Popup } from "semantic-ui-react";
import ChevronDownIcon from "../../Shared/ChevronDownIcon";
import { strings } from "../../../assets/LocalizedStrings";
import "@mozilla-protocol/core/protocol/css/protocol.css";
const TriggerButton = styled.button({
display: "flex",
justifyContent: "space-between",
alignItems: "center",
width: "100%",
"& > svg": {
marginLeft: 24,
width: 16,
height: 16,
},
});
TriggerButton.displayName = "TriggerButton";
const StyledPopup = styled(Popup)({
// limit the width of the popup
minWidth: "300px !important",
maxWidth: "100% !important",
boxShadow: "none !important",
border: "2px solid #9595a2 !important",
});
StyledPopup.displayName = "StyledPopup";
const ContentContainer = styled(Popup.Content)({
// add scrollbar if the content is too long
flexGrow: 1,
overflowY: "auto",
paddingLeft: "10px",
paddingRight: "10px",
});
ContentContainer.displayName = "ContentContainer";
const PopupContainer = styled.div({
// limit the height of the popup
maxHeight: "45vh",
display: "flex",
flexDirection: "column",
});
PopupContainer.displayName = "PopupContainer";
const ButtonContainer = styled.div({
// evenly space the clear and save buttons
display: "flex",
justifyContent: "space-between",
padding: ".833em 0 0",
});
ButtonContainer.displayName = "ButtonContainer";
export interface FilterPopupProps {
/**The name of the filter. */
name: string;
/**Callback to reset the filter state. */
clear(): void;
/**Get the text representation of the filter state. */
getTextRep(): string;
/**Whether the filter state is active. */
isActive(): boolean;
/**Whether the filter popup is open. */
popupIsOpen: boolean;
/**React Dispatch callback to update the popupIsOpen state. */
setPopupIsOpen: Dispatch<SetStateAction<boolean>>;
/**Callback to handle filter popup closing. */
handlePopupClose?(): void;
/**Whether or not the popup should close when a value is selected. */
closeOnChange: boolean;
/**React Child Node. One of the filter components such as SelectDateRange, SelectTextFilterOptions, SelectSorting. */
children: ReactNode;
/**At least one option is selected regarding the filter? */
hasRequiredError?: boolean;
/**The number of selected options exceeded the allowed limit of selected options? */
hasLimitError?: boolean;
/**The number of allowed selected options. */
limit?: number;
}
/**
* Display a button to trigger the opening of filter popup containing filtering React components such as
* SelectDateRange, SelectTextFilterOptions, SelectSorting.
*/
const FilterPopup: FunctionComponent<FilterPopupProps> = ({
name,
clear,
getTextRep,
isActive,
popupIsOpen,
setPopupIsOpen,
handlePopupClose,
closeOnChange,
children,
hasRequiredError,
hasLimitError,
limit,
}: FilterPopupProps) => {
const mountNodeRef = useRef<HTMLDivElement>(null);
const hasError = some([hasRequiredError, hasLimitError]);
const errors = useMemo(() => {
const errors: string[] = [];
if (hasRequiredError) {
errors.push(`Please select at least one ${name.toLowerCase()}.`);
}
if (hasLimitError) {
errors.push(`Please select only ${limit} or fewer ${name.toLowerCase()}s.`);
}
return errors;
}, [hasRequiredError, hasLimitError, name, limit]);
const togglePopupIsOpen = () => {
setPopupIsOpen((prev) => !prev);
};
const onPopupClose = () => {
setPopupIsOpen(false);
if (!hasError && handlePopupClose) {
handlePopupClose();
}
};
const onClearFilter = () => {
clear();
};
return (
<Fragment>
<StyledPopup
basic
flowing
context={mountNodeRef.current || undefined}
on="click"
open={popupIsOpen}
pinned={true}
offset={[0, -5]}
position="bottom left"
positionFixed={true}
trigger={
<TriggerButton
className="mzp-c-button mzp-t-lg mzp-t-neutral"
onClick={togglePopupIsOpen}
>
{getTextRep()}
<ChevronDownIcon />
</TriggerButton>
}
>
<PopupContainer>
<ContentContainer>{children}</ContentContainer>
{hasError && (
<div className="mzp-c-form-errors" style={{ margin: "24px 0" }}>
<ul className="mzp-u-list-styled">
{errors.map((error) => (
<li key={error}>{error}</li>
))}
</ul>
</div>
)}
{!closeOnChange && (
<ButtonContainer>
<button
className="mzp-c-button mzp-t-md mzp-t-neutral"
disabled={!isActive()}
onClick={onClearFilter}
>
{strings.clear}
</button>
<button
className="mzp-c-button mzp-t-md mzp-t-product"
disabled={hasError}
onClick={onPopupClose}
>
{strings.save}
</button>
</ButtonContainer>
)}
</PopupContainer>
</StyledPopup>
<div ref={mountNodeRef} />
</Fragment>
);
};
export default FilterPopup;