|
|
@ -8,6 +8,7 @@ import spring from 'react-motion/lib/spring';
|
|
|
|
import detectPassiveEvents from 'detect-passive-events';
|
|
|
|
import detectPassiveEvents from 'detect-passive-events';
|
|
|
|
|
|
|
|
|
|
|
|
const listenerOptions = detectPassiveEvents.hasSupport ? { passive: true } : false;
|
|
|
|
const listenerOptions = detectPassiveEvents.hasSupport ? { passive: true } : false;
|
|
|
|
|
|
|
|
let id = 0;
|
|
|
|
|
|
|
|
|
|
|
|
class DropdownMenu extends React.PureComponent {
|
|
|
|
class DropdownMenu extends React.PureComponent {
|
|
|
|
|
|
|
|
|
|
|
@ -29,6 +30,10 @@ class DropdownMenu extends React.PureComponent {
|
|
|
|
placement: 'bottom',
|
|
|
|
placement: 'bottom',
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
state = {
|
|
|
|
|
|
|
|
mounted: false,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
handleDocumentClick = e => {
|
|
|
|
handleDocumentClick = e => {
|
|
|
|
if (this.node && !this.node.contains(e.target)) {
|
|
|
|
if (this.node && !this.node.contains(e.target)) {
|
|
|
|
this.props.onClose();
|
|
|
|
this.props.onClose();
|
|
|
@ -38,6 +43,7 @@ class DropdownMenu extends React.PureComponent {
|
|
|
|
componentDidMount () {
|
|
|
|
componentDidMount () {
|
|
|
|
document.addEventListener('click', this.handleDocumentClick, false);
|
|
|
|
document.addEventListener('click', this.handleDocumentClick, false);
|
|
|
|
document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
|
|
|
|
document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
|
|
|
|
|
|
|
|
this.setState({ mounted: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
|
|
|
componentWillUnmount () {
|
|
|
@ -82,11 +88,15 @@ class DropdownMenu extends React.PureComponent {
|
|
|
|
|
|
|
|
|
|
|
|
render () {
|
|
|
|
render () {
|
|
|
|
const { items, style, placement, arrowOffsetLeft, arrowOffsetTop } = this.props;
|
|
|
|
const { items, style, placement, arrowOffsetLeft, arrowOffsetTop } = this.props;
|
|
|
|
|
|
|
|
const { mounted } = this.state;
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<Motion defaultStyle={{ opacity: 0, scaleX: 0.85, scaleY: 0.75 }} style={{ opacity: spring(1, { damping: 35, stiffness: 400 }), scaleX: spring(1, { damping: 35, stiffness: 400 }), scaleY: spring(1, { damping: 35, stiffness: 400 }) }}>
|
|
|
|
<Motion defaultStyle={{ opacity: 0, scaleX: 0.85, scaleY: 0.75 }} style={{ opacity: spring(1, { damping: 35, stiffness: 400 }), scaleX: spring(1, { damping: 35, stiffness: 400 }), scaleY: spring(1, { damping: 35, stiffness: 400 }) }}>
|
|
|
|
{({ opacity, scaleX, scaleY }) => (
|
|
|
|
{({ opacity, scaleX, scaleY }) => (
|
|
|
|
<div className='dropdown-menu' style={{ ...style, opacity: opacity, transform: `scale(${scaleX}, ${scaleY})` }} ref={this.setRef}>
|
|
|
|
// It should not be transformed when mounting because the resulting
|
|
|
|
|
|
|
|
// size will be used to determine the coordinate of the menu by
|
|
|
|
|
|
|
|
// react-overlays
|
|
|
|
|
|
|
|
<div className='dropdown-menu' style={{ ...style, opacity: opacity, transform: mounted ? `scale(${scaleX}, ${scaleY})` : null }} ref={this.setRef}>
|
|
|
|
<div className={`dropdown-menu__arrow ${placement}`} style={{ left: arrowOffsetLeft, top: arrowOffsetTop }} />
|
|
|
|
<div className={`dropdown-menu__arrow ${placement}`} style={{ left: arrowOffsetLeft, top: arrowOffsetTop }} />
|
|
|
|
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
<ul>
|
|
|
@ -115,8 +125,10 @@ export default class Dropdown extends React.PureComponent {
|
|
|
|
status: ImmutablePropTypes.map,
|
|
|
|
status: ImmutablePropTypes.map,
|
|
|
|
isUserTouching: PropTypes.func,
|
|
|
|
isUserTouching: PropTypes.func,
|
|
|
|
isModalOpen: PropTypes.bool.isRequired,
|
|
|
|
isModalOpen: PropTypes.bool.isRequired,
|
|
|
|
onModalOpen: PropTypes.func,
|
|
|
|
onOpen: PropTypes.func.isRequired,
|
|
|
|
onModalClose: PropTypes.func,
|
|
|
|
onClose: PropTypes.func.isRequired,
|
|
|
|
|
|
|
|
dropdownPlacement: PropTypes.string,
|
|
|
|
|
|
|
|
openDropdownId: PropTypes.number,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
static defaultProps = {
|
|
|
@ -124,42 +136,28 @@ export default class Dropdown extends React.PureComponent {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
state = {
|
|
|
|
state = {
|
|
|
|
expanded: false,
|
|
|
|
id: id++,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
handleClick = () => {
|
|
|
|
handleClick = ({ target }) => {
|
|
|
|
if (!this.state.expanded && this.props.isUserTouching() && this.props.onModalOpen) {
|
|
|
|
if (this.state.id === this.props.openDropdownId) {
|
|
|
|
const { status, items } = this.props;
|
|
|
|
this.handleClose();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
this.props.onModalOpen({
|
|
|
|
const { top } = target.getBoundingClientRect();
|
|
|
|
status,
|
|
|
|
const placement = top * 2 < innerHeight ? 'bottom' : 'top';
|
|
|
|
actions: items.map(
|
|
|
|
|
|
|
|
(item, i) => item ? {
|
|
|
|
|
|
|
|
...item,
|
|
|
|
|
|
|
|
name: `${item.text}-${i}`,
|
|
|
|
|
|
|
|
onClick: this.handleItemClick.bind(this, i),
|
|
|
|
|
|
|
|
} : null
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.setState({ expanded: !this.state.expanded });
|
|
|
|
this.props.onOpen(this.state.id, this.handleItemClick, placement);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
handleClose = () => {
|
|
|
|
handleClose = () => {
|
|
|
|
if (this.props.onModalClose) {
|
|
|
|
this.props.onClose(this.state.id);
|
|
|
|
this.props.onModalClose();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.setState({ expanded: false });
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
handleKeyDown = e => {
|
|
|
|
handleKeyDown = e => {
|
|
|
|
switch(e.key) {
|
|
|
|
switch(e.key) {
|
|
|
|
case 'Enter':
|
|
|
|
case 'Enter':
|
|
|
|
this.handleClick();
|
|
|
|
this.handleClick(e);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 'Escape':
|
|
|
|
case 'Escape':
|
|
|
|
this.handleClose();
|
|
|
|
this.handleClose();
|
|
|
@ -190,22 +188,22 @@ export default class Dropdown extends React.PureComponent {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
render () {
|
|
|
|
render () {
|
|
|
|
const { icon, items, size, ariaLabel, disabled } = this.props;
|
|
|
|
const { icon, items, size, ariaLabel, disabled, dropdownPlacement, openDropdownId } = this.props;
|
|
|
|
const { expanded } = this.state;
|
|
|
|
const open = this.state.id === openDropdownId;
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<div onKeyDown={this.handleKeyDown}>
|
|
|
|
<div onKeyDown={this.handleKeyDown}>
|
|
|
|
<IconButton
|
|
|
|
<IconButton
|
|
|
|
icon={icon}
|
|
|
|
icon={icon}
|
|
|
|
title={ariaLabel}
|
|
|
|
title={ariaLabel}
|
|
|
|
active={expanded}
|
|
|
|
active={open}
|
|
|
|
disabled={disabled}
|
|
|
|
disabled={disabled}
|
|
|
|
size={size}
|
|
|
|
size={size}
|
|
|
|
ref={this.setTargetRef}
|
|
|
|
ref={this.setTargetRef}
|
|
|
|
onClick={this.handleClick}
|
|
|
|
onClick={this.handleClick}
|
|
|
|
/>
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
<Overlay show={expanded} placement='bottom' target={this.findTarget}>
|
|
|
|
<Overlay show={open} placement={dropdownPlacement} target={this.findTarget}>
|
|
|
|
<DropdownMenu items={items} onClose={this.handleClose} />
|
|
|
|
<DropdownMenu items={items} onClose={this.handleClose} />
|
|
|
|
</Overlay>
|
|
|
|
</Overlay>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|