|
| 1 | +import { useState } from "react"; |
| 2 | +import { useT } from "../../hooks/useT"; |
| 3 | +import { mergeStrings } from "../../util/merge-classnames"; |
| 4 | +import { Modal } from "../Modal/Modal"; |
| 5 | +import { Icon } from "../Shared/Icon"; |
| 6 | + |
| 7 | +export function StepIndicator({ |
| 8 | + steps, |
| 9 | + stepIndicatorId, |
| 10 | + onStepClick, |
| 11 | +}: { |
| 12 | + onStepClick?: (stepIndex: number, disabled?: boolean) => void; |
| 13 | + stepIndicatorId: string; |
| 14 | + steps: { |
| 15 | + title: string; |
| 16 | + status: "completed" | "error" | "default"; |
| 17 | + stepInformation?: string; |
| 18 | + disabled?: boolean; |
| 19 | + current?: boolean; |
| 20 | + }[]; |
| 21 | +}) { |
| 22 | + const t = useT(); |
| 23 | + const [showMobileDialog, setShowMobileDialog] = useState(false); |
| 24 | + const stepList = ( |
| 25 | + <ol className="step-indicator"> |
| 26 | + {steps.map((step, index) => { |
| 27 | + const StepElement = step.disabled ? "div" : "a"; |
| 28 | + return ( |
| 29 | + <li |
| 30 | + key={index} |
| 31 | + className={mergeStrings( |
| 32 | + step.disabled && "disabled", |
| 33 | + step.current && "current", |
| 34 | + step.status === "error" && "error", |
| 35 | + )} |
| 36 | + > |
| 37 | + <StepElement |
| 38 | + className="step" |
| 39 | + aria-current={step.current ? "step" : undefined} |
| 40 | + href={StepElement === "a" ? "#" : undefined} |
| 41 | + onClick={(e) => { |
| 42 | + e.preventDefault(); |
| 43 | + e.stopPropagation(); |
| 44 | + onStepClick?.(index, step.disabled); |
| 45 | + }} |
| 46 | + > |
| 47 | + {(step.status === "completed" || step.status === "error") && ( |
| 48 | + <span className="step-icon"> |
| 49 | + {/* Step completed */} |
| 50 | + {step.status === "completed" && ( |
| 51 | + <Icon |
| 52 | + icon="check" |
| 53 | + svgProps={{ |
| 54 | + "aria-label": t( |
| 55 | + "step_indicator_step_completed_sr_label", |
| 56 | + { |
| 57 | + stepNumber: index + 1, |
| 58 | + }, |
| 59 | + ), |
| 60 | + }} |
| 61 | + /> |
| 62 | + )} |
| 63 | + {/* Step has error */} |
| 64 | + {step.status === "error" && ( |
| 65 | + <Icon |
| 66 | + icon="error" |
| 67 | + svgProps={{ |
| 68 | + "aria-label": t("step_indicator_error_icon_sr_label", { |
| 69 | + stepNumber: index + 1, |
| 70 | + }), |
| 71 | + }} |
| 72 | + /> |
| 73 | + )} |
| 74 | + </span> |
| 75 | + )} |
| 76 | + {step.status === "default" && ( |
| 77 | + <span className="step-number"> |
| 78 | + <span>{index + 1}</span> |
| 79 | + </span> |
| 80 | + )} |
| 81 | + <div> |
| 82 | + <span className="step-title">{step.title}</span> |
| 83 | + {step.stepInformation && ( |
| 84 | + <span className="step-information"> |
| 85 | + {step.stepInformation} |
| 86 | + </span> |
| 87 | + )} |
| 88 | + </div> |
| 89 | + </StepElement> |
| 90 | + </li> |
| 91 | + ); |
| 92 | + })} |
| 93 | + </ol> |
| 94 | + ); |
| 95 | + const currentStep = steps.findIndex((step) => step.current === true) + 1 || 1; |
| 96 | + return ( |
| 97 | + <> |
| 98 | + <nav aria-label="Trinindikator" className="d-none d-md-block"> |
| 99 | + {stepList} |
| 100 | + </nav> |
| 101 | + <div> |
| 102 | + <button |
| 103 | + className="step-indicator-button d-md-none" |
| 104 | + aria-haspopup="dialog" |
| 105 | + onClick={() => setShowMobileDialog(true)} |
| 106 | + type="button" |
| 107 | + > |
| 108 | + <span> |
| 109 | + {t("step_indicator_mobile_indicator_button", { |
| 110 | + stepNumber: currentStep, |
| 111 | + totalSteps: steps.length, |
| 112 | + }) ?? ( |
| 113 | + <> |
| 114 | + Trin <strong>{currentStep}</strong> af {steps.length} |
| 115 | + </> |
| 116 | + )} |
| 117 | + </span> |
| 118 | + </button> |
| 119 | + </div> |
| 120 | + <Modal |
| 121 | + modalType="step-indicator" |
| 122 | + header={{ |
| 123 | + children: |
| 124 | + t("step_indicator_mobile_modal_heading", { |
| 125 | + stepNumber: currentStep, |
| 126 | + totalSteps: steps.length, |
| 127 | + }) ?? `Trin ${currentStep} af ${steps.length}`, |
| 128 | + level: "h2", |
| 129 | + }} |
| 130 | + id={stepIndicatorId} |
| 131 | + onClose={() => setShowMobileDialog(false)} |
| 132 | + show={showMobileDialog} |
| 133 | + > |
| 134 | + <nav aria-label="Trinindikator" className="d-md-none"> |
| 135 | + {stepList} |
| 136 | + </nav> |
| 137 | + </Modal> |
| 138 | + </> |
| 139 | + ); |
| 140 | +} |
0 commit comments