-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
NavDropdown.tsx
124 lines (107 loc) · 3.21 KB
/
NavDropdown.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
import classNames from 'classnames';
import * as React from 'react';
import PropTypes from 'prop-types';
import { useBootstrapPrefix } from './ThemeProvider';
import Dropdown, { DropdownProps } from './Dropdown';
import { DropdownMenuVariant } from './DropdownMenu';
import NavLink from './NavLink';
import { BsPrefixRefForwardingComponent } from './helpers';
export interface NavDropdownProps extends Omit<DropdownProps, 'title'> {
title: React.ReactNode;
disabled?: boolean;
active?: boolean;
menuRole?: string;
renderMenuOnMount?: boolean;
rootCloseEvent?: 'click' | 'mousedown';
menuVariant?: DropdownMenuVariant;
}
const propTypes = {
/**
* An html id attribute for the Toggle button, necessary for assistive technologies, such as screen readers.
* @type {string}
*/
id: 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 the toggle NavLink */
disabled: PropTypes.bool,
/** Style the toggle NavLink as active */
active: PropTypes.bool,
/** 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](#menu-props) for more details_
*/
rootCloseEvent: PropTypes.string,
/**
* Menu color variant.
*
* Omitting this will use the default light color.
*/
menuVariant: PropTypes.oneOf<DropdownMenuVariant>(['dark']),
/** @ignore */
bsPrefix: PropTypes.string,
};
const NavDropdown: BsPrefixRefForwardingComponent<'div', NavDropdownProps> =
React.forwardRef(
(
{
id,
title,
children,
bsPrefix,
className,
rootCloseEvent,
menuRole,
disabled,
active,
renderMenuOnMount,
menuVariant,
...props
}: NavDropdownProps,
ref,
) => {
/* NavItem has no additional logic, it's purely presentational. Can set nav item class here to support "as" */
const navItemPrefix = useBootstrapPrefix(undefined, 'nav-item');
return (
<Dropdown
ref={ref}
{...props}
className={classNames(className, navItemPrefix)}
>
<Dropdown.Toggle
id={id}
eventKey={null}
active={active}
disabled={disabled}
childBsPrefix={bsPrefix}
as={NavLink}
>
{title}
</Dropdown.Toggle>
<Dropdown.Menu
role={menuRole}
renderOnMount={renderMenuOnMount}
rootCloseEvent={rootCloseEvent}
variant={menuVariant}
>
{children}
</Dropdown.Menu>
</Dropdown>
);
},
);
NavDropdown.displayName = 'NavDropdown';
NavDropdown.propTypes = propTypes;
export default Object.assign(NavDropdown, {
Item: Dropdown.Item,
ItemText: Dropdown.ItemText,
Divider: Dropdown.Divider,
Header: Dropdown.Header,
});