2017-06-29 12:54:02 -06:00
|
|
|
import * as React from "react";
|
|
|
|
import { ToggleButtonProps } from "./interfaces";
|
2019-04-02 13:59:37 -06:00
|
|
|
import { t } from "../i18next_wrapper";
|
2017-06-29 12:54:02 -06:00
|
|
|
|
|
|
|
export class ToggleButton extends React.Component<ToggleButtonProps, {}> {
|
|
|
|
caption() {
|
2017-12-20 20:01:40 -07:00
|
|
|
const { textTrue, textFalse } = this.props.customText
|
|
|
|
|| { textFalse: t("no"), textTrue: t("yes") };
|
2017-08-28 05:49:13 -06:00
|
|
|
const captions: { [s: string]: string | undefined } = {
|
2017-12-20 20:01:40 -07:00
|
|
|
"0": textFalse,
|
|
|
|
"false": textFalse,
|
|
|
|
"off": textFalse,
|
|
|
|
"1": textTrue,
|
|
|
|
"true": textTrue,
|
|
|
|
"on": textTrue,
|
2017-06-29 12:54:02 -06:00
|
|
|
"undefined": "🚫",
|
|
|
|
"-1": "🚫"
|
|
|
|
};
|
2017-08-28 05:49:13 -06:00
|
|
|
const togval = String(this.props.toggleValue);
|
2017-06-29 12:54:02 -06:00
|
|
|
return captions[togval] || "---";
|
|
|
|
}
|
|
|
|
|
|
|
|
css() {
|
2017-12-20 20:01:40 -07:00
|
|
|
const css = "fb-toggle-button fb-button ";
|
|
|
|
const greenCSS = css + "green";
|
|
|
|
const redCSS = css + "red";
|
|
|
|
const yellowCSS = css + "yellow";
|
2017-06-29 12:54:02 -06:00
|
|
|
|
2017-08-28 05:49:13 -06:00
|
|
|
const cssClasses: { [s: string]: string | undefined } = {
|
2017-06-29 12:54:02 -06:00
|
|
|
"0": redCSS,
|
|
|
|
"false": redCSS,
|
|
|
|
"off": redCSS,
|
|
|
|
"1": greenCSS,
|
|
|
|
"true": greenCSS,
|
|
|
|
"on": greenCSS,
|
|
|
|
"undefined": yellowCSS
|
|
|
|
};
|
|
|
|
|
2017-07-20 14:11:45 -06:00
|
|
|
return cssClasses[String(this.props.toggleValue)] || yellowCSS;
|
2017-06-29 12:54:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-03-13 15:06:40 -06:00
|
|
|
const allCss = [
|
|
|
|
this.css(),
|
|
|
|
this.props.className,
|
|
|
|
this.props.dim ? "dim" : "",
|
|
|
|
this.props.grayscale ? "grayscale" : "",
|
|
|
|
].join(" ");
|
2017-08-28 05:49:13 -06:00
|
|
|
const cb = () => !this.props.disabled && this.props.toggleAction();
|
2018-01-20 07:46:44 -07:00
|
|
|
return <button
|
|
|
|
disabled={!!this.props.disabled}
|
2020-03-13 15:06:40 -06:00
|
|
|
className={allCss}
|
2018-12-03 20:05:45 -07:00
|
|
|
title={this.props.title || ""}
|
2018-01-20 07:46:44 -07:00
|
|
|
onClick={cb}>
|
2018-03-04 11:07:50 -07:00
|
|
|
{t(this.caption())}
|
2018-01-20 07:46:44 -07:00
|
|
|
</button>;
|
2017-06-29 12:54:02 -06:00
|
|
|
}
|
|
|
|
}
|