import { TextField, InputAdornment } from "@mui/material"; import { Doc } from '../../../../fields/Doc'; import React = require('react'); import TaskAltIcon from "@mui/icons-material/TaskAlt"; import ErrorOutlineIcon from "@mui/icons-material/ErrorOutline"; export interface IInputProps { label?: JSX.Element; lowerBound: number; dataDoc: doc; prop: string; step: number; unit: string; upperBound: number; value: number | string | Array; correctValue?: number; showIcon?: boolean; effect?: (val: number) => any; radianEquivalent?: boolean; small?: boolean; mode?: string; update?: boolean; labelWidth?: string; } interface IState { tempValue: number, tempRadianValue: number, } export default class InputField extends React.Component { constructor(props: any) { super(props) this.state = { tempValue: this.props.mode != "Freeform" && !this.props.showIcon ? 0 : this.props.value, tempRadianValue: this.props.mode != "Freeform" && !this.props.showIcon ? 0 : (Number(this.props.value) * Math.PI) / 180 } } epsilon: number = 0; width: string = "6em"; margin: string = "6em"; componentDidMount() { this.epsilon = 0.01; this.width = this.props.small ? "6em" : "7em"; this.margin = this.props.small ? "0px" : "10px"; } componentDidUpdate(prevProps: Readonly, prevState: Readonly, snapshot?: any): void { } useEffect(() => { if (mode == "Freeform") { if (Math.abs(tempValue - Number(value)) > 1) { setTempValue(Number(value)); } } }, [value]); externalUpdate = () => { setTempValue(Number(value)); setTempRadianValue((Number(value) * Math.PI) / 180); }; useEffect(() => { this.externalUpdate(); }, [update]); onChange = (event: React.ChangeEvent) => { let value = event.target.value == "" ? 0 : Number(event.target.value); if (value > this.props.upperBound) { value = this.props.upperBound; } else if (value < this.props.lowerBound) { value = this.props.lowerBound; } this.props.dataDoc[this.props.prop] = value setTempValue(event.target.value == "" ? event.target.value : value); setTempRadianValue((value * Math.PI) / 180); if (effect) { effect(value); } }; onChangeRadianValue = (event: React.ChangeEvent) => { let value = event.target.value === "" ? 0 : Number(event.target.value); if (value > 2 * Math.PI) { value = 2 * Math.PI; } else if (value < 0) { value = 0; } this.props.dataDoc[this.props.prop] = (value * 180) / Math.PI setTempValue((value * 180) / Math.PI); setTempRadianValue(value); if (effect) { effect((value * 180) / Math.PI); } }; return (
{label && (
{label}
)} {Math.abs(Number(value) - (correctValue ?? 0)) < epsilon && showIcon && } {Math.abs(Number(value) - (correctValue ?? 0)) >= epsilon && showIcon && } ), endAdornment: {unit}, }} /> {radianEquivalent && (

)} {radianEquivalent && ( rad, }} /> )}
); };