Created
November 4, 2023 22:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} />; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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