-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathDropdownButton.tsx
More file actions
144 lines (127 loc) · 3.83 KB
/
DropdownButton.tsx
File metadata and controls
144 lines (127 loc) · 3.83 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
import * as React from 'react';
import PropTypes from 'prop-types';
import Dropdown, { DropdownProps } from './Dropdown';
import DropdownToggle, { PropsFromToggle } from './DropdownToggle';
import DropdownMenu, { DropdownMenuVariant } from './DropdownMenu';
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
import { alignPropType } from './types';
export interface DropdownButtonProps
extends Omit<DropdownProps, 'title'>,
PropsFromToggle,
BsPrefixProps {
title: React.ReactNode;
menuRole?: string;
renderMenuOnMount?: boolean;
rootCloseEvent?: 'click' | 'mousedown';
menuVariant?: DropdownMenuVariant;
flip?: boolean;
}
const propTypes = {
/**
* An html id attribute for the Toggle button, necessary for assistive technologies, such as screen readers.
* @type {string}
*/
id: PropTypes.string,
/** An `href` passed to the Toggle component */
href: PropTypes.string,
/** An `onClick` handler passed to the Toggle component */
onClick: PropTypes.func,
/** The content of the non-toggle Button. */
title: PropTypes.node.isRequired,
/** Disables both Buttons */
disabled: PropTypes.bool,
/**
* Aligns the dropdown menu.
*
* _see [DropdownMenu](#dropdown-menu-props) for more details_
*
* @type {"start"|"end"|{ sm: "start"|"end" }|{ md: "start"|"end" }|{ lg: "start"|"end" }|{ xl: "start"|"end"}|{ xxl: "start"|"end"} }
*/
align: alignPropType,
/** An ARIA accessible role applied to the Menu component. When set to 'menu', The dropdown */
menuRole: PropTypes.string,
/** Whether to render the dropdown menu in the DOM before the first time it is shown */
renderMenuOnMount: PropTypes.bool,
/**
* Which event when fired outside the component will cause it to be closed.
*
* _see [DropdownMenu](#dropdown-menu-props) for more details_
*/
rootCloseEvent: PropTypes.string,
/**
* Menu color variant.
*
* Omitting this will use the default light color.
*/
menuVariant: PropTypes.oneOf<DropdownMenuVariant>(['dark']),
/**
* Allow Dropdown to flip in case of an overlapping on the reference element. For more information refer to
* Popper.js's flip [docs](https://popper.js.org/docs/v2/modifiers/flip/).
*
*/
flip: PropTypes.bool,
/** @ignore */
bsPrefix: PropTypes.string,
/** @ignore */
variant: PropTypes.string,
/** @ignore */
size: PropTypes.string,
};
/**
* A convenience component for simple or general use dropdowns. Renders a `Button` toggle and all `children`
* are passed directly to the default `Dropdown.Menu`. This component accepts all of
* [`Dropdown`'s props](#dropdown-props).
*
* _All unknown props are passed through to the `Dropdown` component._ Only
* the Button `variant`, `size` and `bsPrefix` props are passed to the toggle,
* along with menu-related props are passed to the `Dropdown.Menu`
*/
const DropdownButton: BsPrefixRefForwardingComponent<
'div',
DropdownButtonProps
> = React.forwardRef<HTMLDivElement, DropdownButtonProps>(
(
{
title,
children,
bsPrefix,
rootCloseEvent,
variant,
size,
menuRole,
renderMenuOnMount,
disabled,
href,
id,
menuVariant,
flip,
...props
},
ref,
) => (
<Dropdown ref={ref} {...props}>
<DropdownToggle
id={id}
href={href}
size={size}
variant={variant}
disabled={disabled}
childBsPrefix={bsPrefix}
>
{title}
</DropdownToggle>
<DropdownMenu
role={menuRole}
renderOnMount={renderMenuOnMount}
rootCloseEvent={rootCloseEvent}
variant={menuVariant}
flip={flip}
>
{children}
</DropdownMenu>
</Dropdown>
),
);
DropdownButton.displayName = 'DropdownButton';
DropdownButton.propTypes = propTypes;
export default DropdownButton;