2022-08-03 12:12:07 +00:00
|
|
|
import { Fragment, FunctionComponent, ReactNode, useMemo } 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
|
|
|
|
2022-05-30 11:07:33 +00:00
|
|
|
type ScrollSection = {
|
|
|
|
title: string;
|
|
|
|
panel: ReactNode;
|
|
|
|
isHidden?: boolean;
|
|
|
|
};
|
|
|
|
|
2021-03-19 07:49:33 +00:00
|
|
|
type ScrollFormProps = GridProps & {
|
2022-05-30 11:07:33 +00:00
|
|
|
sections: ScrollSection[];
|
2021-04-28 05:50:17 +00:00
|
|
|
borders?: boolean;
|
2020-08-28 05:23:39 +00:00
|
|
|
};
|
|
|
|
|
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-08-10 11:18:48 +00:00
|
|
|
export const ScrollForm: FunctionComponent<ScrollFormProps> = ({
|
2021-03-19 07:49:33 +00:00
|
|
|
sections,
|
2021-04-28 05:50:17 +00:00
|
|
|
borders = false,
|
2021-03-19 07:49:33 +00:00
|
|
|
...rest
|
2021-08-10 11:18:48 +00:00
|
|
|
}) => {
|
2020-12-07 12:51:38 +00:00
|
|
|
const { t } = useTranslation("common");
|
2022-05-30 11:07:33 +00:00
|
|
|
const shownSections = useMemo(
|
|
|
|
() => sections.filter(({ isHidden }) => !isHidden),
|
|
|
|
[sections]
|
|
|
|
);
|
2021-10-06 11:05:27 +00:00
|
|
|
|
2020-08-28 05:23:39 +00:00
|
|
|
return (
|
2021-03-19 07:49:33 +00:00
|
|
|
<Grid hasGutter {...rest}>
|
2020-12-07 12:51:38 +00:00
|
|
|
<GridItem span={8}>
|
2022-05-30 11:07:33 +00:00
|
|
|
{shownSections.map(({ title, panel }) => {
|
|
|
|
const scrollId = spacesToHyphens(title.toLowerCase());
|
2021-10-06 11:05:27 +00:00
|
|
|
|
|
|
|
return (
|
2022-05-30 11:07:33 +00:00
|
|
|
<Fragment key={title}>
|
|
|
|
{borders ? (
|
2021-10-06 11:05:27 +00:00
|
|
|
<FormPanel
|
|
|
|
scrollId={scrollId}
|
2022-05-30 11:07:33 +00:00
|
|
|
title={title}
|
2021-10-06 11:05:27 +00:00
|
|
|
className="kc-form-panel__panel"
|
|
|
|
>
|
2022-05-30 11:07:33 +00:00
|
|
|
{panel}
|
2021-10-06 11:05:27 +00:00
|
|
|
</FormPanel>
|
2022-05-30 11:07:33 +00:00
|
|
|
) : (
|
|
|
|
<ScrollPanel scrollId={scrollId} title={title}>
|
|
|
|
{panel}
|
|
|
|
</ScrollPanel>
|
2021-10-06 11:05:27 +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
|
|
|
>
|
2022-05-30 11:07:33 +00:00
|
|
|
{shownSections.map(({ title }) => {
|
|
|
|
const scrollId = spacesToHyphens(title.toLowerCase());
|
2021-10-06 11:05:27 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
// note that JumpLinks currently does not work with spaces in the href
|
|
|
|
<JumpLinksItem
|
2022-05-30 11:07:33 +00:00
|
|
|
key={title}
|
2021-10-06 11:05:27 +00:00
|
|
|
href={`#${scrollId}`}
|
|
|
|
data-testid={`jump-link-${scrollId}`}
|
|
|
|
>
|
2022-05-30 11:07:33 +00:00
|
|
|
{title}
|
2021-10-06 11:05:27 +00:00
|
|
|
</JumpLinksItem>
|
|
|
|
);
|
|
|
|
})}
|
2021-01-05 13:39:27 +00:00
|
|
|
</JumpLinks>
|
|
|
|
</PageSection>
|
2020-12-07 12:51:38 +00:00
|
|
|
</GridItem>
|
|
|
|
</Grid>
|
2020-08-28 05:23:39 +00:00
|
|
|
);
|
|
|
|
};
|