Skip to content

Commit

Permalink
fix: remove required titles prop in Scrollspy (#315)
Browse files Browse the repository at this point in the history
* fix: remove required titles prop in Scrollspy

* fix: add tsdoc

* fix: removed unnecessary changes
  • Loading branch information
PahaN47 authored Oct 9, 2024
1 parent 2a48d7a commit fb64395
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/components/Scrollspy/Scrollspy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ export interface ScrollspyProps
extends Partial<ScrollspyDefaultProps>,
Partial<HTMLProps<HTMLUListElement>> {
items: string[];
titles: string[];
children: ReactElement[];
router: Router;
onSectionClick?: (event: MouseEvent) => void;
onActiveItemTitleChange?: (title: string) => void;
className?: string;
overflowedClassName?: string;
scrollToListItem?: boolean;
/** Is used to identify items for {@link onActiveItemTitleChange} */
titles?: string[];
/** Is called with active item's corresponding value in {@link titles}, if one exists */
onActiveItemTitleChange?: (title: string) => void;
}

interface ScrollspyState {
Expand Down Expand Up @@ -84,10 +86,6 @@ export class Scrollspy extends React.Component<ScrollspyInnerProps, ScrollspySta
const {items, router} = this.props;
const {inViewState} = this.state;

if (this.state.inViewState !== prevState.inViewState) {
console.log(inViewState.findIndex(Boolean));
}

if (!isEqual(inViewState, prevState.inViewState)) {
this.scrollToListItem();
}
Expand Down Expand Up @@ -331,16 +329,9 @@ export class Scrollspy extends React.Component<ScrollspyInnerProps, ScrollspySta
return getNewInViewState();
}

private getActiveItemTitle(titles: string[], inViewState: boolean[]) {
for (const [index, isActive] of inViewState.entries()) {
if (!isActive) {
continue;
}

return titles[index];
}

return null;
private getActiveItemTitle(titles: string[] | undefined, inViewState: boolean[]) {
const activeIndex = inViewState.findIndex(Boolean);
return titles?.[activeIndex] ?? null;
}

private handleScroll = () => {
Expand Down

0 comments on commit fb64395

Please sign in to comment.