2021-01-05 13:39:27 +00:00
|
|
|
import React, { Children } from "react";
|
2020-12-07 12:51:38 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import {
|
|
|
|
Grid,
|
|
|
|
GridItem,
|
|
|
|
JumpLinks,
|
|
|
|
JumpLinksItem,
|
2020-12-11 17:34:18 +00:00
|
|
|
PageSection,
|
2020-12-07 12:51:38 +00:00
|
|
|
} from "@patternfly/react-core";
|
2020-08-28 05:23:39 +00:00
|
|
|
|
2020-09-01 14:51:59 +00:00
|
|
|
import { FormPanel } from "./FormPanel";
|
2020-12-11 17:34:18 +00:00
|
|
|
import "./scroll-form.css";
|
2020-08-28 05:23:39 +00:00
|
|
|
|
|
|
|
type ScrollFormProps = {
|
|
|
|
sections: string[];
|
|
|
|
children: React.ReactNode;
|
|
|
|
};
|
|
|
|
|
2020-12-11 17:34:18 +00:00
|
|
|
// This must match the page id created in App.tsx unless another page section has been given hasScrollableContent
|
|
|
|
const mainPageContentId = "#kc-main-content-page-container";
|
|
|
|
|
2021-01-05 13:39:27 +00:00
|
|
|
const spacesToHyphens = (string: string): string => {
|
2020-12-11 17:34:18 +00:00
|
|
|
return string.replace(/\s+/g, "-");
|
|
|
|
};
|
|
|
|
|
2020-08-28 05:23:39 +00:00
|
|
|
export const ScrollForm = ({ sections, children }: ScrollFormProps) => {
|
2020-12-07 12:51:38 +00:00
|
|
|
const { t } = useTranslation("common");
|
2020-08-28 05:23:39 +00:00
|
|
|
|
|
|
|
const nodes = Children.toArray(children);
|
|
|
|
return (
|
2020-12-07 12:51:38 +00:00
|
|
|
<Grid hasGutter>
|
|
|
|
<GridItem span={8}>
|
|
|
|
{sections.map((cat, index) => (
|
2020-12-11 17:34:18 +00:00
|
|
|
<FormPanel scrollId={spacesToHyphens(cat)} key={cat} title={cat}>
|
2020-12-07 12:51:38 +00:00
|
|
|
{nodes[index]}
|
|
|
|
</FormPanel>
|
|
|
|
))}
|
|
|
|
</GridItem>
|
|
|
|
<GridItem span={4}>
|
2021-01-05 13:39:27 +00:00
|
|
|
<PageSection className="kc-scroll-form--sticky">
|
|
|
|
<JumpLinks
|
|
|
|
isVertical
|
|
|
|
// scrollableSelector has to point to the id of the element whose scrollTop changes
|
|
|
|
// to scroll the entire main section, it has to be the pf-c-page__main
|
|
|
|
scrollableSelector={mainPageContentId}
|
|
|
|
label={t("jumpToSection")}
|
2021-01-04 21:33:18 +00:00
|
|
|
offset={100}
|
2021-01-05 13:39:27 +00:00
|
|
|
>
|
|
|
|
{sections.map((cat) => (
|
|
|
|
// note that JumpLinks currently does not work with spaces in the href
|
|
|
|
<JumpLinksItem key={cat} href={`#${spacesToHyphens(cat)}`}>
|
|
|
|
{cat}
|
|
|
|
</JumpLinksItem>
|
|
|
|
))}
|
|
|
|
</JumpLinks>
|
|
|
|
</PageSection>
|
2020-12-07 12:51:38 +00:00
|
|
|
</GridItem>
|
|
|
|
</Grid>
|
2020-08-28 05:23:39 +00:00
|
|
|
);
|
|
|
|
};
|