Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Scroll active list item into view #3320

Merged
merged 2 commits into from
Jun 26, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { mockMaxOneProps } from "../schemas/mocks/MaxOne";
import { mockZooPayload, mockZooProps } from "../schemas/mocks/Zoo";

jest.setTimeout(20_000);
Element.prototype.scrollIntoView = jest.fn();
Copy link
Contributor Author

Choose a reason for hiding this comment

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


describe("Basic UI", () => {
it("renders correctly", () => {
Expand Down
13 changes: 10 additions & 3 deletions editor.planx.uk/src/@planx/components/List/Public/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import TableCell from "@mui/material/TableCell";
import TableRow from "@mui/material/TableRow";
import Typography from "@mui/material/Typography";
import { PublicProps } from "@planx/components/ui";
import React from "react";
import React, { useEffect, useRef } from "react";
import { FONT_WEIGHT_SEMI_BOLD } from "theme";
import ErrorWrapper from "ui/shared/ErrorWrapper";
import InputRow from "ui/shared/InputRow";
Expand Down Expand Up @@ -67,12 +67,19 @@ const ActiveListCard: React.FC<{
}> = ({ index: i }) => {
const { schema, saveItem, cancelEditItem, errors, isPageComponent } =
useListContext();


const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
if (ref.current) {
ref.current.scrollIntoView({ behavior: "smooth" })
}
}, []);

return (
<ErrorWrapper
error={errors.unsavedItem ? "Please save in order to continue" : ""}
>
<ListCard data-testid={`list-card-${i}`}>
<ListCard data-testid={`list-card-${i}`} ref={ref}>
<Typography component="h2" variant="h3">
{schema.type}
{!isPageComponent && ` ${i + 1}`}
Expand Down
Loading