bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/dropdown/component.jsx

166 lines
4.3 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
2016-09-02 04:19:37 +08:00
import { findDOMNode } from 'react-dom';
import cx from 'classnames';
import { defineMessages, injectIntl } from 'react-intl';
import Button from '/imports/ui/components/button/component';
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 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
},
});
const noop = () => {};
2016-09-02 04:19:37 +08:00
const propTypes = {
/**
* The dropdown needs a trigger and a content component as children
*/
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
}
return null;
2016-09-02 04:19:37 +08:00
},
isOpen: PropTypes.bool,
onHide: PropTypes.func,
onShow: PropTypes.func,
autoFocus: PropTypes.bool,
2016-09-02 04:19:37 +08:00
};
2016-08-20 05:20:17 +08:00
const defaultProps = {
children: null,
isOpen: false,
onShow: noop,
onHide: noop,
2017-05-19 02:38:07 +08:00
autoFocus: false,
};
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);
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 (this.state.isOpen && !prevState.isOpen) {
this.props.onShow();
}
2016-09-15 01:48:50 +08:00
if (!this.state.isOpen && prevState.isOpen) {
this.props.onHide();
2016-09-13 04:11:03 +08:00
}
}
2016-09-02 04:19:37 +08:00
handleShow() {
this.setState({ isOpen: true }, () => {
const { addEventListener } = window;
addEventListener('click', this.handleWindowClick, true);
});
2016-08-20 05:20:17 +08:00
}
2016-09-02 04:19:37 +08:00
handleHide() {
this.setState({ isOpen: false }, () => {
const { removeEventListener } = window;
removeEventListener('click', this.handleWindowClick, true);
});
2016-08-20 05:20:17 +08:00
}
2016-09-02 04:19:37 +08:00
handleWindowClick(event) {
const triggerElement = findDOMNode(this.trigger);
if (!this.state.isOpen
|| triggerElement === event.target
|| triggerElement.contains(event.target)) {
return;
2016-09-02 04:19:37 +08:00
}
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() {
return 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);
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, {
ref: (ref) => { this.trigger = ref; },
dropdownToggle: this.handleToggle,
dropdownShow: this.handleShow,
dropdownHide: this.handleHide,
});
content = React.cloneElement(content, {
ref: (ref) => { this.content = ref; },
'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']}
aria-haspopup={otherProps['aria-haspopup']}
aria-label={otherProps['aria-label']}
tabIndex={-1}
>
2016-09-02 04:19:37 +08:00
{trigger}
{content}
{this.state.isOpen ?
<Button
className={styles.close}
2017-03-16 04:11:34 +08:00
label={intl.formatMessage(intlMessages.close)}
size="lg"
color="default"
onClick={this.handleHide}
/> : null}
2016-08-20 05:20:17 +08:00
</div>
);
}
}
2016-09-02 04:19:37 +08:00
Dropdown.propTypes = propTypes;
Dropdown.defaultProps = defaultProps;
2017-03-16 04:11:34 +08:00
export default injectIntl(Dropdown);