/
Dropdown.tsx
237 lines (211 loc) · 6.87 KB
/
Dropdown.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import classNames from 'classnames';
import PropTypes from 'prop-types';
import * as React from 'react';
import { useContext, useMemo } from 'react';
import BaseDropdown, {
DropdownProps as BaseDropdownProps,
ToggleMetadata,
} from '@restart/ui/Dropdown';
import { useUncontrolled } from 'uncontrollable';
import useEventCallback from '@restart/hooks/useEventCallback';
import DropdownContext, { DropDirection } from './DropdownContext';
import DropdownItem from './DropdownItem';
import DropdownMenu, { getDropdownMenuPlacement } from './DropdownMenu';
import DropdownToggle from './DropdownToggle';
import InputGroupContext from './InputGroupContext';
import { useBootstrapPrefix, useIsRTL } from './ThemeProvider';
import createWithBsPrefix from './createWithBsPrefix';
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
import { AlignType, alignPropType } from './types';
const DropdownHeader = createWithBsPrefix('dropdown-header', {
defaultProps: { role: 'heading' },
});
const DropdownDivider = createWithBsPrefix('dropdown-divider', {
Component: 'hr',
defaultProps: { role: 'separator' },
});
const DropdownItemText = createWithBsPrefix('dropdown-item-text', {
Component: 'span',
});
export interface DropdownProps
extends BaseDropdownProps,
BsPrefixProps,
Omit<React.HTMLAttributes<HTMLElement>, 'onSelect' | 'children'> {
drop?: DropDirection;
align?: AlignType;
focusFirstItemOnShow?: boolean | 'keyboard';
navbar?: boolean;
autoClose?: boolean | 'outside' | 'inside';
}
const propTypes = {
/** @default 'dropdown' */
bsPrefix: PropTypes.string,
/**
* Determines the direction and location of the Menu in relation to it's Toggle.
*/
drop: PropTypes.oneOf<DropDirection>([
'up',
'up-centered',
'start',
'end',
'down',
'down-centered',
]),
as: PropTypes.elementType,
/**
* Aligns the dropdown menu to the specified side of the Dropdown toggle. You can
* also align the menu responsively for breakpoints starting at `sm` and up.
* The alignment direction will affect the specified breakpoint or larger.
*
* *Note: Using responsive alignment will disable Popper usage for positioning.*
*
* @type {"start"|"end"|{ sm: "start"|"end" }|{ md: "start"|"end" }|{ lg: "start"|"end" }|{ xl: "start"|"end"}|{ xxl: "start"|"end"} }
*/
align: alignPropType,
/**
* Whether or not the Dropdown is visible.
*
* @controllable onToggle
*/
show: PropTypes.bool,
/**
* A callback fired when the Dropdown wishes to change visibility. Called with the requested
* `show` value, the DOM event, and the source that fired it: `'click'`,`'keydown'`,`'rootClose'`, or `'select'`.
*
* ```js
* function(
* nextShow: boolean,
* meta: ToggleMetadata,
* ): void
* ```
*
* @controllable show
*/
onToggle: PropTypes.func,
/**
* A callback fired when a menu item is selected.
*
* ```js
* (eventKey: any, event: Object) => any
* ```
*/
onSelect: PropTypes.func,
/**
* Controls the focus behavior for when the Dropdown is opened. Set to
* `true` to always focus the first menu item, `keyboard` to focus only when
* navigating via the keyboard, or `false` to disable completely
*
* The Default behavior is `false` **unless** the Menu has a `role="menu"`
* where it will default to `keyboard` to match the recommended [ARIA Authoring practices](https://www.w3.org/TR/wai-aria-practices-1.1/#menubutton).
*/
focusFirstItemOnShow: PropTypes.oneOf([false, true, 'keyboard']),
/** @private */
navbar: PropTypes.bool,
/**
* Controls the auto close behaviour of the dropdown when clicking outside of
* the button or the list.
*/
autoClose: PropTypes.oneOf([true, 'outside', 'inside', false]),
};
const defaultProps: Partial<DropdownProps> = {
navbar: false,
align: 'start',
autoClose: true,
drop: 'down',
};
const Dropdown: BsPrefixRefForwardingComponent<'div', DropdownProps> =
React.forwardRef<HTMLElement, DropdownProps>((pProps, ref) => {
const {
bsPrefix,
drop,
show,
className,
align,
onSelect,
onToggle,
focusFirstItemOnShow,
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
as: Component = 'div',
navbar: _4,
autoClose,
...props
} = useUncontrolled(pProps, { show: 'onToggle' });
const isInputGroup = useContext(InputGroupContext);
const prefix = useBootstrapPrefix(bsPrefix, 'dropdown');
const isRTL = useIsRTL();
const isClosingPermitted = (source: string): boolean => {
// autoClose=false only permits close on button click
if (autoClose === false) return source === 'click';
// autoClose=inside doesn't permit close on rootClose
if (autoClose === 'inside') return source !== 'rootClose';
// autoClose=outside doesn't permit close on select
if (autoClose === 'outside') return source !== 'select';
return true;
};
const handleToggle = useEventCallback(
(nextShow: boolean, meta: ToggleMetadata) => {
if (
meta.originalEvent!.currentTarget === document &&
(meta.source !== 'keydown' ||
(meta.originalEvent as any).key === 'Escape')
)
meta.source = 'rootClose';
if (isClosingPermitted(meta.source!)) onToggle?.(nextShow, meta);
},
);
const alignEnd = align === 'end';
const placement = getDropdownMenuPlacement(alignEnd, drop, isRTL);
const contextValue = useMemo(
() => ({
align,
drop,
isRTL,
}),
[align, drop, isRTL],
);
const directionClasses = {
down: prefix,
'down-centered': `${prefix}-center`,
up: 'dropup',
'up-centered': 'dropup-center dropup',
end: 'dropend',
start: 'dropstart',
};
return (
<DropdownContext.Provider value={contextValue}>
<BaseDropdown
placement={placement}
show={show}
onSelect={onSelect}
onToggle={handleToggle}
focusFirstItemOnShow={focusFirstItemOnShow}
itemSelector={`.${prefix}-item:not(.disabled):not(:disabled)`}
>
{isInputGroup ? (
props.children
) : (
<Component
{...props}
ref={ref}
className={classNames(
className,
show && 'show',
directionClasses[drop!],
)}
/>
)}
</BaseDropdown>
</DropdownContext.Provider>
);
});
Dropdown.displayName = 'Dropdown';
Dropdown.propTypes = propTypes;
Dropdown.defaultProps = defaultProps;
export default Object.assign(Dropdown, {
Toggle: DropdownToggle,
Menu: DropdownMenu,
Item: DropdownItem,
ItemText: DropdownItemText,
Divider: DropdownDivider,
Header: DropdownHeader,
});