2017-05-23 23:06:01 +00:00
|
|
|
import React, { Component } from 'react';
|
2017-10-02 22:36:59 +00:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import DropdownShell from './DropdownShell';
|
2017-05-23 23:06:01 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
interface Props<T> {
|
2017-10-04 17:51:37 +00:00
|
|
|
value: T | undefined;
|
2017-09-25 02:06:28 +00:00
|
|
|
options: T[];
|
|
|
|
ariaLabel: string;
|
2017-10-02 22:36:59 +00:00
|
|
|
label?: string;
|
2017-09-25 02:06:28 +00:00
|
|
|
extra?: any;
|
2017-10-02 22:36:59 +00:00
|
|
|
size?: string;
|
|
|
|
color?: string;
|
|
|
|
menuAlign?: string;
|
2017-09-26 23:03:38 +00:00
|
|
|
formatTitle?(option: T): any;
|
2017-09-25 02:06:28 +00:00
|
|
|
onChange(value: T): void;
|
|
|
|
}
|
2017-07-04 01:25:01 +00:00
|
|
|
|
2017-10-02 22:36:59 +00:00
|
|
|
export default class DropdownComponent<T> extends Component<Props<T>, {}> {
|
|
|
|
private dropdownShell: DropdownShell | null;
|
2017-05-23 23:06:01 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public render() {
|
2017-10-02 22:36:59 +00:00
|
|
|
const { ariaLabel, color, size } = this.props;
|
2017-05-23 23:06:01 +00:00
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
return (
|
2017-10-02 22:36:59 +00:00
|
|
|
<DropdownShell
|
|
|
|
renderLabel={this.renderLabel}
|
|
|
|
renderOptions={this.renderOptions}
|
|
|
|
size={size}
|
|
|
|
color={color}
|
|
|
|
ariaLabel={ariaLabel}
|
|
|
|
ref={el => (this.dropdownShell = el)}
|
|
|
|
/>
|
2017-07-02 05:49:06 +00:00
|
|
|
);
|
|
|
|
}
|
2017-05-23 23:06:01 +00:00
|
|
|
|
2017-10-02 22:36:59 +00:00
|
|
|
private renderLabel = () => {
|
2017-10-19 02:29:49 +00:00
|
|
|
const { value } = this.props;
|
2017-10-02 22:36:59 +00:00
|
|
|
const labelStr = this.props.label ? `${this.props.label}:` : '';
|
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
{labelStr} {this.formatTitle(value)}
|
|
|
|
</span>
|
|
|
|
);
|
2017-09-26 23:03:38 +00:00
|
|
|
};
|
2017-05-23 23:06:01 +00:00
|
|
|
|
2017-10-02 22:36:59 +00:00
|
|
|
private renderOptions = () => {
|
|
|
|
const { options, value, menuAlign, extra } = this.props;
|
|
|
|
const menuClass = classnames({
|
|
|
|
'dropdown-menu': true,
|
|
|
|
[`dropdown-menu-${menuAlign || ''}`]: !!menuAlign
|
2017-07-02 05:49:06 +00:00
|
|
|
});
|
2017-10-02 22:36:59 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<ul className={menuClass}>
|
|
|
|
{options.map((option, i) => {
|
|
|
|
return (
|
|
|
|
<li key={i}>
|
|
|
|
<a
|
|
|
|
className={option === value ? 'active' : ''}
|
|
|
|
onClick={this.onChange.bind(null, option)}
|
|
|
|
>
|
|
|
|
{this.props.formatTitle ? this.formatTitle(option) : option}
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
{extra && <li key={'separator'} role="separator" className="divider" />}
|
|
|
|
{extra}
|
|
|
|
</ul>
|
|
|
|
);
|
2017-07-02 05:49:06 +00:00
|
|
|
};
|
2017-05-23 23:06:01 +00:00
|
|
|
|
2017-10-02 22:36:59 +00:00
|
|
|
private formatTitle = (option: any) => {
|
|
|
|
if (this.props.formatTitle) {
|
|
|
|
return this.props.formatTitle(option);
|
|
|
|
} else {
|
|
|
|
return option;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private onChange = (value: any) => {
|
2017-07-02 05:49:06 +00:00
|
|
|
this.props.onChange(value);
|
2017-10-02 22:36:59 +00:00
|
|
|
if (this.dropdownShell) {
|
|
|
|
this.dropdownShell.close();
|
|
|
|
}
|
2017-07-02 05:49:06 +00:00
|
|
|
};
|
2017-05-23 23:06:01 +00:00
|
|
|
}
|