forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiAction.tsx
More file actions
94 lines (91 loc) · 2.75 KB
/
MultiAction.tsx
File metadata and controls
94 lines (91 loc) · 2.75 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
import React, { memo, cloneElement, Children } from 'react';
import GoChevronDown from 'react-icons/lib/go/chevron-down';
import GoChevronUp from 'react-icons/lib/go/chevron-up';
import { useMenuState, MenuStateReturn } from 'reakit/Menu';
import {
Container,
PrimaryAction,
ToggleActionsList,
ActionsList,
SecondaryAction,
} from './elements';
interface IMultiActionProps {
Icon?: any;
primaryActionLabel: string;
onPrimaryClick?: (event: React.MouseEvent, menu: MenuStateReturn) => void;
disablePrimary?: boolean;
small?: boolean;
block?: boolean;
disabled?: boolean;
light?: boolean;
secondary?: boolean;
red?: boolean;
danger?: boolean;
className?: string;
children: React.ReactElement | React.ReactElement[];
}
export const MultiAction: React.FC<IMultiActionProps> = memo(
({
Icon,
primaryActionLabel,
onPrimaryClick,
disablePrimary = false,
small = false,
block = false,
disabled = false,
secondary = false,
red = false,
danger = false,
children,
className,
}) => {
const menu = useMenuState();
const buttonProps = { small, block, disabled, secondary, red, danger };
return (
<Container>
{/*
// @ts-ignore */}
<PrimaryAction
onClick={e => onPrimaryClick(e, menu)}
className={className}
{...buttonProps}
disabled={disablePrimary || disabled}
>
{Icon && <Icon />}
{primaryActionLabel}
</PrimaryAction>
<ToggleActionsList {...menu} {...buttonProps} className={className}>
{menu.visible ? <GoChevronUp /> : <GoChevronDown />}
</ToggleActionsList>
<ActionsList {...menu} aria-label="Additional Options">
{children && (children as React.ReactElement[]).length
? /* eslint-disable react/no-array-index-key */
(children as React.ReactElement[]).map((child, i) => (
<SecondaryAction
key={i}
{...menu}
{...(child.props || {})}
onClick={e => {
if (child.props.onClick) {
child.props.onClick(e, menu);
}
}}
>
{itemProps => cloneElement(Children.only(child), itemProps)}
</SecondaryAction>
))
: !Array.isArray(children) && (
<SecondaryAction
{...menu}
{...((children as React.ReactElement).props || {})}
>
{itemProps =>
cloneElement(Children.only(children), itemProps)
}
</SecondaryAction>
)}
</ActionsList>
</Container>
);
}
);