-
-
Notifications
You must be signed in to change notification settings - Fork 489
Expand file tree
/
Copy pathSelectTrigger.tsx
More file actions
191 lines (169 loc) · 4.91 KB
/
SelectTrigger.tsx
File metadata and controls
191 lines (169 loc) · 4.91 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
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
import Trigger, { type TriggerRef } from '@rc-component/trigger';
import type { AlignType, BuildInPlacements } from '@rc-component/trigger/lib/interface';
import classNames from 'classnames';
import * as React from 'react';
import type { Placement, RenderDOMFunc } from './BaseSelect';
const getBuiltInPlacements = (
popupMatchSelectWidth: number | boolean,
): Record<string, AlignType> => {
// Enable horizontal overflow auto-adjustment when a custom dropdown width is provided
const adjustX = popupMatchSelectWidth === true ? 0 : 1;
return {
bottomLeft: {
points: ['tl', 'bl'],
offset: [0, 4],
overflow: {
adjustX,
adjustY: 1,
},
htmlRegion: 'scroll',
},
bottomRight: {
points: ['tr', 'br'],
offset: [0, 4],
overflow: {
adjustX,
adjustY: 1,
},
htmlRegion: 'scroll',
},
topLeft: {
points: ['bl', 'tl'],
offset: [0, -4],
overflow: {
adjustX,
adjustY: 1,
},
htmlRegion: 'scroll',
},
topRight: {
points: ['br', 'tr'],
offset: [0, -4],
overflow: {
adjustX,
adjustY: 1,
},
htmlRegion: 'scroll',
},
};
};
export interface RefTriggerProps {
getPopupElement: () => HTMLDivElement;
}
export interface SelectTriggerProps {
prefixCls: string;
children: React.ReactElement;
disabled: boolean;
open: boolean;
popupElement: React.ReactElement;
animation?: string;
transitionName?: string;
placement?: Placement;
builtinPlacements?: BuildInPlacements;
popupStyle: React.CSSProperties;
popupClassName: string;
direction: string;
popupMatchSelectWidth?: boolean | number;
popupRender?: (menu: React.ReactElement) => React.ReactElement;
getPopupContainer?: RenderDOMFunc;
popupAlign: AlignType;
empty: boolean;
getTriggerDOMNode: (node: HTMLElement) => HTMLElement;
onOpenChange?: (open: boolean) => void;
onPopupMouseEnter: () => void;
}
const SelectTrigger: React.ForwardRefRenderFunction<RefTriggerProps, SelectTriggerProps> = (
props,
ref,
) => {
const {
prefixCls,
disabled,
open,
children,
popupElement,
animation,
transitionName,
popupStyle,
popupClassName,
direction = 'ltr',
placement,
builtinPlacements,
popupMatchSelectWidth,
popupRender,
popupAlign,
getPopupContainer,
empty,
getTriggerDOMNode,
onOpenChange,
onPopupMouseEnter,
...restProps
} = props;
// We still use `dropdown` className to keep compatibility
// This is used for:
// 1. Styles
// 2. Animation
// 3. Theme customization
// Please do not modify this since it's a breaking change
const popupPrefixCls = `${prefixCls}-dropdown`;
let popupNode = popupElement;
if (popupRender) {
popupNode = popupRender(popupElement);
}
const mergedBuiltinPlacements = React.useMemo(
() => builtinPlacements || getBuiltInPlacements(popupMatchSelectWidth),
[builtinPlacements, popupMatchSelectWidth],
);
// ===================== Motion ======================
const mergedTransitionName = animation ? `${popupPrefixCls}-${animation}` : transitionName;
// =================== Popup Width ===================
const isNumberPopupWidth = typeof popupMatchSelectWidth === 'number';
const stretch = React.useMemo(() => {
if (isNumberPopupWidth) {
return null;
}
return popupMatchSelectWidth === false ? 'minWidth' : 'width';
}, [popupMatchSelectWidth, isNumberPopupWidth]);
let mergedPopupStyle = popupStyle;
if (isNumberPopupWidth) {
mergedPopupStyle = {
...popupStyle,
width: popupMatchSelectWidth,
};
}
// ======================= Ref =======================
const triggerPopupRef = React.useRef<TriggerRef>(null);
React.useImperativeHandle(ref, () => ({
getPopupElement: () => triggerPopupRef.current?.popupElement,
}));
return (
<Trigger
{...restProps}
showAction={onOpenChange ? ['click'] : []}
hideAction={onOpenChange ? ['click'] : []}
popupPlacement={placement || (direction === 'rtl' ? 'bottomRight' : 'bottomLeft')}
builtinPlacements={mergedBuiltinPlacements}
prefixCls={popupPrefixCls}
popupMotion={{ motionName: mergedTransitionName }}
popup={<div onMouseEnter={onPopupMouseEnter}>{popupNode}</div>}
ref={triggerPopupRef}
stretch={stretch}
popupAlign={popupAlign}
popupVisible={open}
getPopupContainer={getPopupContainer}
popupClassName={classNames(popupClassName, {
[`${popupPrefixCls}-empty`]: empty,
})}
popupStyle={mergedPopupStyle}
getTriggerDOMNode={getTriggerDOMNode}
onOpenChange={onOpenChange}
>
{children}
</Trigger>
);
};
const RefSelectTrigger = React.forwardRef<RefTriggerProps, SelectTriggerProps>(SelectTrigger);
if (process.env.NODE_ENV !== 'production') {
RefSelectTrigger.displayName = 'SelectTrigger';
}
export default RefSelectTrigger;