TeaWeb/shared/js/ui/frames/control-bar/button.tsx

86 lines
2.7 KiB
TypeScript
Raw Normal View History

2020-04-06 16:28:15 +02:00
import * as React from "react";
2020-04-10 20:57:50 +02:00
import {ReactComponentBase} from "tc-shared/ui/react-elements/ReactComponentBase";
2020-04-06 16:28:15 +02:00
import {DropdownContainer} from "tc-shared/ui/frames/control-bar/dropdown";
const cssStyle = require("./button.scss");
export interface ButtonState {
switched: boolean;
dropdownShowed: boolean;
2020-04-06 16:29:40 +02:00
dropdownForceShow: boolean;
2020-04-06 16:28:15 +02:00
}
export interface ButtonProperties {
colorTheme?: "red" | "default";
autoSwitch: boolean;
tooltip?: string;
iconNormal: string;
iconSwitched?: string;
onToggle?: (state: boolean) => boolean | void;
dropdownButtonExtraClass?: string;
switched?: boolean;
}
export class Button extends ReactComponentBase<ButtonProperties, ButtonState> {
protected defaultState(): ButtonState {
2020-04-06 16:28:15 +02:00
return {
switched: false,
2020-04-06 16:29:40 +02:00
dropdownShowed: false,
dropdownForceShow: false
2020-04-06 16:28:15 +02:00
}
}
render() {
const switched = this.props.switched || this.state.switched;
const buttonRootClass = this.classList(
cssStyle.button,
switched ? cssStyle.activated : "",
typeof this.props.colorTheme === "string" ? cssStyle["theme-" + this.props.colorTheme] : "");
const button = (
<div className={buttonRootClass} title={this.props.tooltip} onClick={this.onClick.bind(this)}>
<div className={this.classList("icon_em ", (switched ? this.props.iconSwitched : "") || this.props.iconNormal)} />
</div>
);
if(!this.hasChildren())
return button;
return (
2020-04-06 16:29:40 +02:00
<div className={this.classList(cssStyle.buttonDropdown, this.state.dropdownShowed || this.state.dropdownForceShow ? cssStyle.dropdownDisplayed : "", this.props.dropdownButtonExtraClass)} onMouseLeave={this.onMouseLeave.bind(this)}>
2020-04-06 16:28:15 +02:00
<div className={cssStyle.buttons}>
{button}
<div className={cssStyle.dropdownArrow} onMouseEnter={this.onMouseEnter.bind(this)}>
<div className={this.classList("arrow", "down")} />
</div>
</div>
<DropdownContainer>
{this.props.children}
</DropdownContainer>
</div>
);
}
private onMouseEnter() {
this.setState({
2020-04-06 16:28:15 +02:00
dropdownShowed: true
});
}
private onMouseLeave() {
this.setState({
2020-04-06 16:28:15 +02:00
dropdownShowed: false
});
}
private onClick() {
const new_state = !(this.state.switched || this.props.switched);
2020-04-06 16:28:15 +02:00
const result = this.props.onToggle?.call(undefined, new_state);
if(this.props.autoSwitch)
this.setState({ switched: typeof result === "boolean" ? result : new_state });
2020-04-06 16:28:15 +02:00
}
}