Skip to content

Commit

Permalink
refactor: scrolling back up to top of listpage view when at bottom of…
Browse files Browse the repository at this point in the history
… listpage
  • Loading branch information
eternalmaha committed Oct 17, 2024
1 parent ea845fb commit 6cfaca2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
9 changes: 1 addition & 8 deletions src/components/forms/AddItemForm.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useRef } from "react";
import { ChangeEvent, FormEvent, useState } from "react";
import { addItem, ListItem } from "../../api";
import { validateItemName } from "../../utils";
Expand Down Expand Up @@ -96,13 +97,6 @@ export function AddItemForm({ listPath, data: unfilteredListItems }: Props) {
}
};

const navigateToListPage = () => {
if (listPath) {
const listName = listPath.split("/").pop();
navigate(`/list/${listName}`);
}
};

return (
<section className="custom-borders d-flex flex-column align-items-center">
<Form onSubmit={(e) => handleSubmit(e, listPath)}>
Expand Down Expand Up @@ -188,7 +182,6 @@ export function AddItemForm({ listPath, data: unfilteredListItems }: Props) {
<Button type="submit" aria-label="Add item to shopping list">
Submit Item
</Button>
<Button onClick={navigateToListPage}>{"View List"}</Button>
</section>
);
}
13 changes: 12 additions & 1 deletion src/views/authenticated/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ export function List({ data: unfilteredListItems, listPath }: Props) {
}
};

const viewListRef = useRef<HTMLElement | null>(null);

// Function to handle scrolling to the Add-ShareList section
const scrollToViewList = () => {
if (viewListRef.current) {
viewListRef.current.scrollIntoView({ behavior: "smooth" });
}
};

// Main content when list is not empty
return (
<Container className="ListPageContainer">
Expand All @@ -102,7 +111,7 @@ export function List({ data: unfilteredListItems, listPath }: Props) {
</Button>
</section>

<section>
<section ref={viewListRef}>
{filteredListItems.map((item) => (
<ListItemCheckBox key={item.id} item={item} listPath={listPath} />
))}
Expand All @@ -120,6 +129,8 @@ export function List({ data: unfilteredListItems, listPath }: Props) {
<ShareListForm listPath={listPath} />
</div>
</section>

<Button onClick={scrollToViewList}>{"View List"}</Button>
</Container>
);
}

0 comments on commit 6cfaca2

Please sign in to comment.