likeopera-frontend/DropDownButton.js

80 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-12-02 19:27:22 +03:00
import React from 'react';
2019-05-20 19:35:47 +03:00
import PropTypes from 'prop-types';
2018-12-02 19:27:22 +03:00
import DropDownBase from './DropDownBase.js';
export default class DropDownButton extends React.PureComponent
{
2019-05-20 19:35:47 +03:00
static propTypes = {
checkable: PropTypes.bool,
whole: PropTypes.bool,
checked: PropTypes.bool,
className: PropTypes.string,
icon: PropTypes.string,
checkedIcon: PropTypes.string,
text: PropTypes.string,
checkedText: PropTypes.string,
onClick: PropTypes.func,
}
state = { pressed: false }
2018-12-02 19:27:22 +03:00
componentDidUpdate(prevProps, prevState)
{
if (prevProps.hidden && !this.props.hidden &&
DropDownBase.instances[this.props.dropdownId].isVisible())
{
DropDownBase.instances[this.props.dropdownId].showAt(this.refs.btn, this.unpress);
}
2018-12-02 19:27:22 +03:00
}
render()
{
2019-05-20 19:35:47 +03:00
return <a ref="btn" title={(this.props.checked ? this.props.checkedTitle : null) || this.props.title} onClick={this.onClickButton}
className={'button '+(this.props.dropdownId ? 'show-dropdown ' : '')+(this.props.checked ? 'checked ' : '')+
(this.props.hidden ? 'hidden ' : '')+
(this.state.pressed ? 'pressed ' : '')+(this.props.className || '')}>
2019-05-20 19:35:47 +03:00
{this.props.icon ? <img src={'icons/'+(this.props.checked ? this.props.checkedIcon : this.props.icon)+'.png'} /> : null}
{(this.props.checked ? this.props.checkedText : this.props.text) || null}
{this.props.dropdownId ? <span className="down" onClick={this.onClickDown}></span> : null}
</a>
2018-12-02 19:27:22 +03:00
}
toggle = () =>
{
if (!this.state.pressed)
{
DropDownBase.hideAll();
DropDownBase.instances[this.props.dropdownId].showAt(this.refs.btn, this.unpress);
}
else
DropDownBase.instances[this.props.dropdownId].hide();
this.setState({ pressed: !this.state.pressed });
2018-12-02 19:27:22 +03:00
}
unpress = () =>
{
this.setState({ pressed: false });
2018-12-02 19:27:22 +03:00
}
onClickButton = (ev) =>
{
if (this.props.whole || this.props.checkable && this.state.pressed)
this.toggle();
if (this.props.checkable)
2016-10-03 16:55:14 +03:00
{
if (this.props.onClick)
2019-05-20 19:35:47 +03:00
this.props.onClick(!this.props.checked);
2016-10-03 16:55:14 +03:00
}
else if (this.props.onClick)
this.props.onClick();
ev.stopPropagation();
2018-12-02 19:27:22 +03:00
}
onClickDown = (ev) =>
{
this.toggle();
ev.stopPropagation();
}
2018-12-02 19:27:22 +03:00
}