Skip to content

Commit

Permalink
fix: Display of StepList with hidden items
Browse files Browse the repository at this point in the history
- Use `displayIndex` prop to seperately track step numbers to display
  • Loading branch information
kiosion committed Dec 3, 2024
1 parent c135d34 commit 026ec4a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function StepItem<T>(props: {
return (
<StepsContainer active={isDone || isActive}>
<StepTitle>
<Bullet isDone={isDone} isActive={isActive} stepNumber={index + 1} />
<Bullet isDone={isDone} isActive={isActive} stepNumber={props.view.displayIndex ?? index + 1} />
{props.view.title}
</StepTitle>
</StepsContainer>
Expand Down
24 changes: 17 additions & 7 deletions web/packages/teleport/src/components/Wizard/flow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
export type BaseView<T> = T & {
hide?: boolean;
index?: number;
displayIndex?: number;
views?: BaseView<T>[];
title: string;
};
Expand All @@ -28,13 +29,17 @@ export type BaseView<T> = T & {
* child. This is because the first child shares the same index with its parent, so we don't
* need to count it as it's not taking up a new index
*/
export function computeViewChildrenSize<T>(views: BaseView<T>[]) {
export function computeViewChildrenSize<T>(views: BaseView<T>[], constrainToVisible = false) {
let size = 0;
for (const view of views) {
if (constrainToVisible && view.hide) {
continue;
}

if (view.views) {
size += computeViewChildrenSize(view.views);
size += computeViewChildrenSize(view.views, constrainToVisible);
} else {
size += 1;
size++;
}
}

Expand All @@ -48,7 +53,8 @@ export function computeViewChildrenSize<T>(views: BaseView<T>[]) {
*/
export function addIndexToViews<T>(
views: BaseView<T>[],
index = 0
index = 0,
displayIndex = 1
): BaseView<T>[] {
const result: BaseView<T>[] = [];

Expand All @@ -60,11 +66,15 @@ export function addIndexToViews<T>(
};

if (view.views) {
copy.views = addIndexToViews(view.views, index);

copy.views = addIndexToViews(view.views, index, displayIndex);
index += computeViewChildrenSize(view.views);
} else {
index += 1;
index ++;
}

if (!view.hide) {
copy.displayIndex = displayIndex;
displayIndex += (view.views ? computeViewChildrenSize(view.views, true) : 1);
}

result.push(copy);
Expand Down

0 comments on commit 026ec4a

Please sign in to comment.