Skip to content
Open
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
61 changes: 58 additions & 3 deletions src/components/Stepper/Stepper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import React, {
FC,
Ref,
useCallback,
useContext,
useEffect,
useLayoutEffect,
Expand Down Expand Up @@ -52,6 +53,8 @@ export const Stepper: FC<StepperProps> = React.forwardRef(
locale = enUS,
onChange,
readonly = true,
sectionRefs,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this prop is not user friendly, please find a different option

sectionIndexOffset = 0,
required = false,
scrollable,
scrollDownAriaLabelText: defaultScrollDownAriaLabelText,
Expand Down Expand Up @@ -225,6 +228,37 @@ export const Stepper: FC<StepperProps> = React.forwardRef(
onChange?.(index, event);
};

const scrollToSectionAndFocus = useCallback(
(visibleIndex: number) => {
if (!sectionRefs?.current) return;

const sectionIndex = visibleIndex + sectionIndexOffset;
const sectionElement = sectionRefs.current[sectionIndex] as HTMLElement;

if (!sectionElement) return;

sectionElement.scrollIntoView({ behavior: 'smooth' });

setTimeout(() => {
const firstInput = sectionElement.querySelector(
'input, textarea, select, [tabindex="0"]'
) as HTMLElement;
firstInput?.focus();
}, 100);
},
[sectionRefs, sectionIndexOffset]
);

const handleStepContentKeyDown = (
event: React.KeyboardEvent<HTMLElement>,
index: number
): void => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
scrollToSectionAndFocus(index);
}
};

const handleScroll = (): void => {
const steps: HTMLDivElement = stepsContainerRef.current;
if (layout === 'horizontal') {
Expand Down Expand Up @@ -608,6 +642,13 @@ export const Stepper: FC<StepperProps> = React.forwardRef(
styles.contentInner,
(styles as any)[`${combinedStepSize}`],
])}
tabIndex={0}
role="button"
aria-label={`Navigate to step ${index + 1}`}
onClick={() => scrollToSectionAndFocus(index)}
onKeyDown={(event) =>
handleStepContentKeyDown(event, index)
}
>
{step.content}
</div>
Expand Down Expand Up @@ -659,6 +700,13 @@ export const Stepper: FC<StepperProps> = React.forwardRef(
styles.contentInner,
(styles as any)[`${combinedStepSize}`],
])}
tabIndex={0}
role="button"
aria-label={`Navigate to step ${index + 1}`}
onClick={() => scrollToSectionAndFocus(index)}
onKeyDown={(event) =>
handleStepContentKeyDown(event, index)
}
>
{step.content}
</div>
Expand Down Expand Up @@ -789,6 +837,13 @@ export const Stepper: FC<StepperProps> = React.forwardRef(
styles.contentInner,
(styles as any)[`${combinedStepSize}`],
])}
tabIndex={0}
role="button"
aria-label={`Navigate to step ${index + 1}`}
onClick={() => scrollToSectionAndFocus(index)}
onKeyDown={(event) =>
handleStepContentKeyDown(event, index)
}
>
{step.content}
</div>
Expand All @@ -802,7 +857,7 @@ export const Stepper: FC<StepperProps> = React.forwardRef(
size === StepperSize.Small &&
layout === 'horizontal' && (
<hr
aria-hidden='true'
aria-hidden="true"
className={mergeClasses([
styles.separator,
{
Expand All @@ -824,7 +879,7 @@ export const Stepper: FC<StepperProps> = React.forwardRef(
<li className={styles.step}>
{layout === 'vertical' && (
<hr
role='presentation'
role="presentation"
className={mergeClasses([
innerSeparatorClassNames,
(styles as any)[`${step.size}`],
Expand All @@ -835,7 +890,7 @@ export const Stepper: FC<StepperProps> = React.forwardRef(
)}
{size !== StepperSize.Small && (
<hr
role='presentation'
role="presentation"
className={mergeClasses([
innerSeparatorClassNames,
(styles as any)[`${step.size}`],
Expand Down
11 changes: 11 additions & 0 deletions src/components/Stepper/Stepper.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ export interface StepperProps
* The onChange event handler.
*/
onChange?: OnChangeHandler;
/**
* Reference to an array of section elements to scroll to when step navigation occurs.
* When provided, automatic scroll and focus behavior is enabled.
*/
sectionRefs?: React.MutableRefObject<HTMLElement[]>;
/**
* Offset to adjust the section index calculation.
* Useful when there are additional sections before the step sections.
* @default 0
*/
sectionIndexOffset?: number;
/**
* The Stepper is read only.
* @default true
Expand Down
Loading