Skip to content

Instantly share code, notes, and snippets.

@jeff-r-koyaltech
Created November 4, 2023 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeff-r-koyaltech/786b4b31e632398ddc4dba948081c266 to your computer and use it in GitHub Desktop.
Save jeff-r-koyaltech/786b4b31e632398ddc4dba948081c266 to your computer and use it in GitHub Desktop.
React MUI - prevent onWheel from changing a focused input type number
import { TextField } from '@mui/material';
import { useRef } from 'react';
export default function TextFieldExt(props) {
const { type } = props;
const noStupidWheelScroll = type === 'number';
const thisTextField = useRef(null);
const noOnWheelFn = (wheelEv) => {
wheelEv.preventDefault();
};
const wheelScrollOpts = noStupidWheelScroll
? {
onFocus: () => {
thisTextField.current.addEventListener('mousewheel', noOnWheelFn);
},
onBlur: () => {
thisTextField.current.removeEventListener('mousewheel', noOnWheelFn);
},
}
: {};
return <TextField ref={thisTextField} {...wheelScrollOpts} {...props} />;
}
<TextFieldExt
min="0"
type="number"
label="Budget Amount"
name="BudgetAmount"
onChange={(e) => {
// ... typical MUI onChange event
}}
value={prop.defaultAmount || 0}
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment