UNPKG

7.9 kBJavaScriptView Raw
1import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2import _extends from "@babel/runtime/helpers/esm/extends";
3import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
4// TODO: 4.0 - codemod should help to change `filterOption` to support node props.
5var __rest = this && this.__rest || function (s, e) {
6 var t = {};
7 for (var p in s) {
8 if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
9 }
10 if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
11 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
12 }
13 return t;
14};
15import classNames from 'classnames';
16import RcSelect, { OptGroup, Option } from 'rc-select';
17import omit from "rc-util/es/omit";
18import * as React from 'react';
19import { useContext } from 'react';
20import { ConfigContext } from '../config-provider';
21import defaultRenderEmpty from '../config-provider/defaultRenderEmpty';
22import DisabledContext from '../config-provider/DisabledContext';
23import SizeContext from '../config-provider/SizeContext';
24import { FormItemInputContext } from '../form/context';
25import { getTransitionDirection, getTransitionName } from '../_util/motion';
26import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
27import getIcons from './utils/iconUtil';
28import useStyle from './style';
29import genPurePanel from '../_util/PurePanel';
30import warning from '../_util/warning';
31import { useCompactItemContext } from '../space/Compact';
32var SECRET_COMBOBOX_MODE_DO_NOT_USE = 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
33var InternalSelect = function InternalSelect(_a, ref) {
34 var _classNames2;
35 var customizePrefixCls = _a.prefixCls,
36 _a$bordered = _a.bordered,
37 bordered = _a$bordered === void 0 ? true : _a$bordered,
38 className = _a.className,
39 getPopupContainer = _a.getPopupContainer,
40 popupClassName = _a.popupClassName,
41 dropdownClassName = _a.dropdownClassName,
42 _a$listHeight = _a.listHeight,
43 listHeight = _a$listHeight === void 0 ? 256 : _a$listHeight,
44 placement = _a.placement,
45 _a$listItemHeight = _a.listItemHeight,
46 listItemHeight = _a$listItemHeight === void 0 ? 24 : _a$listItemHeight,
47 customizeSize = _a.size,
48 customDisabled = _a.disabled,
49 notFoundContent = _a.notFoundContent,
50 customStatus = _a.status,
51 showArrow = _a.showArrow,
52 props = __rest(_a, ["prefixCls", "bordered", "className", "getPopupContainer", "popupClassName", "dropdownClassName", "listHeight", "placement", "listItemHeight", "size", "disabled", "notFoundContent", "status", "showArrow"]);
53 var _React$useContext = React.useContext(ConfigContext),
54 getContextPopupContainer = _React$useContext.getPopupContainer,
55 getPrefixCls = _React$useContext.getPrefixCls,
56 renderEmpty = _React$useContext.renderEmpty,
57 direction = _React$useContext.direction,
58 virtual = _React$useContext.virtual,
59 dropdownMatchSelectWidth = _React$useContext.dropdownMatchSelectWidth;
60 var size = React.useContext(SizeContext);
61 var prefixCls = getPrefixCls('select', customizePrefixCls);
62 var rootPrefixCls = getPrefixCls();
63 var _useCompactItemContex = useCompactItemContext(prefixCls, direction),
64 compactSize = _useCompactItemContex.compactSize,
65 compactItemClassnames = _useCompactItemContex.compactItemClassnames;
66 var _useStyle = useStyle(prefixCls),
67 _useStyle2 = _slicedToArray(_useStyle, 2),
68 wrapSSR = _useStyle2[0],
69 hashId = _useStyle2[1];
70 var mode = React.useMemo(function () {
71 var m = props.mode;
72 if (m === 'combobox') {
73 return undefined;
74 }
75 if (m === SECRET_COMBOBOX_MODE_DO_NOT_USE) {
76 return 'combobox';
77 }
78 return m;
79 }, [props.mode]);
80 var isMultiple = mode === 'multiple' || mode === 'tags';
81 var mergedShowArrow = showArrow !== undefined ? showArrow : props.loading || !(isMultiple || mode === 'combobox');
82 // ===================== Form Status =====================
83 var _useContext = useContext(FormItemInputContext),
84 contextStatus = _useContext.status,
85 hasFeedback = _useContext.hasFeedback,
86 isFormItemInput = _useContext.isFormItemInput,
87 feedbackIcon = _useContext.feedbackIcon;
88 var mergedStatus = getMergedStatus(contextStatus, customStatus);
89 // ===================== Empty =====================
90 var mergedNotFound;
91 if (notFoundContent !== undefined) {
92 mergedNotFound = notFoundContent;
93 } else if (mode === 'combobox') {
94 mergedNotFound = null;
95 } else {
96 mergedNotFound = (renderEmpty || defaultRenderEmpty)('Select');
97 }
98 // ===================== Icons =====================
99 var _getIcons = getIcons(_extends(_extends({}, props), {
100 multiple: isMultiple,
101 hasFeedback: hasFeedback,
102 feedbackIcon: feedbackIcon,
103 showArrow: mergedShowArrow,
104 prefixCls: prefixCls
105 })),
106 suffixIcon = _getIcons.suffixIcon,
107 itemIcon = _getIcons.itemIcon,
108 removeIcon = _getIcons.removeIcon,
109 clearIcon = _getIcons.clearIcon;
110 var selectProps = omit(props, ['suffixIcon', 'itemIcon']);
111 var rcSelectRtlDropdownClassName = classNames(popupClassName || dropdownClassName, _defineProperty({}, prefixCls + "-dropdown-" + direction, direction === 'rtl'), hashId);
112 var mergedSize = compactSize || customizeSize || size;
113 // ===================== Disabled =====================
114 var disabled = React.useContext(DisabledContext);
115 var mergedDisabled = customDisabled !== null && customDisabled !== void 0 ? customDisabled : disabled;
116 var mergedClassName = classNames((_classNames2 = {}, _defineProperty(_classNames2, prefixCls + "-lg", mergedSize === 'large'), _defineProperty(_classNames2, prefixCls + "-sm", mergedSize === 'small'), _defineProperty(_classNames2, prefixCls + "-rtl", direction === 'rtl'), _defineProperty(_classNames2, prefixCls + "-borderless", !bordered), _defineProperty(_classNames2, prefixCls + "-in-form-item", isFormItemInput), _classNames2), getStatusClassNames(prefixCls, mergedStatus, hasFeedback), compactItemClassnames, className, hashId);
117 // ===================== Placement =====================
118 var getPlacement = function getPlacement() {
119 if (placement !== undefined) {
120 return placement;
121 }
122 return direction === 'rtl' ? 'bottomRight' : 'bottomLeft';
123 };
124 // ====================== Warning ======================
125 if (process.env.NODE_ENV !== 'production') {
126 process.env.NODE_ENV !== "production" ? warning(!dropdownClassName, 'Select', '`dropdownClassName` is deprecated. Please use `popupClassName` instead.') : void 0;
127 }
128 // ====================== Render =======================
129 return wrapSSR( /*#__PURE__*/React.createElement(RcSelect, _extends({
130 ref: ref,
131 virtual: virtual,
132 dropdownMatchSelectWidth: dropdownMatchSelectWidth
133 }, selectProps, {
134 transitionName: getTransitionName(rootPrefixCls, getTransitionDirection(placement), props.transitionName),
135 listHeight: listHeight,
136 listItemHeight: listItemHeight,
137 mode: mode,
138 prefixCls: prefixCls,
139 placement: getPlacement(),
140 direction: direction,
141 inputIcon: suffixIcon,
142 menuItemSelectedIcon: itemIcon,
143 removeIcon: removeIcon,
144 clearIcon: clearIcon,
145 notFoundContent: mergedNotFound,
146 className: mergedClassName,
147 getPopupContainer: getPopupContainer || getContextPopupContainer,
148 dropdownClassName: rcSelectRtlDropdownClassName,
149 showArrow: hasFeedback || showArrow,
150 disabled: mergedDisabled
151 })));
152};
153var Select = /*#__PURE__*/React.forwardRef(InternalSelect);
154// We don't care debug panel
155/* istanbul ignore next */
156var PurePanel = genPurePanel(Select);
157Select.SECRET_COMBOBOX_MODE_DO_NOT_USE = SECRET_COMBOBOX_MODE_DO_NOT_USE;
158Select.Option = Option;
159Select.OptGroup = OptGroup;
160Select._InternalPanelDoNotUseOrYouWillBeFired = PurePanel;
161export default Select;
\No newline at end of file