57 lines
1.2 KiB
React
Raw Normal View History

2017-06-27 02:27:55 +04:00
// @flow
import React from 'react';
export default class UnitDropdown extends React.Component {
2017-07-02 00:49:06 -05:00
props: {
value: string,
options: string[],
onChange?: (value: string) => void
};
state: {
expanded: boolean
} = {
expanded: false
};
2017-06-27 02:27:55 +04:00
2017-07-02 00:49:06 -05:00
render() {
const { value, options, onChange } = this.props;
const isReadonly = !onChange;
2017-06-27 02:27:55 +04:00
2017-07-02 00:49:06 -05:00
return (
<div className="input-group-btn">
<a
style={{ minWidth: 170 }}
className="btn btn-default dropdown-toggle"
onClick={this.onToggleExpand}
>
<strong>
{value}<i className="caret" />
</strong>
</a>
{this.state.expanded &&
!isReadonly &&
<ul className="dropdown-menu dropdown-menu-right">
{options.map(o =>
<li>
2017-06-27 02:27:55 +04:00
<a
2017-07-02 00:49:06 -05:00
className={value === o ? 'active' : ''}
onClick={this.props.onChange}
2017-06-27 02:27:55 +04:00
>
2017-07-02 00:49:06 -05:00
{o}
2017-06-27 02:27:55 +04:00
</a>
2017-07-02 00:49:06 -05:00
</li>
)}
</ul>}
</div>
);
}
2017-06-27 02:27:55 +04:00
2017-07-02 00:49:06 -05:00
onToggleExpand = () => {
this.setState(state => {
return {
expanded: !state.expanded
};
});
};
2017-06-27 02:27:55 +04:00
}