Skip to content

Commit

Permalink
fix(ProgressStepBar): add validate activeStepIndex (#3784)
Browse files Browse the repository at this point in the history
closes #3774
  • Loading branch information
gizeasy authored Oct 16, 2024
1 parent d0929e9 commit a32b963
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/components/ProgressStepBar/ProgressStepBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
progressStepBarPropDirectionDefault,
ProgressStepBarProps,
progressStepBarPropSizeDefault,
validateVisibleIndex,
withDefaultGetters,
} from './helpers';
import { ProgressStepBarItem } from './ProgressStepBarItem/ProgressStepBarItem';
Expand All @@ -49,7 +50,7 @@ function ProgressStepBarRender<ITEM = ProgressStepBarItemDefault>(
direction = progressStepBarPropDirectionDefault,
size = progressStepBarPropSizeDefault,
className,
activeStepIndex,
activeStepIndex: activeStepIndexProp,
onItemClick,
getItemContent,
getItemLabel,
Expand All @@ -63,6 +64,11 @@ function ProgressStepBarRender<ITEM = ProgressStepBarItemDefault>(
...otherProps
} = usePropsHandler(COMPONENT_NAME, withDefaultGetters(props), containerRef);

const activeStepIndex = validateVisibleIndex(
steps.length,
activeStepIndexProp,
);

const [visibleIndex, setVisibleIndex] = useState<number>(
activeStepIndex || 0,
);
Expand Down
15 changes: 15 additions & 0 deletions src/components/ProgressStepBar/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,18 @@ export const calculateLines = (

return sizes;
};

export const validateVisibleIndex = (
lenght: number,
index: number | undefined,
) => {
if (!index) {
return index;
}

if (index < 0) {
return undefined;
}

return Math.min(index, lenght - 1);
};
18 changes: 13 additions & 5 deletions src/hooks/useScrollElements/useScrollElements.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { createRef, useMemo } from 'react';

import { useDebounce } from '##/hooks/useDebounce';

type UseScrollElementsResult = {
refs: React.RefObject<HTMLElement>[];
scrollTo: (index: number) => void;
Expand All @@ -18,23 +20,29 @@ export function useScrollElements<ITEM>(
return refArray;
}, [items]);

const scrollTo = (index: number) => {
const scrollTo = useDebounce((index: number) => {
const currentRef = refs[Math.max(index, 0)];
const container = currentRef.current?.parentElement;
if (currentRef.current && container) {
const container = currentRef?.current?.parentElement;
if (currentRef?.current && container) {
const defaultPadding =
getComputedStyle(container).position !== 'relative'
? container.offsetLeft
: 0;

let scrollLeft = currentRef.current.offsetLeft - defaultPadding;

if (index <= 0) {
scrollLeft = 0;
} else if (index === refs.length - 1) {
scrollLeft = container.scrollWidth;
}
container.scrollLeft = scrollLeft;

container.scrollTo({
left: scrollLeft,
behavior: 'smooth',
});
}
};
}, 20);

return {
refs,
Expand Down

0 comments on commit a32b963

Please sign in to comment.