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

stop number inputs to update value on scroll #710

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 17 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<ConfigProvider>
<StanCoreContext.Provider value={stanCore}>
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/components/forms/Input.spec.tsx
Original file line number Diff line number Diff line change
@@ -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<FormikType>({
initialValues: {
numberField: 2
},
onSubmit: jest.fn()
});

return (
<FormikProvider value={formik}>
<Form>
<FormikInput label="number field" type="number" name="numberField" />
</Form>
</FormikProvider>
);
};

describe('Formik Input', () => {
test('scroll wheel does not change value', () => {
const { getByLabelText } = render(<MockFormikInput />);
const numberInput = getByLabelText('number field');
expect(numberInput).toHaveValue(2);

fireEvent.scroll(numberInput, { target: { scrollTop: 200 } });

expect(numberInput).toHaveValue(2);
});
});
Loading