From c1705ae269f482384fdfa2fb5d7addefc1cba3fe Mon Sep 17 00:00:00 2001 From: brynnchernosky <56202540+brynnchernosky@users.noreply.github.com> Date: Mon, 1 May 2023 18:15:08 -0400 Subject: refactor input --- .../PhysicsBox/PhysicsSimulationInputField.tsx | 90 +++++++++++----------- 1 file changed, 43 insertions(+), 47 deletions(-) (limited to 'src/client/views/nodes/PhysicsBox/PhysicsSimulationInputField.tsx') diff --git a/src/client/views/nodes/PhysicsBox/PhysicsSimulationInputField.tsx b/src/client/views/nodes/PhysicsBox/PhysicsSimulationInputField.tsx index aa2d1c2cc..645c1995e 100644 --- a/src/client/views/nodes/PhysicsBox/PhysicsSimulationInputField.tsx +++ b/src/client/views/nodes/PhysicsBox/PhysicsSimulationInputField.tsx @@ -6,7 +6,7 @@ import ErrorOutlineIcon from "@mui/icons-material/ErrorOutline"; export interface IInputProps { label?: JSX.Element; lowerBound: number; - dataDoc: doc; + dataDoc: Doc; prop: string; step: number; unit: string; @@ -47,27 +47,23 @@ export default class InputField extends React.Component { componentDidUpdate(prevProps: Readonly, prevState: Readonly, snapshot?: any): void { - } - - - - useEffect(() => { - if (mode == "Freeform") { - if (Math.abs(tempValue - Number(value)) > 1) { - setTempValue(Number(value)); + if (prevProps.value != this.props.value) { + if (this.props.mode == "Freeform") { + if (Math.abs(this.state.tempValue - Number(this.props.value)) > 1) { + this.setState({tempValue: Number(this.props.value)}) + } } } - }, [value]); + if (prevProps.update != this.props.update) { + this.externalUpdate(); + } + } externalUpdate = () => { - setTempValue(Number(value)); - setTempRadianValue((Number(value) * Math.PI) / 180); + this.setState({tempValue: Number(this.props.value)}) + this.setState({tempRadianValue: Number(this.props.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) { @@ -76,10 +72,10 @@ export default class InputField extends React.Component { 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); + 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); } }; @@ -91,14 +87,14 @@ export default class InputField extends React.Component { 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); + this.setState({tempValue: (value * 180) / Math.PI}) + this.setState({tempRadianValue: value}) + if (this.props.effect) { + this.props.effect((value * 180) / Math.PI); } }; - return ( + render () {
{ alignItems: "center", }} > - {label && ( + {this.props.label && (
- {label} + {this.props.label}
)} - {Math.abs(Number(value) - (correctValue ?? 0)) < epsilon && - showIcon && } - {Math.abs(Number(value) - (correctValue ?? 0)) >= epsilon && - showIcon && } + {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: {unit}, + endAdornment: {this.props.unit}, }} /> - {radianEquivalent && ( + {this.props.radianEquivalent && (

)} - {radianEquivalent && ( + {this.props.radianEquivalent && ( { /> )}
- ); + } }; -- cgit v1.2.3-70-g09d2