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

Scroll up when changing pages #2288

Merged
merged 2 commits into from
Sep 28, 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
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
Loading