Skip to content
Snippets Groups Projects
Commit a25fe40c authored by Bjarke Madsen's avatar Bjarke Madsen
Browse files

refactor the survey progressbar component

parent 8b81a79d
Branches
Tags
1 merge request!56Feature/comp 233 survey navigation
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import { Col, Container, Row } from "react-bootstrap";
interface Progress { interface Progress {
completionPercentage: number; completionPercentage: number;
...@@ -16,6 +17,9 @@ function ProgressBar({ surveyModel, pageNoSetter }) { ...@@ -16,6 +17,9 @@ function ProgressBar({ surveyModel, pageNoSetter }) {
return question.value !== null && question.value !== undefined; return question.value !== null && question.value !== undefined;
}; };
useEffect(() => {
const calculateProgress = (survey) => { const calculateProgress = (survey) => {
if (survey && survey.pages) { if (survey && survey.pages) {
const progressArray: Progress[] = []; const progressArray: Progress[] = [];
...@@ -40,7 +44,6 @@ function ProgressBar({ surveyModel, pageNoSetter }) { ...@@ -40,7 +44,6 @@ function ProgressBar({ surveyModel, pageNoSetter }) {
} }
}; };
useEffect(() => {
surveyModel.onValueChanged.add((sender) => { surveyModel.onValueChanged.add((sender) => {
calculateProgress(sender); calculateProgress(sender);
}); });
...@@ -49,63 +52,51 @@ function ProgressBar({ surveyModel, pageNoSetter }) { ...@@ -49,63 +52,51 @@ function ProgressBar({ surveyModel, pageNoSetter }) {
}, [surveyModel]); }, [surveyModel]);
function progressBarContainerStyle(sectionProgress): React.CSSProperties { const progressBarStyle: React.CSSProperties = {
return { height: "0.5rem",
display: "flex",
flexWrap: "wrap",
height: "10px",
margin: "5px",
width: `${100 / sectionProgress.totalPages}%`,
}
}
const progressBarFillStyle: React.CSSProperties = {
height: "100%",
transition: "width 0.3s ease", transition: "width 0.3s ease",
}; };
function progressBarFillStyleCopy(sectionProgress): React.CSSProperties { return (
return { <Container className="survey-progress">
...progressBarFillStyle, <Row>
{progress.map((sectionProgress, index) => (
<Col xs={12} md key={index} onClick={() => pageNoSetter(index)} style={{ cursor: "pointer", margin: '0.5rem' }}>
<div>
<span style={{
whiteSpace: "nowrap",
fontSize: "1.5rem",
marginRight: "0.25rem",
fontWeight: "bold",
color: "#2db394",
}}>{index + 1}</span>
<span style={{
whiteSpace: "nowrap",
...(surveyModel.currentPageNo == index) && {
fontWeight: "bold",
},
}}>{sectionProgress.pageTitle}</span>
<div style={{ display: "flex", flexWrap: "wrap" }}>
<div style={{
...progressBarStyle,
width: `${sectionProgress.completionPercentage}%`, width: `${sectionProgress.completionPercentage}%`,
backgroundColor: "#1ab394", backgroundColor: "#262261",
} }} />
} <div style={{
...progressBarStyle,
function unansweredProgressBarFillStyle(sectionProgress): React.CSSProperties {
return {
...progressBarFillStyle,
width: `${sectionProgress.unansweredPercentage}%`, width: `${sectionProgress.unansweredPercentage}%`,
backgroundColor: "#9d9d9d", backgroundColor: "#cdcdcd",
} }} />
}
function pageTitleStyle(index): React.CSSProperties {
if (surveyModel.currentPageNo == index) {
return {
width: "100%",
textAlign: "center",
fontWeight: "bold"
}
} else {
return {
width: "100%",
textAlign: "center"
}
}
}
return (
<div className="survey-progress">
{progress.map((sectionProgress, index) => (
<div key={index} style={progressBarContainerStyle(sectionProgress)} onClick={() => pageNoSetter(index)}>
<div style={progressBarFillStyleCopy(sectionProgress)} />
<div style={unansweredProgressBarFillStyle(sectionProgress)} />
<div style={pageTitleStyle(index)}>{sectionProgress.pageTitle}</div>
</div> </div>
))}
</div> </div>
);
</Col>
))}
</Row>
</Container>);
} }
export default ProgressBar; export default ProgressBar;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment