diff --git a/src/App.tsx b/src/App.tsx index a5d098a13..382ab09bf 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -10,6 +10,23 @@ function App() { *Cypress is waiting for this event to happen in onBeforeLoad callback in cypress/support/command.ts*/ window.dispatchEvent(new CustomEvent('reactRenderComplete')); }, []); + + /** + * This disables the default scroll wheel behavior on all number input fields. + * This effect applies globally to all number input fields in the document. + */ + React.useEffect(() => { + const handleWheel = (event: MouseEvent) => { + if (document.activeElement && document.activeElement.getAttribute('type') === 'number') { + event.preventDefault(); + } + }; + document.addEventListener('wheel', handleWheel, { passive: false }); + return () => { + document.removeEventListener('wheel', handleWheel); + }; + }, []); + return ( diff --git a/tests/unit/components/forms/Input.spec.tsx b/tests/unit/components/forms/Input.spec.tsx new file mode 100644 index 000000000..959f9d3e5 --- /dev/null +++ b/tests/unit/components/forms/Input.spec.tsx @@ -0,0 +1,42 @@ +import { cleanup, fireEvent, render } from '@testing-library/react'; +import FormikInput from '../../../../src/components/forms/Input'; +import React, { FC } from 'react'; +import { Form, FormikProvider, useFormik } from 'formik'; +import '@testing-library/jest-dom'; + +afterEach(() => { + cleanup(); +}); + +type FormikType = { + numberField: number; +}; + +const MockFormikInput: FC = () => { + const formik = useFormik({ + initialValues: { + numberField: 2 + }, + onSubmit: jest.fn() + }); + + return ( + +
+ + +
+ ); +}; + +describe('Formik Input', () => { + test('scroll wheel does not change value', () => { + const { getByLabelText } = render(); + const numberInput = getByLabelText('number field'); + expect(numberInput).toHaveValue(2); + + fireEvent.scroll(numberInput, { target: { scrollTop: 200 } }); + + expect(numberInput).toHaveValue(2); + }); +});