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

177 lines
4.6 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';
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';
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';
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 = {
/**
* 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
const defaultProps = {
isOpen: 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);
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);
}
}
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) {
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) {
const dropdownElement = findDOMNode(this);
const shouldUpdateState = event.target !== dropdownElement &&
!dropdownElement.contains(event.target) &&
this.state.isOpen;
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() {
2017-05-10 23:12:24 +08:00
const {
children,
className,
style, intl,
hasPopup,
2017-05-13 02:11:42 +08:00
ariaLive,
ariaRelevant,
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-06-03 03:25:02 +08:00
style={style}
className={cx(styles.dropdown, className)}
aria-live={ariaLive}
aria-relevant={ariaRelevant}
aria-haspopup={hasPopup}
>
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);