Skip to content

Commit

Permalink
Scroll up when changing pages (#2288)
Browse files Browse the repository at this point in the history
* Scroll up when changing pages

* use null safe operator
  • Loading branch information
jadmsaadaot committed Sep 28, 2023
1 parent 294b007 commit a330664
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
29 changes: 20 additions & 9 deletions met-web/src/components/Form/MultiPageForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useRef } from 'react';
import { Form } from '@formio/react';
import { FormSubmissionData, FormSubmitterProps } from './types';
import FormStepper from 'components/survey/submit/Stepper';
Expand All @@ -9,22 +9,33 @@ interface PageData {

const MultiPageForm = ({ handleFormChange, savedForm, handleFormSubmit }: FormSubmitterProps) => {
const [currentPage, setCurrentPage] = useState(0);
const totalComponents = savedForm?.components?.length;

const stepperRef = useRef<HTMLDivElement>(null);

const handleScrollUp = () => {
if (stepperRef.current) {
stepperRef.current.scrollIntoView({
behavior: 'smooth',
block: 'end',
});
}
};

return (
<div className="formio">
<FormStepper
currentPage={currentPage}
totalPages={totalComponents ? totalComponents - 1 : 0}
pages={savedForm?.components || []}
/>
<div className="formio" ref={stepperRef}>
<FormStepper currentPage={currentPage} pages={savedForm?.components ?? []} />
<Form
form={savedForm || { display: 'wizard' }}
options={{ noAlerts: true }}
onChange={(form: unknown) => handleFormChange(form as FormSubmissionData)}
onNextPage={(pageData: PageData) => {
setCurrentPage(pageData.page);
handleScrollUp();
}}
onPrevPage={(pageData: PageData) => {
setCurrentPage(pageData.page);
handleScrollUp();
}}
onPrevPage={(pageData: PageData) => setCurrentPage(pageData.page)}
onSubmit={(form: unknown) => {
const formSubmissionData = form as FormSubmissionData;
handleFormSubmit(formSubmissionData.data);
Expand Down
6 changes: 3 additions & 3 deletions met-web/src/components/survey/submit/Stepper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import useMediaQuery from '@mui/material/useMediaQuery';

export interface ProgressBarProps {
currentPage: number;
totalPages: number;
pages: Array<FormInfo>;
[prop: string]: unknown;
}

function FormStepper({ currentPage, totalPages, pages }: ProgressBarProps) {
function FormStepper({ currentPage, pages, ...rest }: ProgressBarProps) {
const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down('sm'));

return (
<Box sx={{ pb: 2 }}>
<Stepper activeStep={currentPage} alternativeLabel>
<Stepper activeStep={currentPage} alternativeLabel {...rest}>
{pages.map((page: FormInfo, index: number) => (
<Step key={index}>
<StepLabel> {(!isMobile || index === currentPage) && page.title}</StepLabel>
Expand Down

0 comments on commit a330664

Please sign in to comment.