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

102 lines
2.8 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import Button from '/imports/ui/components/button/component';
import PropTypes from 'prop-types';
2016-09-15 01:48:50 +08:00
import cx from 'classnames';
import KEY_CODES from '/imports/utils/keyCodes';
2016-09-02 04:19:37 +08:00
const propTypes = {
children: PropTypes.element.isRequired,
2016-09-02 04:19:37 +08:00
};
export default class DropdownTrigger extends Component {
static isButtonTriggerOnEmoji(buttonComponent) {
return (
buttonComponent
&& (buttonComponent.type === Button)
&& (buttonComponent.props)
&& (buttonComponent.props.children)
);
}
2016-09-02 04:19:37 +08:00
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
2021-06-01 02:28:27 +08:00
this.trigger = null;
}
handleClick(e) {
e.stopPropagation();
const { dropdownToggle, onClick } = this.props;
2021-06-01 02:28:27 +08:00
if (onClick) onClick();
return dropdownToggle();
}
handleKeyDown(event) {
event.stopPropagation();
2017-06-03 03:25:02 +08:00
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();
2021-06-01 02:28:27 +08:00
} else if ([KEY_CODES.ARROW_UP, KEY_CODES.ARROW_DOWN].includes(event.which)) {
dropdownShow();
2021-06-01 02:28:27 +08:00
} else if (KEY_CODES.ESCAPE === event.which) {
dropdownHide();
}
2016-09-02 04:19:37 +08:00
}
render() {
2021-06-01 02:28:27 +08:00
const { dropdownIsOpen } = this.props;
2017-10-13 08:20:27 +08:00
const remainingProps = { ...this.props };
delete remainingProps.dropdownToggle;
delete remainingProps.dropdownShow;
delete remainingProps.dropdownHide;
delete remainingProps.dropdownIsOpen;
const {
children,
className,
...restProps
} = remainingProps;
let TriggerComponent;
let TriggerComponentBounded;
2016-09-02 04:19:37 +08:00
const buttonComponentProps = {
...restProps,
'aria-expanded': dropdownIsOpen,
};
const triggerComponentProps = {
onClick: this.handleClick,
onKeyDown: this.handleKeyDown,
};
if (DropdownTrigger.isButtonTriggerOnEmoji(children)) {
const { children: grandChildren } = children.props;
triggerComponentProps.className = cx(children.props.className, className);
TriggerComponent = React.Children.only(grandChildren);
TriggerComponentBounded = React.cloneElement(TriggerComponent,
triggerComponentProps);
const ButtonComponent = React.Children.only(children);
return React.cloneElement(ButtonComponent,
buttonComponentProps, TriggerComponentBounded);
}
buttonComponentProps.className = cx(children.props.className, className);
TriggerComponent = React.Children.only(children);
TriggerComponentBounded = React.cloneElement(TriggerComponent,
{ ...buttonComponentProps, ...triggerComponentProps });
2016-09-02 04:19:37 +08:00
return TriggerComponentBounded;
}
}
DropdownTrigger.propTypes = propTypes;