Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix triggering the onWheel only when an input is focused #6940

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions newIDE/app/src/UI/CompactTextField/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,18 @@ const CompactTextField = React.forwardRef<
onChange: (value: number) => onChange(value.toString(), 'iconControl'),
onGetInitialValue: () => parseFloat(value),
});
const [isFocused, setIsFocused] = React.useState<boolean>(false);

const onBlurInput = React.useCallback(
event => {
setIsFocused(false);
if (onBlur) onBlur(event);
},
[onBlur]
);
const onFocusInput = React.useCallback(
event => {
setIsFocused(true);
if (onFocus) onFocus(event);
},
[onFocus]
Expand All @@ -116,18 +119,28 @@ const CompactTextField = React.forwardRef<
() => {
const input = inputRef.current;
if (input && onWheel) {
input.addEventListener('wheel', onWheel, {
passive: false,
});
return () => {
input &&
input.removeEventListener('wheel', onWheel, {
passive: false,
});
};
// Do not allow the wheel event to be listened if the input is not focused.
// This is to avoid scrolling the input accidentally when the user is scrolling the page.
ClementPasteau marked this conversation as resolved.
Show resolved Hide resolved
if (isFocused) {
input.addEventListener('wheel', onWheel, {
passive: false,
});
} else {
input.removeEventListener('wheel', onWheel, {
passive: false,
});
}
}

return () => {
if (input && onWheel) {
input.removeEventListener('wheel', onWheel, {
passive: false,
});
}
};
},
[onWheel]
[onWheel, isFocused]
);

return (
Expand Down
Loading