-
-
Notifications
You must be signed in to change notification settings - Fork 489
Expand file tree
/
Copy pathSelectTrigger.tsx
More file actions
220 lines (195 loc) · 5.92 KB
/
SelectTrigger.tsx
File metadata and controls
220 lines (195 loc) · 5.92 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
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
import Trigger, { type TriggerRef } from '@rc-component/trigger';
import type { AlignType, BuildInPlacements } from '@rc-component/trigger/lib/interface';
import { clsx } from 'clsx';
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;
visible: 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;
/** Get dynamic popup offset based on input element for textarea cursor positioning */
getPopupOffset?: (inputElement: HTMLElement) => [number, number] | null;
empty: boolean;
onPopupVisibleChange?: (visible: boolean) => void;
onPopupMouseEnter: () => void;
onPopupMouseDown: React.MouseEventHandler<HTMLDivElement>;
onPopupBlur?: React.FocusEventHandler<HTMLDivElement>;
}
const SelectTrigger: React.ForwardRefRenderFunction<RefTriggerProps, SelectTriggerProps> = (
props,
ref,
) => {
const {
prefixCls,
disabled,
visible,
children,
popupElement,
animation,
transitionName,
popupStyle,
popupClassName,
direction = 'ltr',
placement,
builtinPlacements,
popupMatchSelectWidth,
popupRender,
popupAlign,
getPopupOffset,
getPopupContainer,
empty,
onPopupVisibleChange,
onPopupMouseEnter,
onPopupMouseDown,
onPopupBlur,
...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(() => {
const base = builtinPlacements || getBuiltInPlacements(popupMatchSelectWidth);
if (!getPopupOffset) {
return base;
}
// Apply dynamic offset to placements
const dynamicPlacements: Record<string, AlignType> = {};
Object.keys(base).forEach((key) => {
const placement = base[key];
dynamicPlacements[key] = {
...placement,
offset: (node: HTMLElement) => {
// Get the offset from getPopupOffset callback
const customOffset = getPopupOffset(node);
if (customOffset) {
return customOffset;
}
// Default offset from base placement
return placement.offset as [number, number];
},
};
});
return dynamicPlacements;
}, [builtinPlacements, popupMatchSelectWidth, getPopupOffset]);
// ===================== 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={onPopupVisibleChange ? ['click'] : []}
hideAction={onPopupVisibleChange ? ['click'] : []}
popupPlacement={placement || (direction === 'rtl' ? 'bottomRight' : 'bottomLeft')}
builtinPlacements={mergedBuiltinPlacements}
prefixCls={popupPrefixCls}
popupMotion={{ motionName: mergedTransitionName }}
popup={
<div onMouseEnter={onPopupMouseEnter} onMouseDown={onPopupMouseDown} onBlur={onPopupBlur}>
{popupNode}
</div>
}
ref={triggerPopupRef}
stretch={stretch}
popupAlign={popupAlign}
popupVisible={visible}
getPopupContainer={getPopupContainer}
popupClassName={clsx(popupClassName, { [`${popupPrefixCls}-empty`]: empty })}
popupStyle={mergedPopupStyle}
onPopupVisibleChange={onPopupVisibleChange}
>
{children}
</Trigger>
);
};
const RefSelectTrigger = React.forwardRef<RefTriggerProps, SelectTriggerProps>(SelectTrigger);
if (process.env.NODE_ENV !== 'production') {
RefSelectTrigger.displayName = 'SelectTrigger';
}
export default RefSelectTrigger;