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

55 lines
1.5 KiB
TypeScript
Raw Normal View History

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() {
2018-01-29 13:53:24 -07:00
const addCss = (this.props.dim ? " dim" : "")
+ (this.props.grayscale ? " grayscale" : "");
2017-08-28 05:49:13 -06:00
const cb = () => !this.props.disabled && this.props.toggleAction();
return <button
disabled={!!this.props.disabled}
2018-01-29 13:53:24 -07:00
className={this.css() + addCss}
2018-12-03 20:05:45 -07:00
title={this.props.title || ""}
onClick={cb}>
2018-03-04 11:07:50 -07:00
{t(this.caption())}
</button>;
2017-06-29 12:54:02 -06:00
}
}