2021-04-29 06:28:59 +00:00
|
|
|
import React, { Children, Fragment } from "react";
|
2020-12-07 12:51:38 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import {
|
|
|
|
Grid,
|
|
|
|
GridItem,
|
2021-03-19 07:49:33 +00:00
|
|
|
GridProps,
|
2020-12-07 12:51:38 +00:00
|
|
|
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
|
|
|
|
2021-03-17 13:40:19 +00:00
|
|
|
import { mainPageContentId } from "../../App";
|
2021-04-28 05:50:17 +00:00
|
|
|
import { ScrollPanel } from "./ScrollPanel";
|
2020-09-01 14:51:59 +00:00
|
|
|
import { FormPanel } from "./FormPanel";
|
2021-04-28 05:50:17 +00:00
|
|
|
|
2020-12-11 17:34:18 +00:00
|
|
|
import "./scroll-form.css";
|
2020-08-28 05:23:39 +00:00
|
|
|
|
2021-03-19 07:49:33 +00:00
|
|
|
type ScrollFormProps = GridProps & {
|
2020-08-28 05:23:39 +00:00
|
|
|
sections: string[];
|
2021-04-28 05:50:17 +00:00
|
|
|
borders?: boolean;
|
2020-08-28 05:23:39 +00:00
|
|
|
children: React.ReactNode;
|
|
|
|
};
|
|
|
|
|
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, "-");
|
|
|
|
};
|
|
|
|
|
2021-03-19 07:49:33 +00:00
|
|
|
export const ScrollForm = ({
|
|
|
|
sections,
|
2021-04-28 05:50:17 +00:00
|
|
|
borders = false,
|
2021-03-19 07:49:33 +00:00
|
|
|
children,
|
|
|
|
...rest
|
|
|
|
}: 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 (
|
2021-03-19 07:49:33 +00:00
|
|
|
<Grid hasGutter {...rest}>
|
2020-12-07 12:51:38 +00:00
|
|
|
<GridItem span={8}>
|
|
|
|
{sections.map((cat, index) => (
|
2021-04-29 06:28:59 +00:00
|
|
|
<Fragment key={cat}>
|
2021-04-28 05:50:17 +00:00
|
|
|
{!borders && (
|
2021-04-29 06:28:59 +00:00
|
|
|
<ScrollPanel scrollId={spacesToHyphens(cat)} title={cat}>
|
2021-04-28 05:50:17 +00:00
|
|
|
{nodes[index]}
|
|
|
|
</ScrollPanel>
|
|
|
|
)}
|
|
|
|
{borders && (
|
2021-05-24 04:21:49 +00:00
|
|
|
<FormPanel
|
|
|
|
scrollId={spacesToHyphens(cat)}
|
|
|
|
title={cat}
|
|
|
|
className="kc-form-panel__panel"
|
|
|
|
>
|
2021-04-28 05:50:17 +00:00
|
|
|
{nodes[index]}
|
|
|
|
</FormPanel>
|
|
|
|
)}
|
2021-04-29 06:28:59 +00:00
|
|
|
</Fragment>
|
2020-12-07 12:51:38 +00:00
|
|
|
))}
|
|
|
|
</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
|
2021-03-17 13:40:19 +00:00
|
|
|
scrollableSelector={`#${mainPageContentId}`}
|
2021-01-05 13:39:27 +00:00
|
|
|
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
|
|
|
);
|
|
|
|
};
|