import { TextField, InputAdornment } from '@mui/material'; import { Doc } from '../../../../fields/Doc'; import * as React from 'react'; import TaskAltIcon from '@mui/icons-material/TaskAlt'; import ErrorOutlineIcon from '@mui/icons-material/ErrorOutline'; import { isNumber } from 'lodash'; 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: string | number | (string | number)[]; tempRadianValue: number; width: string; margin: string; } 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, width: this.props.small ? '6em' : '7em', margin: this.props.small ? '0px' : '10px', }; } epsilon: number = 0.01; componentDidMount(): void { this.setState({ tempValue: Number(this.props.value) }); } componentDidUpdate(prevProps: Readonly, prevState: Readonly, snapshot?: any): void { if (prevProps.value != this.props.value && isNumber(this.props.value) && !isNaN(this.props.value)) { if (this.props.mode == 'Freeform') { if (isNumber(this.state.tempValue) && Math.abs(this.state.tempValue - Number(this.props.value)) > 1) { this.setState({ tempValue: Number(this.props.value) }); } } } if (prevProps.update != this.props.update) { this.externalUpdate(); } } externalUpdate = () => { this.setState({ tempValue: Number(this.props.value) }); this.setState({ tempRadianValue: (Number(this.props.value) * Math.PI) / 180 }); }; 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; } if (this.props.prop != '') { this.props.dataDoc[this.props.prop] = value; } this.setState({ tempValue: event.target.value == '' ? event.target.value : value }); this.setState({ tempRadianValue: (value * Math.PI) / 180 }); if (this.props.effect) { this.props.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; } if (this.props.prop != '') { this.props.dataDoc[this.props.prop] = (value * 180) / Math.PI; } this.setState({ tempValue: (value * 180) / Math.PI }); this.setState({ tempRadianValue: value }); if (this.props.effect) { this.props.effect((value * 180) / Math.PI); } }; render() { return (
{this.props.label && (
{this.props.label}
)} {Math.abs(Number(this.props.value) - (this.props.correctValue ?? 0)) < this.epsilon && this.props.showIcon && } {Math.abs(Number(this.props.value) - (this.props.correctValue ?? 0)) >= this.epsilon && this.props.showIcon && } ), endAdornment: {this.props.unit}, }} /> {this.props.radianEquivalent && (

)} {this.props.radianEquivalent && ( rad, }} /> )}
); } }