linkmore-design
Version:
π πlmη»δ»ΆεΊγπ
149 lines (142 loc) β’ 7.28 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["prefixCls", "bordered", "className", "getPopupContainer", "dropdownClassName", "popupClassName", "listHeight", "placement", "listItemHeight", "size", "disabled", "notFoundContent", "status", "showArrow"];
// TODO: 4.0 - codemod should help to change `filterOption` to support node props.
import classNames from 'classnames';
import RcSelect, { OptGroup, Option } from 'rc-select';
import omit from 'rc-util/lib/omit';
import * as React from 'react';
import { useContext } from 'react';
import { ConfigContext } from "../config-provider";
import defaultRenderEmpty from "../config-provider/defaultRenderEmpty";
import DisabledContext from "../config-provider/DisabledContext";
import SizeContext from "../config-provider/SizeContext";
import { FormItemInputContext } from "../form/context";
import { useCompactItemContext } from "../space/Compact";
import { getTransitionDirection, getTransitionName } from "../_util/motion";
import { getMergedStatus, getStatusClassNames } from "../_util/statusUtils";
import warning from "../_util/warning";
import getIcons from "./utils/iconUtil";
var SECRET_COMBOBOX_MODE_DO_NOT_USE = 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
var InternalSelect = function InternalSelect(_ref, ref) {
var _classNames2;
var customizePrefixCls = _ref.prefixCls,
_ref$bordered = _ref.bordered,
bordered = _ref$bordered === void 0 ? true : _ref$bordered,
className = _ref.className,
getPopupContainer = _ref.getPopupContainer,
dropdownClassName = _ref.dropdownClassName,
popupClassName = _ref.popupClassName,
_ref$listHeight = _ref.listHeight,
listHeight = _ref$listHeight === void 0 ? 256 : _ref$listHeight,
placement = _ref.placement,
_ref$listItemHeight = _ref.listItemHeight,
listItemHeight = _ref$listItemHeight === void 0 ? 24 : _ref$listItemHeight,
customizeSize = _ref.size,
customDisabled = _ref.disabled,
notFoundContent = _ref.notFoundContent,
customStatus = _ref.status,
showArrow = _ref.showArrow,
props = _objectWithoutProperties(_ref, _excluded);
var _React$useContext = React.useContext(ConfigContext),
getContextPopupContainer = _React$useContext.getPopupContainer,
getPrefixCls = _React$useContext.getPrefixCls,
renderEmpty = _React$useContext.renderEmpty,
direction = _React$useContext.direction,
virtual = _React$useContext.virtual,
dropdownMatchSelectWidth = _React$useContext.dropdownMatchSelectWidth;
var size = React.useContext(SizeContext);
var prefixCls = getPrefixCls('select', customizePrefixCls);
var rootPrefixCls = getPrefixCls();
var _useCompactItemContex = useCompactItemContext(prefixCls, direction),
compactSize = _useCompactItemContex.compactSize,
compactItemClassnames = _useCompactItemContex.compactItemClassnames;
var mode = React.useMemo(function () {
var _ref2 = props,
m = _ref2.mode;
if (m === 'combobox') {
return undefined;
}
if (m === SECRET_COMBOBOX_MODE_DO_NOT_USE) {
return 'combobox';
}
return m;
}, [props.mode]);
var isMultiple = mode === 'multiple' || mode === 'tags';
var mergedShowArrow = showArrow !== undefined ? showArrow : props.loading || !(isMultiple || mode === 'combobox');
// =================== Warning =====================
warning(!dropdownClassName, 'Select', '`dropdownClassName` is deprecated which will be removed in next major version. Please use `popupClassName` instead.');
// ===================== Form Status =====================
var _useContext = useContext(FormItemInputContext),
contextStatus = _useContext.status,
hasFeedback = _useContext.hasFeedback,
isFormItemInput = _useContext.isFormItemInput,
feedbackIcon = _useContext.feedbackIcon;
var mergedStatus = getMergedStatus(contextStatus, customStatus);
// ===================== Empty =====================
var mergedNotFound;
if (notFoundContent !== undefined) {
mergedNotFound = notFoundContent;
} else if (mode === 'combobox') {
mergedNotFound = null;
} else {
mergedNotFound = (renderEmpty || defaultRenderEmpty)('Select');
}
// ===================== Icons =====================
var _getIcons = getIcons(_objectSpread(_objectSpread({}, props), {}, {
multiple: isMultiple,
hasFeedback: hasFeedback,
feedbackIcon: feedbackIcon,
showArrow: mergedShowArrow,
prefixCls: prefixCls
})),
suffixIcon = _getIcons.suffixIcon,
itemIcon = _getIcons.itemIcon,
removeIcon = _getIcons.removeIcon,
clearIcon = _getIcons.clearIcon;
var selectProps = omit(props, ['suffixIcon', 'itemIcon']);
var rcSelectRtlDropdownClassName = classNames(popupClassName || dropdownClassName, _defineProperty({}, "".concat(prefixCls, "-dropdown-").concat(direction), direction === 'rtl'));
var mergedSize = compactSize || customizeSize || size;
// ===================== Disabled =====================
var disabled = React.useContext(DisabledContext);
var mergedDisabled = customDisabled !== null && customDisabled !== void 0 ? customDisabled : disabled;
var mergedClassName = classNames((_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-lg"), mergedSize === 'large'), _defineProperty(_classNames2, "".concat(prefixCls, "-sm"), mergedSize === 'small'), _defineProperty(_classNames2, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _defineProperty(_classNames2, "".concat(prefixCls, "-borderless"), !bordered), _defineProperty(_classNames2, "".concat(prefixCls, "-in-form-item"), isFormItemInput), _classNames2), getStatusClassNames(prefixCls, mergedStatus, hasFeedback), compactItemClassnames, className);
// ===================== Placement =====================
var getPlacement = function getPlacement() {
if (placement !== undefined) {
return placement;
}
return direction === 'rtl' ? 'bottomRight' : 'bottomLeft';
};
return /*#__PURE__*/React.createElement(RcSelect, _extends({
ref: ref,
virtual: virtual,
dropdownMatchSelectWidth: dropdownMatchSelectWidth
}, selectProps, {
transitionName: getTransitionName(rootPrefixCls, getTransitionDirection(placement), props.transitionName),
value: !props.loading ? selectProps.value : undefined,
listHeight: listHeight,
listItemHeight: listItemHeight,
mode: mode,
prefixCls: prefixCls,
placement: getPlacement(),
direction: direction,
inputIcon: suffixIcon,
menuItemSelectedIcon: itemIcon,
removeIcon: removeIcon,
clearIcon: clearIcon,
notFoundContent: mergedNotFound,
className: mergedClassName,
getPopupContainer: getPopupContainer || getContextPopupContainer,
dropdownClassName: rcSelectRtlDropdownClassName,
showArrow: hasFeedback || showArrow,
disabled: mergedDisabled
}));
};
var Select = /*#__PURE__*/React.forwardRef(InternalSelect);
Select.SECRET_COMBOBOX_MODE_DO_NOT_USE = SECRET_COMBOBOX_MODE_DO_NOT_USE;
Select.Option = Option;
Select.OptGroup = OptGroup;
export default Select;