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

90 lines
3 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-09-28 09:37:48 +02:00
import {DropdownContainer} from "./DropDown";
import {ClientIcon} from "svg-sprites/client-icons";
2020-09-28 09:37:48 +02:00
const cssStyle = require("./Button.scss");
2020-04-06 16:28:15 +02:00
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 | ClientIcon;
iconSwitched?: string | ClientIcon;
2020-04-06 16:28:15 +02:00
onToggle?: (state: boolean) => boolean | void;
2020-09-28 09:37:48 +02:00
className?: string;
2020-04-06 16:28:15 +02:00
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] : "");
2020-09-28 09:37:48 +02:00
if(!this.hasChildren()) {
return (
<div className={buttonRootClass + " " + this.props.className} title={this.props.tooltip} onClick={this.onClick.bind(this)}>
<div className={this.classList("icon_em ", (switched ? this.props.iconSwitched : "") || this.props.iconNormal)} />
</div>
);
}
2020-04-06 16:28:15 +02:00
return (
2020-09-28 09:37:48 +02:00
<div className={this.classList(cssStyle.buttonDropdown, this.state.dropdownShowed || this.state.dropdownForceShow ? cssStyle.dropdownDisplayed : "", this.props.className)} onMouseLeave={this.onMouseLeave.bind(this)}>
2020-04-06 16:28:15 +02:00
<div className={cssStyle.buttons}>
2020-09-28 09:37:48 +02:00
<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>
2020-04-06 16:28:15 +02:00
<div className={cssStyle.dropdownArrow} onMouseEnter={this.onMouseEnter.bind(this)}>
2020-09-28 09:37:48 +02:00
<div className={cssStyle.arrow + " " + cssStyle.down} />
2020-04-06 16:28:15 +02:00
</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
}
}