bee-select
Version:
select ui component for react
196 lines (178 loc) • 5.38 kB
JavaScript
/**
* This source code is quoted from rc-select.
* homepage: https://github.com/react-component/select
*/
import Trigger from 'bee-overlay/build/trigger';
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import ReactDOM from 'react-dom';
import { isSingleMode, saveRef } from './util';
import DropdownMenu from './DropdownMenu';
Trigger.displayName = 'Trigger';
const BUILT_IN_PLACEMENTS = {
bottomLeft: {
points: ['tl', 'bl'],
offset: [0, 4],
overflow: {
adjustX: 0,
adjustY: 1,
},
},
topLeft: {
points: ['bl', 'tl'],
offset: [0, -4],
overflow: {
adjustX: 0,
adjustY: 1,
},
},
};
export default class SelectTrigger extends React.Component {
static propTypes = {
onPopupFocus: PropTypes.func,
onPopupScroll: PropTypes.func,
dropdownMatchSelectWidth: PropTypes.bool,
dropdownAlign: PropTypes.object,
visible: PropTypes.bool,
disabled: PropTypes.bool,
showSearch: PropTypes.bool,
dropdownClassName: PropTypes.string,
multiple: PropTypes.bool,
inputValue: PropTypes.string,
filterOption: PropTypes.any,
options: PropTypes.any,
prefixCls: PropTypes.string,
popupClassName: PropTypes.string,
children: PropTypes.any,
showAction: PropTypes.arrayOf(PropTypes.string),
menuItemSelectedIcon: PropTypes.oneOfType([
PropTypes.func,
PropTypes.node,
]),
};
constructor(props) {
super(props);
this.saveDropdownMenuRef = saveRef(this, 'dropdownMenuRef');
this.saveTriggerRef = saveRef(this, 'triggerRef');
this.state = {
dropdownWidth: null,
};
}
componentDidMount() {
if(this.props.open){//宽度计算时机修改
this.setDropdownWidth();
}
}
componentDidUpdate() {
if(this.props.visible){//宽度计算时机修改
this.setDropdownWidth();
}
}
setDropdownWidth = () => {
if (!this.props.dropdownMatchSelectWidth) {
return;
}
const width = ReactDOM.findDOMNode(this).offsetWidth;
if (width !== this.state.dropdownWidth) {
this.setState({ dropdownWidth: width });
}
}
getInnerMenu = () => {
return this.dropdownMenuRef && this.dropdownMenuRef.menuRef;
};
getPopupDOMNode = () => {
return this.triggerRef.getPopupDomNode();
};
getDropdownElement = newProps => {
const props = this.props;
return (
<DropdownMenu
ref={this.saveDropdownMenuRef}
{...newProps}
clsPrefix={this.getDropdownPrefixCls()}
onMenuSelect={props.onMenuSelect}
onMenuDeselect={props.onMenuDeselect}
onPopupScroll={props.onPopupScroll}
value={props.value}
backfillValue={props.backfillValue}
firstActiveValue={props.firstActiveValue}
defaultActiveFirstOption={props.defaultActiveFirstOption}
dropdownMenuStyle={props.dropdownMenuStyle}
menuItemSelectedIcon={props.menuItemSelectedIcon}
/>
);
};
getDropdownTransitionName = () => {
const props = this.props;
let transitionName = props.transitionName;
if (!transitionName && props.animation) {
transitionName = `${this.getDropdownPrefixCls()}-${props.animation}`;
}
return transitionName;
};
getDropdownPrefixCls = () => {
return `${this.props.prefixCls}-dropdown`;
};
render() {
const { onPopupFocus, ...props } = this.props;
const {
multiple,
visible,
inputValue,
dropdownAlign,
disabled,
showSearch,
dropdownClassName,
dropdownStyle,
dropdownMatchSelectWidth,
} = props;
const dropdownPrefixCls = this.getDropdownPrefixCls();
const popupClassName = {
[dropdownClassName]: !!dropdownClassName,
[`${dropdownPrefixCls}--${multiple ? 'multiple' : 'single'}`]: 1,
};
const popupElement = this.getDropdownElement({
menuItems: props.options,
onPopupFocus,
multiple,
inputValue,
visible,
});
let hideAction;
if (disabled) {
hideAction = [];
} else if (isSingleMode(props) && !showSearch) {
hideAction = ['click'];
} else {
hideAction = ['blur'];
}
const popupStyle = { ...dropdownStyle };
const widthProp = dropdownMatchSelectWidth ? 'width' : 'minWidth';
if (this.state.dropdownWidth) {
popupStyle[widthProp] = `${this.state.dropdownWidth}px`;
}
return (
<Trigger
{...props}
showAction={disabled ? [] : this.props.showAction}
hideAction={hideAction}
ref={this.saveTriggerRef}
popupPlacement="bottomLeft"
builtinPlacements={BUILT_IN_PLACEMENTS}
prefixCls={dropdownPrefixCls}
popupTransitionName={this.getDropdownTransitionName()}
onPopupVisibleChange={props.onDropdownVisibleChange}
popup={popupElement}
popupAlign={dropdownAlign}
popupVisible={visible}
getPopupContainer={props.getPopupContainer}
popupClassName={classnames(popupClassName)}
popupStyle={popupStyle}
>
{props.children}
</Trigger>
);
}
}
SelectTrigger.displayName = 'SelectTrigger';