-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdropdown.tsx
More file actions
executable file
·103 lines (91 loc) · 2.75 KB
/
Copy pathdropdown.tsx
File metadata and controls
executable file
·103 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
95
96
97
98
99
100
101
102
103
import React, { useCallback, useContext, useState } from 'react';
import classNames from 'classnames';
import { hasMarker, MENU_MARK } from '../_utils/component-markers';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
import { DropdownProps } from './types';
import { MenuProps } from '../menu/types';
import Popup from '../popup';
const Dropdown = (props: DropdownProps): JSX.Element => {
const {
trigger = 'hover',
placement = 'bottom-start',
disabled = false,
arrow = false,
onVisibleChange,
overlay,
className,
children,
prefixCls: customisedCls,
...otherProps
} = props;
const configContext = useContext(ConfigContext);
const prefixCls = getPrefixCls('dropdown', configContext.prefixCls, customisedCls);
const cls = classNames(prefixCls, className);
const isControlled = 'visible' in props;
const [uncontrolledVisible, setUncontrolledVisible] = useState(false);
const popupVisible = isControlled ? Boolean(props.visible) : uncontrolledVisible;
const setPopupVisibleState = useCallback(
(nextVisible: boolean): void => {
if (!isControlled) {
setUncontrolledVisible(nextVisible);
}
onVisibleChange?.(nextVisible);
},
[isControlled, onVisibleChange]
);
const renderOverlay = (): React.ReactNode => {
if (!overlay) {
return null;
}
const originalOnSelect = overlay.props.onSelect;
const isMenuOverlay =
hasMarker(overlay.type, MENU_MARK);
if (!isMenuOverlay) {
return overlay;
}
const overlayProps: Partial<MenuProps> = {
overlayClassName: cls,
appearance: 'dropdown',
mode: 'vertical',
selectedKeys: [],
onSelect: (selectedIndex, info) => {
originalOnSelect?.(selectedIndex, info);
if (!originalOnSelect) {
setPopupVisibleState(false);
}
},
};
return React.cloneElement(overlay, overlayProps);
};
const handleKeyDown = (e: React.KeyboardEvent) => {
children.props.onKeyDown?.(e);
if (e.key === 'Escape' && popupVisible) {
setPopupVisibleState(false);
}
};
const childrenProps = {
onKeyDown: handleKeyDown,
'aria-expanded': popupVisible,
'aria-haspopup': true as const,
};
React.Children.only(children);
return (
<Popup
{...otherProps}
biZoom={false}
arrow={arrow}
disabled={disabled}
placement={placement}
flip={!('placement' in props)}
trigger={trigger}
className={cls}
visible={popupVisible}
onVisibleChange={setPopupVisibleState}
content={renderOverlay()}>
{React.cloneElement(children, childrenProps)}
</Popup>
);
};
Dropdown.displayName = 'Dropdown';
export default Dropdown;