Files
lincodex/frontend/src/shared/ui/stepper/stepper.tsx
T

56 lines
1.9 KiB
TypeScript

import { VisuallyHidden } from "../visually-hidden/visually-hidden";
import styles from "./stepper.module.css";
import type { StepStatus, StepperStep } from "./build-steps";
const statusText: Readonly<Record<StepStatus, string>> = {
complete: "выполнен",
current: "текущий шаг",
upcoming: "впереди",
};
type StepperProps = {
/** Accessible name of the step list, for example "Шаги регистрации". */
label: string;
steps: readonly StepperStep[];
};
/**
* Progress through a multi-step flow: a visible "step N of M" line and an
* ordered list joined by a trace. The current step has aria-current="step";
* each step states its status in text, so the shape and color only repeat it.
*/
export function Stepper({ label, steps }: StepperProps) {
const currentIndex = steps.findIndex((step) => step.status === "current");
const completed = steps.filter((step) => step.status === "complete").length;
const position = currentIndex === -1 ? completed : currentIndex + 1;
return (
<div className={styles.root}>
<p className={styles.summary}>
{currentIndex === -1 && completed === steps.length
? `Все шаги выполнены: ${steps.length} из ${steps.length}`
: `Шаг ${position} из ${steps.length}`}
</p>
<ol className={styles.list} aria-label={label}>
{steps.map((step, index) => (
<li
key={step.id}
className={styles.step}
data-status={step.status}
aria-current={step.status === "current" ? "step" : undefined}
>
<span className={styles.marker} aria-hidden="true">
{step.status === "complete" ? "✓" : index + 1}
</span>
<span className={styles.label}>
{step.label}
<VisuallyHidden>{`, ${statusText[step.status]}`}</VisuallyHidden>
</span>
</li>
))}
</ol>
</div>
);
}