|
|
@ -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 {
|
|
|
|
|
|
|
|
|
|
|
@ -124,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 = {
|
|
|
@ -133,43 +136,28 @@ export default class Dropdown extends React.PureComponent {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
state = {
|
|
|
|
state = {
|
|
|
|
placement: null,
|
|
|
|
id: id++,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
handleClick = ({ target }) => {
|
|
|
|
handleClick = ({ target }) => {
|
|
|
|
if (this.state.placement) {
|
|
|
|
if (this.state.id === this.props.openDropdownId) {
|
|
|
|
this.setState({ placement: null });
|
|
|
|
this.handleClose();
|
|
|
|
} else if (this.props.isUserTouching() && this.props.onModalOpen) {
|
|
|
|
|
|
|
|
const { status, items } = this.props;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.props.onModalOpen({
|
|
|
|
|
|
|
|
status,
|
|
|
|
|
|
|
|
actions: items.map(
|
|
|
|
|
|
|
|
(item, i) => item ? {
|
|
|
|
|
|
|
|
...item,
|
|
|
|
|
|
|
|
name: `${item.text}-${i}`,
|
|
|
|
|
|
|
|
onClick: this.handleItemClick.bind(this, i),
|
|
|
|
|
|
|
|
} : null
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
const { top } = target.getBoundingClientRect();
|
|
|
|
const { top } = target.getBoundingClientRect();
|
|
|
|
this.setState({ placement: top * 2 < innerHeight ? 'bottom' : 'top' });
|
|
|
|
const placement = top * 2 < innerHeight ? 'bottom' : 'top';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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({ placement: null });
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
@ -200,23 +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 { placement } = this.state;
|
|
|
|
const open = this.state.id === openDropdownId;
|
|
|
|
const show = placement !== null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<div onKeyDown={this.handleKeyDown}>
|
|
|
|
<div onKeyDown={this.handleKeyDown}>
|
|
|
|
<IconButton
|
|
|
|
<IconButton
|
|
|
|
icon={icon}
|
|
|
|
icon={icon}
|
|
|
|
title={ariaLabel}
|
|
|
|
title={ariaLabel}
|
|
|
|
active={show}
|
|
|
|
active={open}
|
|
|
|
disabled={disabled}
|
|
|
|
disabled={disabled}
|
|
|
|
size={size}
|
|
|
|
size={size}
|
|
|
|
ref={this.setTargetRef}
|
|
|
|
ref={this.setTargetRef}
|
|
|
|
onClick={this.handleClick}
|
|
|
|
onClick={this.handleClick}
|
|
|
|
/>
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
<Overlay show={show} placement={placement} 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>
|
|
|
|