Skip to content

Commit f025c0c

Browse files
Fix for simulated button clicks on simulator button/pin inputs (#1017)
1 parent e4e2154 commit f025c0c

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/simulator/SensorInput.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: MIT
55
*/
66
import { Box, Button, Switch, VStack } from "@chakra-ui/react";
7-
import { ReactNode, useCallback, useState } from "react";
7+
import { ReactNode, useCallback, useRef, useState } from "react";
88
import { useIntl } from "react-intl";
99
import {
1010
RangeSensor as RangeSensorType,
@@ -35,15 +35,18 @@ const SensorInput = ({
3535
minimised,
3636
}: SensorInputProps) => {
3737
const sensor = state[sensorId] as RangeSensorType;
38+
const sensorValue = useRef<number>(sensor.value);
3839
const intl = useIntl();
39-
const [mouseDown, setMouseDown] = useState<boolean>(false);
40+
const mouseDown = useRef<boolean>(false);
4041
const handleSensorChange = useCallback(
4142
(value: number) => {
4243
// In this case isHeld is true, so the value should be reversed.
43-
if (sensor.value === value) {
44+
if (sensorValue.current === value) {
4445
onValueChange(sensorId, value === sensor.min ? sensor.max : sensor.min);
46+
sensorValue.current = value === sensor.min ? sensor.max : sensor.min;
4547
} else {
4648
onValueChange(sensorId, value);
49+
sensorValue.current = value;
4750
}
4851
},
4952
[onValueChange, sensor, sensorId]
@@ -61,12 +64,12 @@ const SensorInput = ({
6164
}
6265
};
6366
const mouseDownTouchStartAction = () => {
64-
setMouseDown(true);
67+
mouseDown.current = true;
6568
handleSensorChange(sensor.max);
6669
};
6770
const mouseUpTouchEndAction = () => {
68-
if (mouseDown) {
69-
setMouseDown(false);
71+
if (mouseDown.current) {
72+
mouseDown.current = false;
7073
handleSensorChange(sensor.min);
7174
}
7275
};
@@ -87,8 +90,8 @@ const SensorInput = ({
8790
mouseUpTouchEndAction();
8891
};
8992
const mouseLeaveListener = () => {
90-
if (mouseDown) {
91-
setMouseDown(false);
93+
if (mouseDown.current) {
94+
mouseDown.current = false;
9295
handleSensorChange(sensor.min);
9396
}
9497
};
@@ -108,11 +111,11 @@ const SensorInput = ({
108111
} else if (sensor.value === sensor.max) {
109112
setIsHeld(true);
110113
}
114+
sensorValue.current = sensor.value;
111115
setPrevSensorValue(sensor.value);
112116
}
113117

114118
const disabled = running === RunningStatus.STOPPED;
115-
116119
return (
117120
<VStack spacing={3}>
118121
<Button
@@ -122,13 +125,13 @@ const SensorInput = ({
122125
)}
123126
transition="none"
124127
_active={
125-
sensor.value === sensor.min
128+
sensorValue.current === sensor.min
126129
? {}
127130
: {
128131
background: "brand.100",
129132
}
130133
}
131-
isActive={!!sensor.value}
134+
isActive={!!sensorValue.current}
132135
disabled={disabled}
133136
size="sm"
134137
onKeyDown={keyListener}

0 commit comments

Comments
 (0)