2017-10-02 22:36:59 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import DropdownShell from './DropdownShell';
|
|
|
|
|
|
|
|
interface Option<T> {
|
|
|
|
name: any;
|
|
|
|
value: T;
|
|
|
|
color?: string;
|
2017-11-10 03:30:20 +00:00
|
|
|
hidden: boolean | undefined;
|
2017-10-02 22:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Props<T> {
|
|
|
|
value: T;
|
|
|
|
options: Option<T>[];
|
|
|
|
label?: string;
|
|
|
|
ariaLabel: string;
|
|
|
|
extra?: any;
|
|
|
|
size?: string;
|
|
|
|
color?: string;
|
|
|
|
menuAlign?: string;
|
2017-11-10 03:30:20 +00:00
|
|
|
disabled?: boolean;
|
2017-10-02 22:36:59 +00:00
|
|
|
onChange(value: T): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class ColorDropdown<T> extends Component<Props<T>, {}> {
|
|
|
|
private dropdownShell: DropdownShell | null;
|
|
|
|
|
|
|
|
public render() {
|
2017-11-10 03:30:20 +00:00
|
|
|
const { ariaLabel, disabled, color, size } = this.props;
|
2017-10-02 22:36:59 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<DropdownShell
|
|
|
|
renderLabel={this.renderLabel}
|
|
|
|
renderOptions={this.renderOptions}
|
|
|
|
size={size}
|
|
|
|
color={color}
|
|
|
|
ariaLabel={ariaLabel}
|
|
|
|
ref={el => (this.dropdownShell = el)}
|
2017-11-10 03:30:20 +00:00
|
|
|
disabled={disabled}
|
2017-10-02 22:36:59 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private renderLabel = () => {
|
|
|
|
const label = this.props.label ? `${this.props.label}:` : '';
|
|
|
|
const activeOption = this.getActiveOption();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
{label} {activeOption ? activeOption.name : '-'}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
private renderOptions = () => {
|
|
|
|
const { options, value, menuAlign, extra } = this.props;
|
|
|
|
|
2017-11-10 03:30:20 +00:00
|
|
|
const listItems = options
|
|
|
|
.filter(opt => !opt.hidden)
|
|
|
|
.reduce((prev: any[], opt) => {
|
|
|
|
const prevOpt = prev.length ? prev[prev.length - 1] : null;
|
|
|
|
if (prevOpt && !prevOpt.divider && prevOpt.color !== opt.color) {
|
|
|
|
prev.push({ divider: true });
|
|
|
|
}
|
|
|
|
prev.push(opt);
|
|
|
|
return prev;
|
|
|
|
}, []);
|
2017-10-02 22:36:59 +00:00
|
|
|
|
|
|
|
const menuClass = classnames({
|
|
|
|
'dropdown-menu': true,
|
|
|
|
[`dropdown-menu-${menuAlign || ''}`]: !!menuAlign
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ul className={menuClass}>
|
|
|
|
{listItems.map((option, i) => {
|
|
|
|
if (option.divider) {
|
|
|
|
return <li key={i} role="separator" className="divider" />;
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<li key={i} style={{ borderLeft: `2px solid ${option.color}` }}>
|
|
|
|
<a
|
|
|
|
className={option.value === value ? 'active' : ''}
|
|
|
|
onClick={this.onChange.bind(null, option.value)}
|
|
|
|
>
|
|
|
|
{option.name}
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})}
|
|
|
|
{extra && <li key="separator" role="separator" className="divider" />}
|
|
|
|
{extra}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
private onChange = (value: any) => {
|
|
|
|
this.props.onChange(value);
|
|
|
|
if (this.dropdownShell) {
|
|
|
|
this.dropdownShell.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private getActiveOption() {
|
|
|
|
return this.props.options.find(opt => opt.value === this.props.value);
|
|
|
|
}
|
|
|
|
}
|