2017-06-04 10:40:14 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2016-09-02 04:19:37 +08:00
|
|
|
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';
|
2017-03-16 04:11:34 +08:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2016-09-02 04:19:37 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
const FOCUSABLE_CHILDREN = '[tabindex]:not([tabindex="-1"]), a, input, button';
|
2016-09-07 03:10:18 +08:00
|
|
|
|
2017-03-16 04:11:34 +08:00
|
|
|
const intlMessages = defineMessages({
|
|
|
|
close: {
|
|
|
|
id: 'app.dropdown.close',
|
2017-04-10 23:50:03 +08:00
|
|
|
description: 'Close button label',
|
2017-03-16 04:11:34 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
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(
|
2017-06-03 03:25:02 +08:00
|
|
|
`Invalid prop \`${propName}\` supplied to` +
|
|
|
|
` \`${componentName}\`. Validation failed.`,
|
2016-09-02 04:19:37 +08:00
|
|
|
);
|
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(
|
2017-06-03 03:25:02 +08:00
|
|
|
`Invalid prop \`${propName}\` supplied to` +
|
|
|
|
` \`${componentName}\`. Missing \`DropdownTrigger\`. Validation failed.`,
|
2016-09-02 04:19:37 +08:00
|
|
|
);
|
2016-08-20 05:20:17 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
if (!content) {
|
|
|
|
return new Error(
|
2017-06-03 03:25:02 +08:00
|
|
|
`Invalid prop \`${propName}\` supplied to` +
|
|
|
|
` \`${componentName}\`. Missing \`DropdownContent\`. Validation failed.`,
|
2016-09-02 04:19:37 +08:00
|
|
|
);
|
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,
|
2017-05-19 02:38:07 +08:00
|
|
|
autoFocus: false,
|
2016-09-07 03:10:18 +08:00
|
|
|
};
|
|
|
|
|
2017-03-16 04:11:34 +08:00
|
|
|
class Dropdown extends Component {
|
2016-09-02 04:19:37 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-06-03 03:25:02 +08:00
|
|
|
this.state = { isOpen: false };
|
2016-09-02 04:19:37 +08:00
|
|
|
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() {
|
2017-05-23 07:39:08 +08:00
|
|
|
const { addEventListener } = window;
|
|
|
|
addEventListener('click', this.handleWindowClick, false);
|
|
|
|
|
2016-09-15 20:27:24 +08:00
|
|
|
this.setState({ isOpen: true }, this.handleStateCallback);
|
2016-08-20 05:20:17 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
handleHide() {
|
2017-05-23 07:39:08 +08:00
|
|
|
const { removeEventListener } = window;
|
|
|
|
removeEventListener('click', this.handleWindowClick, false);
|
|
|
|
|
2017-05-03 05:15:44 +08:00
|
|
|
const { autoFocus } = this.props;
|
|
|
|
|
2016-09-15 20:27:24 +08:00
|
|
|
this.setState({ isOpen: false }, this.handleStateCallback);
|
2017-05-03 05:15:44 +08:00
|
|
|
|
|
|
|
if (autoFocus) {
|
2017-06-04 07:58:27 +08:00
|
|
|
const triggerElement = findDOMNode(this.trigger);
|
2017-05-03 05:15:44 +08:00
|
|
|
triggerElement.focus();
|
|
|
|
}
|
2016-08-20 05:20:17 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 04:19:37 +08:00
|
|
|
handleWindowClick(event) {
|
2017-06-12 11:47:17 +08:00
|
|
|
if (this.state.isOpen) {
|
2017-06-11 02:22:54 +08:00
|
|
|
const dropdownElement = findDOMNode(this);
|
|
|
|
const shouldUpdateState = event.target !== dropdownElement &&
|
2016-09-02 04:19:37 +08:00
|
|
|
!dropdownElement.contains(event.target) &&
|
|
|
|
this.state.isOpen;
|
2016-08-25 22:34:27 +08:00
|
|
|
|
2017-06-11 02:22:54 +08:00
|
|
|
if (shouldUpdateState) {
|
|
|
|
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
|
|
|
handleToggle() {
|
|
|
|
this.state.isOpen ?
|
|
|
|
this.handleHide() :
|
|
|
|
this.handleShow();
|
2016-08-20 05:20:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-05-10 23:12:24 +08:00
|
|
|
const {
|
|
|
|
children,
|
|
|
|
className,
|
2017-05-19 02:38:07 +08:00
|
|
|
style,
|
|
|
|
intl,
|
|
|
|
...otherProps,
|
2017-05-10 23:12:24 +08:00
|
|
|
} = 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, {
|
2017-06-04 07:58:27 +08:00
|
|
|
ref: (ref) => { this.trigger = ref; },
|
2016-09-07 03:10:18 +08:00
|
|
|
dropdownToggle: this.handleToggle,
|
|
|
|
dropdownShow: this.handleShow,
|
|
|
|
dropdownHide: this.handleHide,
|
|
|
|
});
|
|
|
|
|
|
|
|
content = React.cloneElement(content, {
|
2017-06-04 07:58:27 +08:00
|
|
|
ref: (ref) => { this.content = ref; },
|
2016-09-07 03:10:18 +08:00
|
|
|
'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 (
|
2017-05-10 23:12:24 +08:00
|
|
|
<div
|
2017-05-19 02:38:07 +08:00
|
|
|
style={style}
|
|
|
|
className={cx(styles.dropdown, className)}
|
|
|
|
aria-live={otherProps['aria-live']}
|
|
|
|
aria-relevant={otherProps['aria-relevant']}
|
2017-06-17 02:42:28 +08:00
|
|
|
aria-haspopup={otherProps['aria-haspopup']}
|
|
|
|
aria-label={otherProps['aria-label']}>
|
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}
|
2017-03-16 04:11:34 +08:00
|
|
|
label={intl.formatMessage(intlMessages.close)}
|
2017-03-16 02:09:20 +08:00
|
|
|
size={'lg'}
|
2017-03-16 22:34:03 +08:00
|
|
|
color={'default'}
|
2017-03-16 02:09:20 +08:00
|
|
|
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;
|
2017-03-16 04:11:34 +08:00
|
|
|
export default injectIntl(Dropdown);
|