2016-09-02 04:19:37 +08:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
|
|
|
import { findDOMNode } from 'react-dom';
|
2016-08-20 05:20:17 +08:00
|
|
|
import styles from './styles';
|
2016-09-02 04:19:37 +08:00
|
|
|
import DropdownTrigger from './trigger/component';
|
|
|
|
import DropdownContent from './content/component';
|
2017-03-16 02:09:20 +08:00
|
|
|
import Button from '/imports/ui/components/button/component';
|
2016-09-13 04:11:03 +08:00
|
|
|
import cx from 'classnames';
|
2016-09-02 04:19:37 +08:00
|
|
|
|
2016-09-07 03:10:18 +08:00
|
|
|
const FOCUSABLE_CHILDREN = `[tabindex]:not([tabindex="-1"]), a, input, button`;
|
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
const propTypes = {
|
2016-09-07 03:10:18 +08:00
|
|
|
/**
|
|
|
|
* The dropdown needs a trigger and a content component as childrens
|
|
|
|
*/
|
2016-09-02 04:19:37 +08:00
|
|
|
children: (props, propName, componentName) => {
|
|
|
|
const children = props[propName];
|
|
|
|
|
|
|
|
if (!children || children.length < 2) {
|
|
|
|
return new Error(
|
|
|
|
'Invalid prop `' + propName + '` supplied to' +
|
|
|
|
' `' + componentName + '`. Validation failed.'
|
|
|
|
);
|
2016-08-20 05:20:17 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
const trigger = children.find(x => x.type === DropdownTrigger);
|
|
|
|
const content = children.find(x => x.type === DropdownContent);
|
2016-08-20 05:20:17 +08:00
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
if (!trigger) {
|
|
|
|
return new Error(
|
|
|
|
'Invalid prop `' + propName + '` supplied to' +
|
|
|
|
' `' + componentName + '`. Missing `DropdownTrigger`. Validation failed.'
|
|
|
|
);
|
2016-08-20 05:20:17 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
if (!content) {
|
|
|
|
return new Error(
|
|
|
|
'Invalid prop `' + propName + '` supplied to' +
|
|
|
|
' `' + componentName + '`. Missing `DropdownContent`. Validation failed.'
|
|
|
|
);
|
2016-08-20 05:20:17 +08:00
|
|
|
}
|
2016-09-02 04:19:37 +08:00
|
|
|
},
|
|
|
|
};
|
2016-08-20 05:20:17 +08:00
|
|
|
|
2016-09-07 03:10:18 +08:00
|
|
|
const defaultProps = {
|
2016-09-28 03:27:12 +08:00
|
|
|
isOpen: false,
|
2016-09-07 03:10:18 +08:00
|
|
|
};
|
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
export default class Dropdown extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = { isOpen: false, };
|
|
|
|
this.handleShow = this.handleShow.bind(this);
|
|
|
|
this.handleHide = this.handleHide.bind(this);
|
2016-09-15 20:27:24 +08:00
|
|
|
this.handleStateCallback = this.handleStateCallback.bind(this);
|
2016-09-02 04:19:37 +08:00
|
|
|
this.handleToggle = this.handleToggle.bind(this);
|
|
|
|
this.handleWindowClick = this.handleWindowClick.bind(this);
|
2016-08-20 05:20:17 +08:00
|
|
|
}
|
|
|
|
|
2016-09-13 04:11:03 +08:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
|
|
if (prevState.isOpen !== this.props.isOpen
|
|
|
|
&& this.state.isOpen !== this.props.isOpen) {
|
2016-09-15 20:27:24 +08:00
|
|
|
this.setState({ isOpen: this.props.isOpen }, this.handleStateCallback);
|
2016-09-13 04:19:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleStateCallback() {
|
2016-09-15 01:48:50 +08:00
|
|
|
const { onShow, onHide } = this.props;
|
|
|
|
|
|
|
|
if (this.state.isOpen && onShow) {
|
|
|
|
onShow();
|
|
|
|
} else if (onHide) {
|
|
|
|
onHide();
|
2016-09-13 04:11:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
handleShow() {
|
2016-09-15 20:27:24 +08:00
|
|
|
this.setState({ isOpen: true }, this.handleStateCallback);
|
2016-09-07 03:10:18 +08:00
|
|
|
|
|
|
|
const contentElement = findDOMNode(this.refs.content);
|
|
|
|
contentElement.querySelector(FOCUSABLE_CHILDREN).focus();
|
2016-08-20 05:20:17 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
handleHide() {
|
2016-09-15 20:27:24 +08:00
|
|
|
this.setState({ isOpen: false }, this.handleStateCallback);
|
2016-09-07 03:10:18 +08:00
|
|
|
const triggerElement = findDOMNode(this.refs.trigger);
|
|
|
|
triggerElement.focus();
|
2016-08-20 05:20:17 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
componentDidMount () {
|
|
|
|
const { addEventListener } = window;
|
|
|
|
addEventListener('click', this.handleWindowClick, false);
|
2016-08-20 05:20:17 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
componentWillUnmount () {
|
|
|
|
const { removeEventListener } = window;
|
|
|
|
removeEventListener('click', this.handleWindowClick, false);
|
2016-08-24 06:19:13 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
handleWindowClick(event) {
|
|
|
|
const dropdownElement = findDOMNode(this);
|
|
|
|
const shouldUpdateState = event.target !== dropdownElement &&
|
|
|
|
!dropdownElement.contains(event.target) &&
|
|
|
|
this.state.isOpen;
|
2016-08-25 22:34:27 +08:00
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
if (shouldUpdateState) {
|
|
|
|
this.handleHide();
|
|
|
|
}
|
|
|
|
}
|
2016-08-24 06:19:13 +08:00
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
handleToggle() {
|
|
|
|
this.state.isOpen ?
|
|
|
|
this.handleHide() :
|
|
|
|
this.handleShow();
|
2016-08-20 05:20:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-09-15 01:48:50 +08:00
|
|
|
const { children, className, style } = this.props;
|
2016-08-24 06:19:13 +08:00
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
let trigger = children.find(x => x.type === DropdownTrigger);
|
2016-09-07 03:10:18 +08:00
|
|
|
let content = children.find(x => x.type === DropdownContent);
|
2016-08-24 06:19:13 +08:00
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
trigger = React.cloneElement(trigger, {
|
2016-09-07 03:10:18 +08:00
|
|
|
ref: 'trigger',
|
|
|
|
dropdownToggle: this.handleToggle,
|
|
|
|
dropdownShow: this.handleShow,
|
|
|
|
dropdownHide: this.handleHide,
|
|
|
|
});
|
|
|
|
|
|
|
|
content = React.cloneElement(content, {
|
|
|
|
ref: 'content',
|
|
|
|
'aria-expanded': this.state.isOpen,
|
|
|
|
dropdownToggle: this.handleToggle,
|
|
|
|
dropdownShow: this.handleShow,
|
|
|
|
dropdownHide: this.handleHide,
|
2016-09-02 04:19:37 +08:00
|
|
|
});
|
2016-08-24 06:19:13 +08:00
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
return (
|
2016-09-15 01:48:50 +08:00
|
|
|
<div style={style} className={cx(styles.dropdown, className)}>
|
2016-09-02 04:19:37 +08:00
|
|
|
{trigger}
|
2016-09-07 03:10:18 +08:00
|
|
|
{content}
|
2017-03-16 02:09:20 +08:00
|
|
|
{ this.state.isOpen ?
|
|
|
|
<Button
|
|
|
|
className={styles.close}
|
|
|
|
label={'Close'}
|
|
|
|
size={'lg'}
|
|
|
|
color={'danger'}
|
|
|
|
onClick={this.handleHide}
|
|
|
|
/> : null }
|
2016-08-20 05:20:17 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-09-02 04:19:37 +08:00
|
|
|
|
|
|
|
Dropdown.propTypes = propTypes;
|
2016-09-07 03:10:18 +08:00
|
|
|
Dropdown.defaultProps = defaultProps;
|