@hisptz/react-ui
Version:
A collection of reusable complex DHIS2 react ui components.
148 lines (136 loc) • 5.03 kB
JavaScript
import i18n from "@dhis2/d2-i18n";
import { Button, ButtonStrip } from "@dhis2/ui";
import { makeStyles, Step, StepLabel, Stepper } from "@material-ui/core";
import { findIndex } from "lodash";
import React, { Suspense, useMemo } from "react";
import FullPageLoader from "../shared/components/FullPageLoader";
import "./styles/index.css";
export default function ConfigurationStepper(_ref) {
var _steps$label, _steps, _steps2;
let {
steps,
onLastAction,
activeStepperBackGroundColor,
onLastActionButtonName,
onStepChange: onStepChangeHandler,
setActiveStep,
activeStep
} = _ref;
const Component = activeStep.component;
const onNextStep = async () => {
if (!hasNextStep) {
onLastAction();
return;
}
const currentStepIndex = findIndex(steps, ["label", activeStep.label]);
if (onStepChangeHandler !== undefined) {
if (await onStepChangeHandler(currentStepIndex, currentStepIndex + 1)) {
if (currentStepIndex !== steps.length - 1 && currentStepIndex <= 0) {
setActiveStep(steps[currentStepIndex + 1]);
return;
}
} else {
return;
}
}
if (currentStepIndex !== steps.length - 1 && currentStepIndex >= 0) {
setActiveStep(steps[currentStepIndex + 1]);
}
};
const onPreviousStep = async () => {
const currentStepIndex = findIndex(steps, ["label", activeStep.label]);
if (onStepChangeHandler !== undefined) {
if (await onStepChangeHandler(currentStepIndex, currentStepIndex - 1)) {
if (currentStepIndex !== steps.length - 1 && currentStepIndex >= 0) {
setActiveStep(steps[currentStepIndex - 1]);
return;
}
} else {
return;
}
}
if (currentStepIndex !== 0) {
setActiveStep(steps[currentStepIndex - 1]);
}
};
const onStepChange = async step => {
if (onStepChangeHandler !== undefined) {
const currentStepIndex = findIndex(steps, ["label", activeStep.label]);
const newStepIndex = findIndex(steps, ["label", step.label]);
if (await onStepChangeHandler(currentStepIndex, newStepIndex)) {
setActiveStep(step);
} else {
return;
}
}
setActiveStep(step);
};
const hasNextStep = useMemo(() => findIndex(steps, ["label", activeStep.label]) !== steps.length - 1, [activeStep]);
const hasPreviousStep = useMemo(() => findIndex(steps, ["label", activeStep.label]) > 0, [activeStep]);
const currentIndex = useMemo(() => findIndex(steps, ["label", activeStep.label]), [activeStep]);
const useStyles = makeStyles({
root: {
margin: "0 8px",
padding: "8px",
fontweight: "bold",
height: "32px",
color: "#FFFFFF",
borderRadius: 3,
borderwidth: "2px",
boxShadow: "0 3px 5px 2px transparent"
}
});
const classes = useStyles();
return /*#__PURE__*/React.createElement(Suspense, {
fallback: /*#__PURE__*/React.createElement(FullPageLoader, null)
}, /*#__PURE__*/React.createElement("div", {
className: "column w-100 h-100"
}, /*#__PURE__*/React.createElement(Stepper, null, steps === null || steps === void 0 ? void 0 : steps.map(step => /*#__PURE__*/React.createElement(Step, {
id: "stepper",
className: classes.root,
style: step.label === activeStep.label ? {
background: activeStepperBackGroundColor
} : undefined,
active: step.label === activeStep.label,
onClick: () => onStepChange(step),
key: "".concat(step.label, "-step")
}, /*#__PURE__*/React.createElement(StepLabel, null, step.label)))), /*#__PURE__*/React.createElement("div", {
className: "row",
style: {
flex: 1
}
}, /*#__PURE__*/React.createElement("div", {
className: "column center",
style: {
flex: 1,
width: "100%"
}
}, /*#__PURE__*/React.createElement("div", {
className: "column p-16",
style: {
height: "100%",
justifyContent: "space-between",
gap: 24
}
}, /*#__PURE__*/React.createElement("div", {
style: {
height: "100%",
width: "100%",
flex: 1
}
}, /*#__PURE__*/React.createElement(Component, {
onNextStep: onNextStep,
onPreviousStep: onPreviousStep
})), /*#__PURE__*/React.createElement(ButtonStrip, {
start: true
}, /*#__PURE__*/React.createElement(Button, {
disabled: !hasPreviousStep,
onClick: onPreviousStep
}, "".concat(i18n.t("Previous"), ": ").concat((_steps$label = (_steps = steps[currentIndex - 1]) === null || _steps === void 0 ? void 0 : _steps.label) !== null && _steps$label !== void 0 ? _steps$label : "")), /*#__PURE__*/React.createElement(Button, {
primary: true,
disabled: false,
onClick: hasNextStep ? onNextStep : onLastAction,
className: "settings-next-button",
dataTest: "scorecard-admin-next-button"
}, !hasNextStep ? onLastActionButtonName : "".concat(i18n.t("Next"), ": ").concat((_steps2 = steps[currentIndex + 1]) === null || _steps2 === void 0 ? void 0 : _steps2.label))))))));
}