Skip to content

Commit

Permalink
feat: Scroll active list item into view (#3320)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Jun 26, 2024
1 parent be6a176 commit 36016b9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
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();

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

0 comments on commit 36016b9

Please sign in to comment.