Farmbot-Web-App/webpack/controls/toggle_button.tsx

57 lines
1.5 KiB
TypeScript
Raw Normal View History

2017-06-29 12:54:02 -06:00
import * as React from "react";
2017-08-09 19:37:03 -06:00
import { t } from "i18next";
2017-06-29 12:54:02 -06:00
import { ToggleButtonProps } from "./interfaces";
2017-08-09 19:37:03 -06:00
import { isUndefined } from "util";
2017-06-29 12:54:02 -06:00
export class ToggleButton extends React.Component<ToggleButtonProps, {}> {
caption() {
2017-08-28 05:49:13 -06:00
const useNoYes = isUndefined(this.props.noYes) ? true : this.props.noYes;
const noOff = useNoYes ? t("no") : t("off");
const yesOn = useNoYes ? t("yes") : t("on");
const captions: { [s: string]: string | undefined } = {
2017-08-09 19:37:03 -06:00
"0": noOff,
"false": noOff,
"off": noOff,
"1": yesOn,
"true": yesOn,
"on": yesOn,
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-08-28 05:49:13 -06:00
const css = "fb-toggle-button fb-button";
2017-06-29 12:54:02 -06:00
if (this.props.disabled) { return css + " gray"; }
2017-08-28 05:49:13 -06:00
const redCSS = css + " red";
const greenCSS = css + " green";
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() {
2017-08-28 05:49:13 -06:00
const cb = () => !this.props.disabled && this.props.toggleAction();
return (
<button
disabled={!!this.props.disabled}
className={this.css()}
2017-08-23 16:26:09 -06:00
onClick={cb}>
{this.caption()}
</button>
);
2017-06-29 12:54:02 -06:00
}
}