2018-05-29 14:51:42 +00:00
|
|
|
import React, { Component } from 'react';
|
2017-10-02 22:36:59 +00:00
|
|
|
import classnames from 'classnames';
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
ariaLabel: string;
|
2017-11-10 03:30:20 +00:00
|
|
|
disabled?: boolean;
|
2017-10-02 22:36:59 +00:00
|
|
|
size?: string;
|
|
|
|
color?: string;
|
|
|
|
renderLabel(): any;
|
|
|
|
renderOptions(): any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
expanded: boolean;
|
|
|
|
}
|
|
|
|
|
2018-05-29 14:51:42 +00:00
|
|
|
export default class DropdownComponent extends Component<Props, State> {
|
2017-10-02 22:36:59 +00:00
|
|
|
public static defaultProps = {
|
|
|
|
color: 'default',
|
|
|
|
size: 'sm'
|
|
|
|
};
|
|
|
|
|
|
|
|
public state: State = {
|
|
|
|
expanded: false
|
|
|
|
};
|
|
|
|
|
|
|
|
private dropdown: HTMLElement | null;
|
|
|
|
|
|
|
|
public componentDidMount() {
|
|
|
|
document.addEventListener('click', this.clickOffHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentWillUnmount() {
|
|
|
|
document.removeEventListener('click', this.clickOffHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
2017-12-18 23:29:26 +00:00
|
|
|
const { ariaLabel, color, disabled, size, renderOptions, renderLabel } = this.props;
|
2017-10-02 22:36:59 +00:00
|
|
|
const { expanded } = this.state;
|
2017-12-18 23:29:26 +00:00
|
|
|
const toggleClasses = classnames(['dropdown-toggle', 'btn', `btn-${color}`, `btn-${size}`]);
|
2017-10-02 22:36:59 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<span
|
2017-11-10 03:30:20 +00:00
|
|
|
className={`dropdown ${expanded || disabled ? 'open' : ''}`}
|
2017-10-02 22:36:59 +00:00
|
|
|
ref={el => (this.dropdown = el)}
|
|
|
|
>
|
|
|
|
<a
|
|
|
|
tabIndex={0}
|
|
|
|
aria-haspopup="true"
|
|
|
|
aria-expanded={expanded}
|
|
|
|
aria-label={ariaLabel}
|
|
|
|
className={toggleClasses}
|
|
|
|
onClick={this.toggle}
|
|
|
|
>
|
|
|
|
{renderLabel()}
|
2017-11-10 03:30:20 +00:00
|
|
|
{!disabled && <i className="caret" />}
|
2017-10-02 22:36:59 +00:00
|
|
|
</a>
|
2017-11-10 03:30:20 +00:00
|
|
|
{expanded && !disabled && renderOptions()}
|
2017-10-02 22:36:59 +00:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public toggle = () => {
|
|
|
|
this.setState({ expanded: !this.state.expanded });
|
|
|
|
};
|
|
|
|
|
|
|
|
public open = () => {
|
|
|
|
this.setState({ expanded: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
public close = () => {
|
|
|
|
this.setState({ expanded: false });
|
|
|
|
};
|
|
|
|
|
|
|
|
private clickOffHandler = (ev: MouseEvent) => {
|
|
|
|
// Only calculate if dropdown is open & we have the ref
|
|
|
|
if (!this.state.expanded || !this.dropdown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's an element that's not inside of the dropdown, close it up
|
|
|
|
if (
|
|
|
|
this.dropdown !== ev.target &&
|
|
|
|
ev.target instanceof HTMLElement &&
|
|
|
|
!this.dropdown.contains(ev.target)
|
|
|
|
) {
|
|
|
|
this.setState({ expanded: false });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|