Farmbot-Web-App/frontend/devices/components/mcu_input_box.tsx

83 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-06-29 12:54:02 -06:00
import * as React from "react";
2019-06-24 15:39:49 -06:00
import { warning } from "../../toast/toast";
2017-06-29 12:54:02 -06:00
import { McuInputBoxProps } from "../interfaces";
import { updateMCU } from "../actions";
import { BlurableInput } from "../../ui/index";
2019-01-28 17:04:50 -07:00
import {
clampUnsignedInteger, IntegerSize, getMaxInputFromIntSize
} from "../../util";
2019-04-02 13:59:37 -06:00
2019-02-04 07:32:26 -07:00
import { isUndefined } from "lodash";
2019-04-02 13:59:37 -06:00
import { t } from "../../i18next_wrapper";
2017-06-29 12:54:02 -06:00
export class McuInputBox extends React.Component<McuInputBoxProps, {}> {
get key() { return this.props.setting; }
2018-03-09 02:34:24 -07:00
get config() {
return this.props.sourceFwConfig(this.key);
}
2017-06-29 12:54:02 -06:00
get value() {
2018-03-09 02:34:24 -07:00
const v = this.config.value;
2017-12-20 15:22:35 -07:00
const { filter } = this.props;
2019-02-04 07:32:26 -07:00
const goodValue = !isUndefined(v) && !(filter && v > filter);
2017-12-20 15:22:35 -07:00
return goodValue ? (v || 0).toString() : "";
2017-06-29 12:54:02 -06:00
}
2019-01-28 17:04:50 -07:00
get showValue() {
return this.props.scale
? "" + parseInt(this.value) / this.props.scale
: this.value;
}
2018-03-09 02:34:24 -07:00
get className() {
const dim = !this.config.consistent ? "dim" : "";
const gray = this.props.gray ? "gray" : "";
return [dim, gray].join(" ");
}
2017-12-16 17:16:19 -07:00
clampInputAndWarn = (input: string, intSize: IntegerSize): number => {
const result = clampUnsignedInteger(input, intSize);
const max = intSize === "long" ? "2,000,000,000" : "32,000";
switch (result.outcome) {
case "ok":
break;
case "high":
warning(t(`Maximum input is ${max}. Rounding down.`));
break;
case "low":
warning(t("Must be a positive number. Rounding up to 0."));
break;
default:
warning(t(`Please enter a number between 0 and ${max}`));
throw new Error("Bad input in mcu_input_box. Impossible?");
}
return result.result;
}
2017-06-29 12:54:02 -06:00
commit = (e: React.SyntheticEvent<HTMLInputElement>) => {
2017-08-28 05:49:13 -06:00
const { value } = e.currentTarget;
2019-01-28 17:04:50 -07:00
const scaledValue = this.props.scale
? "" + Math.round(parseFloat(value) * this.props.scale)
: value;
const actuallyDifferent = this.value !== scaledValue;
2017-06-29 12:54:02 -06:00
if (actuallyDifferent) {
2018-07-13 14:56:01 -06:00
const result = this.props.float
2019-01-28 17:04:50 -07:00
? Math.max(0, parseFloat(scaledValue))
: this.clampInputAndWarn(scaledValue, this.props.intSize);
2017-12-16 17:16:19 -07:00
this.props.dispatch(updateMCU(this.key, result.toString()));
2017-06-29 12:54:02 -06:00
}
}
render() {
return <BlurableInput
type="number"
2018-03-09 02:34:24 -07:00
className={this.className}
2019-01-28 17:04:50 -07:00
value={this.showValue}
2019-01-13 16:39:26 -07:00
onCommit={this.commit}
min={0}
max={getMaxInputFromIntSize(this.props.intSize)} />;
2017-06-29 12:54:02 -06:00
}
}