/* eslint-disable */ // @ts-nocheck import { Grid, GridItem, GridProps, JumpLinks, JumpLinksItem, PageSection, } from "../../@patternfly/react-core"; import { Fragment, ReactNode, useEffect, useMemo, useState } from "react"; import { FormPanel } from "./FormPanel"; import { ScrollPanel } from "./ScrollPanel"; import style from "./scroll-form.module.css"; export const mainPageContentId = "kc-main-content-page-container"; type ScrollSection = { title: string; panel: ReactNode; isHidden?: boolean; }; type ScrollFormProps = GridProps & { label: string; sections: ScrollSection[]; borders?: boolean; }; const spacesToHyphens = (string: string): string => { return string.replace(/\s+/g, "-"); }; export const ScrollForm = ({ label, sections, borders = false, ...rest }: ScrollFormProps) => { const shownSections = useMemo( () => sections.filter(({ isHidden }) => !isHidden), [sections], ); const [activeSection, setActiveSection] = useState(0); useEffect(() => { const scroller = document.getElementById(mainPageContentId); if (!scroller) return; const offset = 100; const updateActive = () => { const scrollTop = scroller.scrollTop + offset; let active = 0; shownSections.forEach(({ title }, index) => { const id = spacesToHyphens(title.toLowerCase()); const el = document.getElementById(id); if (el && scrollTop >= el.offsetTop) { active = index; } }); setActiveSection(active); }; updateActive(); scroller.addEventListener("scroll", updateActive); return () => scroller.removeEventListener("scroll", updateActive); }, [shownSections]); return ( {shownSections.map(({ title, panel }) => { const scrollId = spacesToHyphens(title.toLowerCase()); return ( {borders ? ( {panel} ) : ( {panel} )} ); })} {shownSections.map(({ title }, index) => { const scrollId = spacesToHyphens(title.toLowerCase()); return ( { const element = document.getElementById(scrollId); if (element) { element.scrollIntoView({ behavior: "smooth", block: "start", }); } }} data-testid={`jump-link-${scrollId}`} > {title} ); })} ); };