UNPKG

4.3 kBJavaScriptView Raw
1import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
4import * as React from 'react';
5import Trigger from 'rc-trigger';
6import classNames from 'classnames';
7
8var getBuiltInPlacements = function getBuiltInPlacements(dropdownMatchSelectWidth) {
9 // Enable horizontal overflow auto-adjustment when a custom dropdown width is provided
10 var adjustX = typeof dropdownMatchSelectWidth !== 'number' ? 0 : 1;
11 return {
12 bottomLeft: {
13 points: ['tl', 'bl'],
14 offset: [0, 4],
15 overflow: {
16 adjustX: adjustX,
17 adjustY: 1
18 }
19 },
20 bottomRight: {
21 points: ['tr', 'br'],
22 offset: [0, 4],
23 overflow: {
24 adjustX: adjustX,
25 adjustY: 1
26 }
27 },
28 topLeft: {
29 points: ['bl', 'tl'],
30 offset: [0, -4],
31 overflow: {
32 adjustX: adjustX,
33 adjustY: 1
34 }
35 },
36 topRight: {
37 points: ['br', 'tr'],
38 offset: [0, -4],
39 overflow: {
40 adjustX: adjustX,
41 adjustY: 1
42 }
43 }
44 };
45};
46
47var SelectTrigger = function SelectTrigger(props, ref) {
48 var prefixCls = props.prefixCls,
49 disabled = props.disabled,
50 visible = props.visible,
51 children = props.children,
52 popupElement = props.popupElement,
53 containerWidth = props.containerWidth,
54 animation = props.animation,
55 transitionName = props.transitionName,
56 dropdownStyle = props.dropdownStyle,
57 dropdownClassName = props.dropdownClassName,
58 _props$direction = props.direction,
59 direction = _props$direction === void 0 ? 'ltr' : _props$direction,
60 _props$dropdownMatchS = props.dropdownMatchSelectWidth,
61 dropdownMatchSelectWidth = _props$dropdownMatchS === void 0 ? true : _props$dropdownMatchS,
62 dropdownRender = props.dropdownRender,
63 dropdownAlign = props.dropdownAlign,
64 getPopupContainer = props.getPopupContainer,
65 empty = props.empty,
66 getTriggerDOMNode = props.getTriggerDOMNode,
67 restProps = _objectWithoutProperties(props, ["prefixCls", "disabled", "visible", "children", "popupElement", "containerWidth", "animation", "transitionName", "dropdownStyle", "dropdownClassName", "direction", "dropdownMatchSelectWidth", "dropdownRender", "dropdownAlign", "getPopupContainer", "empty", "getTriggerDOMNode"]);
68
69 var dropdownPrefixCls = "".concat(prefixCls, "-dropdown");
70 var popupNode = popupElement;
71
72 if (dropdownRender) {
73 popupNode = dropdownRender(popupElement);
74 }
75
76 var builtInPlacements = React.useMemo(function () {
77 return getBuiltInPlacements(dropdownMatchSelectWidth);
78 }, [dropdownMatchSelectWidth]); // ===================== Motion ======================
79
80 var mergedTransitionName = animation ? "".concat(dropdownPrefixCls, "-").concat(animation) : transitionName; // ======================= Ref =======================
81
82 var popupRef = React.useRef(null);
83 React.useImperativeHandle(ref, function () {
84 return {
85 getPopupElement: function getPopupElement() {
86 return popupRef.current;
87 }
88 };
89 });
90
91 var popupStyle = _objectSpread({
92 minWidth: containerWidth
93 }, dropdownStyle);
94
95 if (typeof dropdownMatchSelectWidth === 'number') {
96 popupStyle.width = dropdownMatchSelectWidth;
97 } else if (dropdownMatchSelectWidth) {
98 popupStyle.width = containerWidth;
99 }
100
101 return React.createElement(Trigger, Object.assign({}, restProps, {
102 showAction: [],
103 hideAction: [],
104 popupPlacement: direction === 'rtl' ? 'bottomRight' : 'bottomLeft',
105 builtinPlacements: builtInPlacements,
106 prefixCls: dropdownPrefixCls,
107 popupTransitionName: mergedTransitionName,
108 popup: React.createElement("div", {
109 ref: popupRef
110 }, popupNode),
111 popupAlign: dropdownAlign,
112 popupVisible: visible,
113 getPopupContainer: getPopupContainer,
114 popupClassName: classNames(dropdownClassName, _defineProperty({}, "".concat(dropdownPrefixCls, "-empty"), empty)),
115 popupStyle: popupStyle,
116 getTriggerDOMNode: getTriggerDOMNode
117 }), children);
118};
119
120var RefSelectTrigger = React.forwardRef(SelectTrigger);
121RefSelectTrigger.displayName = 'SelectTrigger';
122export default RefSelectTrigger;
\No newline at end of file