-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.tsx
88 lines (85 loc) · 1.95 KB
/
index.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
import { FaCaretDown } from "@react-icons/all-files/fa/FaCaretDown";
import { FaCaretUp } from "@react-icons/all-files/fa/FaCaretUp";
import cx from "classnames";
import React, { ReactNode } from "react";
import Popup, { PopupProps } from "../Popup";
import css from "./index.module.css";
export type Props = Partial<PopupProps> & {
children: ReactNode;
isOpen?: boolean;
setIsOpen?: (o: boolean) => void;
triggerText?: string;
buttonClassName?: string;
["data-cy"]?: string;
};
export default function ButtonWithPopup({
children,
isOpen,
setIsOpen,
triggerText,
...props
}: Props) {
const openProps: Partial<PopupProps> =
isOpen !== undefined && setIsOpen !== undefined
? {
open: isOpen,
onOpen: () => setIsOpen(true),
onClose: () => setIsOpen(false),
}
: {};
return (
<Popup
{...openProps}
position="bottom right"
on="click"
offsetX={triggerText ? 32 : 0}
contentStyle={{ width: "10rem" }}
closeOnDocumentClick
trigger={open =>
getTriggerButton(
open,
triggerText,
props.buttonClassName,
props["data-cy"],
)
}
// props must come last to override default props above
{...props}
>
{children}
</Popup>
);
}
function getTriggerButton(
open: boolean,
triggerText?: string,
buttonClassName?: string,
dataCy?: string,
): JSX.Element {
return (
<button
type="button"
className={cx(
css.triggerButton,
{ [css.withoutText]: !triggerText },
buttonClassName,
)}
data-cy={dataCy}
>
{triggerText}
{open ? (
<FaCaretUp
className={cx(css.caret, {
[css.caretWithoutText]: !triggerText,
})}
/>
) : (
<FaCaretDown
className={cx(css.caret, {
[css.caretWithoutText]: !triggerText,
})}
/>
)}
</button>
);
}