2020-09-30 08:21:04 +00:00
|
|
|
import Trigger from '../vc-trigger';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import { getSlot } from '../_util/props-util';
|
|
|
|
import classNames from '../_util/classNames';
|
|
|
|
import createRef from '../_util/createRef';
|
2021-12-16 09:20:18 +00:00
|
|
|
import type { CSSProperties } from 'vue';
|
2021-06-26 01:35:40 +00:00
|
|
|
import { defineComponent } from 'vue';
|
|
|
|
import type { RenderDOMFunc } from './interface';
|
|
|
|
import type { DropdownRender } from './interface/generator';
|
2021-11-03 08:22:02 +00:00
|
|
|
import type { Placement } from './generate';
|
2021-12-16 09:20:18 +00:00
|
|
|
import type { VueNode } from '../_util/type';
|
2020-09-30 08:21:04 +00:00
|
|
|
|
2020-10-03 07:54:52 +00:00
|
|
|
const getBuiltInPlacements = (dropdownMatchSelectWidth: number | boolean) => {
|
2020-09-30 08:21:04 +00:00
|
|
|
// Enable horizontal overflow auto-adjustment when a custom dropdown width is provided
|
|
|
|
const adjustX = typeof dropdownMatchSelectWidth !== 'number' ? 0 : 1;
|
|
|
|
|
|
|
|
return {
|
|
|
|
bottomLeft: {
|
|
|
|
points: ['tl', 'bl'],
|
|
|
|
offset: [0, 4],
|
|
|
|
overflow: {
|
|
|
|
adjustX,
|
|
|
|
adjustY: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
bottomRight: {
|
|
|
|
points: ['tr', 'br'],
|
|
|
|
offset: [0, 4],
|
|
|
|
overflow: {
|
|
|
|
adjustX,
|
|
|
|
adjustY: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
topLeft: {
|
|
|
|
points: ['bl', 'tl'],
|
|
|
|
offset: [0, -4],
|
|
|
|
overflow: {
|
|
|
|
adjustX,
|
|
|
|
adjustY: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
topRight: {
|
|
|
|
points: ['br', 'tr'],
|
|
|
|
offset: [0, -4],
|
|
|
|
overflow: {
|
|
|
|
adjustX,
|
|
|
|
adjustY: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
2020-10-03 07:54:52 +00:00
|
|
|
export interface SelectTriggerProps {
|
|
|
|
prefixCls: string;
|
|
|
|
disabled: boolean;
|
|
|
|
visible: boolean;
|
2021-12-16 09:20:18 +00:00
|
|
|
popupElement: VueNode;
|
2020-10-03 07:54:52 +00:00
|
|
|
animation?: string;
|
|
|
|
transitionName?: string;
|
|
|
|
containerWidth: number;
|
2021-11-03 08:22:02 +00:00
|
|
|
placement?: Placement;
|
2020-10-03 07:54:52 +00:00
|
|
|
dropdownStyle: CSSProperties;
|
|
|
|
dropdownClassName: string;
|
|
|
|
direction: string;
|
|
|
|
dropdownMatchSelectWidth?: boolean | number;
|
2020-11-23 08:39:09 +00:00
|
|
|
dropdownRender?: DropdownRender;
|
2020-10-03 07:54:52 +00:00
|
|
|
getPopupContainer?: RenderDOMFunc;
|
|
|
|
dropdownAlign: object;
|
|
|
|
empty: boolean;
|
2020-10-07 14:49:01 +00:00
|
|
|
getTriggerDOMNode: () => any;
|
2020-10-03 07:54:52 +00:00
|
|
|
}
|
2021-04-09 03:00:41 +00:00
|
|
|
|
2020-10-10 09:01:39 +00:00
|
|
|
const SelectTrigger = defineComponent<SelectTriggerProps, { popupRef: any }>({
|
2020-09-30 08:21:04 +00:00
|
|
|
name: 'SelectTrigger',
|
|
|
|
inheritAttrs: false,
|
|
|
|
created() {
|
|
|
|
this.popupRef = createRef();
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
getPopupElement() {
|
|
|
|
return this.popupRef.current;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
2020-10-03 07:54:52 +00:00
|
|
|
const { empty = false, ...props } = { ...this.$props, ...this.$attrs };
|
2020-09-30 08:21:04 +00:00
|
|
|
const {
|
|
|
|
visible,
|
|
|
|
dropdownAlign,
|
|
|
|
prefixCls,
|
|
|
|
popupElement,
|
|
|
|
dropdownClassName,
|
|
|
|
dropdownStyle,
|
2021-11-03 08:22:02 +00:00
|
|
|
direction = 'ltr',
|
|
|
|
placement,
|
2020-09-30 08:21:04 +00:00
|
|
|
dropdownMatchSelectWidth,
|
|
|
|
containerWidth,
|
|
|
|
dropdownRender,
|
2020-10-08 14:51:09 +00:00
|
|
|
animation,
|
|
|
|
transitionName,
|
2020-10-10 09:01:39 +00:00
|
|
|
getPopupContainer,
|
2021-09-25 08:51:32 +00:00
|
|
|
getTriggerDOMNode,
|
2020-10-10 05:57:37 +00:00
|
|
|
} = props as SelectTriggerProps;
|
2020-09-30 08:21:04 +00:00
|
|
|
const dropdownPrefixCls = `${prefixCls}-dropdown`;
|
|
|
|
|
|
|
|
let popupNode = popupElement;
|
|
|
|
if (dropdownRender) {
|
|
|
|
popupNode = dropdownRender({ menuNode: popupElement, props });
|
|
|
|
}
|
|
|
|
|
|
|
|
const builtInPlacements = getBuiltInPlacements(dropdownMatchSelectWidth);
|
2020-10-08 14:51:09 +00:00
|
|
|
|
|
|
|
const mergedTransitionName = animation ? `${dropdownPrefixCls}-${animation}` : transitionName;
|
|
|
|
|
2020-10-25 12:44:26 +00:00
|
|
|
const popupStyle = { minWidth: `${containerWidth}px`, ...dropdownStyle };
|
2020-09-30 08:21:04 +00:00
|
|
|
|
|
|
|
if (typeof dropdownMatchSelectWidth === 'number') {
|
|
|
|
popupStyle.width = `${dropdownMatchSelectWidth}px`;
|
|
|
|
} else if (dropdownMatchSelectWidth) {
|
|
|
|
popupStyle.width = `${containerWidth}px`;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Trigger
|
|
|
|
{...props}
|
|
|
|
showAction={[]}
|
|
|
|
hideAction={[]}
|
2021-11-03 08:22:02 +00:00
|
|
|
popupPlacement={placement || (direction === 'rtl' ? 'bottomRight' : 'bottomLeft')}
|
2020-09-30 08:21:04 +00:00
|
|
|
builtinPlacements={builtInPlacements}
|
|
|
|
prefixCls={dropdownPrefixCls}
|
2020-10-08 14:51:09 +00:00
|
|
|
popupTransitionName={mergedTransitionName}
|
2020-09-30 08:21:04 +00:00
|
|
|
popup={<div ref={this.popupRef}>{popupNode}</div>}
|
|
|
|
popupAlign={dropdownAlign}
|
|
|
|
popupVisible={visible}
|
2020-10-10 09:01:39 +00:00
|
|
|
getPopupContainer={getPopupContainer}
|
2020-09-30 08:21:04 +00:00
|
|
|
popupClassName={classNames(dropdownClassName, {
|
|
|
|
[`${dropdownPrefixCls}-empty`]: empty,
|
|
|
|
})}
|
|
|
|
popupStyle={popupStyle}
|
2021-09-25 08:51:32 +00:00
|
|
|
getTriggerDOMNode={getTriggerDOMNode}
|
2020-09-30 08:21:04 +00:00
|
|
|
>
|
|
|
|
{getSlot(this)[0]}
|
|
|
|
</Trigger>
|
|
|
|
);
|
|
|
|
},
|
2020-10-03 07:54:52 +00:00
|
|
|
});
|
2021-04-09 03:00:41 +00:00
|
|
|
|
2020-10-03 07:54:52 +00:00
|
|
|
SelectTrigger.props = {
|
|
|
|
dropdownAlign: PropTypes.object,
|
2020-10-11 13:31:57 +00:00
|
|
|
visible: PropTypes.looseBool,
|
|
|
|
disabled: PropTypes.looseBool,
|
2020-10-03 07:54:52 +00:00
|
|
|
dropdownClassName: PropTypes.string,
|
|
|
|
dropdownStyle: PropTypes.object,
|
2021-11-03 08:22:02 +00:00
|
|
|
placement: PropTypes.string,
|
2020-10-11 13:31:57 +00:00
|
|
|
empty: PropTypes.looseBool,
|
2020-10-03 07:54:52 +00:00
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
popupClassName: PropTypes.string,
|
|
|
|
animation: PropTypes.string,
|
|
|
|
transitionName: PropTypes.string,
|
|
|
|
getPopupContainer: PropTypes.func,
|
|
|
|
dropdownRender: PropTypes.func,
|
|
|
|
containerWidth: PropTypes.number,
|
|
|
|
dropdownMatchSelectWidth: PropTypes.oneOfType([Number, Boolean]).def(true),
|
|
|
|
popupElement: PropTypes.any,
|
|
|
|
direction: PropTypes.string,
|
|
|
|
getTriggerDOMNode: PropTypes.func,
|
2020-09-30 08:21:04 +00:00
|
|
|
};
|
2021-04-09 03:00:41 +00:00
|
|
|
|
2020-10-03 07:54:52 +00:00
|
|
|
export default SelectTrigger;
|