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

58 lines
1.3 KiB
React
Raw Normal View History

2016-09-02 04:19:37 +08:00
import React, { Component, PropTypes } from 'react';
import { findDOMNode } from 'react-dom';
import KEY_CODES from '/imports/utils/keyCodes';
2016-09-02 04:19:37 +08:00
const propTypes = {
children: React.PropTypes.element.isRequired,
};
export default class DropdownTrigger extends Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
}
handleClick() {
const { dropdownToggle } = this.props;
return dropdownToggle();
}
handleKeyDown(event) {
const { dropdownShow, dropdownHide } = this.props;
if ([KEY_CODES.SPACE, KEY_CODES.ENTER].includes(event.which)) {
2016-09-07 22:43:54 +08:00
event.preventDefault();
event.stopPropagation();
2016-09-07 20:04:10 +08:00
return findDOMNode(this).click();
}
if ([KEY_CODES.ARROW_UP, KEY_CODES.ARROW_DOWN].includes(event.which)) {
dropdownShow();
}
if (KEY_CODES.ESCAPE === event.which) {
dropdownHide();
}
2016-09-02 04:19:37 +08:00
}
render() {
const { children } = this.props;
2016-09-02 04:19:37 +08:00
const TriggerComponent = React.Children.only(children);
const TriggerComponentBounded = React.cloneElement(children, {
onClick: this.handleClick,
onKeyDown: this.handleKeyDown,
2016-09-02 04:19:37 +08:00
'aria-haspopup': true,
2016-09-13 04:11:03 +08:00
tabIndex: '0',
2016-09-02 04:19:37 +08:00
});
return TriggerComponentBounded;
}
}
DropdownTrigger.propTypes = propTypes;