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

HAI-1248 Fix document scroll position in forms #390

Merged
merged 2 commits into from
Oct 16, 2023
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
2 changes: 2 additions & 0 deletions src/common/components/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import theme from './theme';
import { GlobalNotificationProvider } from '../globalNotification/GlobalNotificationContext';
import GlobalNotification from '../globalNotification/GlobalNotification';
import { FeatureFlagsProvider } from '../featureFlags/FeatureFlagsContext';
import ScrollToTop from '../scrollToTop/ScrollToTop';
import './app.scss';
import '../../../assets/styles/reset.css';

const queryClient = new QueryClient();

const App: React.FC<React.PropsWithChildren<unknown>> = () => (
<Router>
<ScrollToTop />
<Provider store={store}>
<QueryClientProvider client={queryClient}>
<ChakraProvider theme={theme}>
Expand Down
15 changes: 15 additions & 0 deletions src/common/components/scrollToTop/ScrollToTop.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';

/**
* Scroll to top of document when URL pathname changes
*/
export default function ScrollToTop() {
const { pathname } = useLocation();

useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);

return null;
}
1 change: 1 addition & 0 deletions src/domain/forms/MultipageForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const MultipageForm: React.FC<Props> = ({

function handleStepChange(value: Action) {
function changeStep() {
window.scrollTo(0, 0);
dispatch(value);
if (onStepChange) {
onStepChange();
Expand Down
1 change: 1 addition & 0 deletions src/testUtils/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const AllTheProviders = ({ children }: Props) => {

const customRender = (ui: React.ReactElement, options: RenderOptions = {}, route = '/') => {
window.history.pushState({}, 'Test page', route);
window.scrollTo = function () {};
return {
user: userEvent.setup(),
...render(ui, {
Expand Down
Loading