forked from data-driven-forms/react-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwizard-step.tsx
More file actions
97 lines (88 loc) · 2.62 KB
/
Copy pathwizard-step.tsx
File metadata and controls
97 lines (88 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import React, { Fragment, useEffect, useRef, ReactNode } from 'react';
import { Title, WizardBody } from '@patternfly/react-core';
import WizardStepButtons from './step-buttons';
interface RenderTitleProps {
title: string;
customTitle?: ReactNode;
}
export const RenderTitle: React.FC<RenderTitleProps> = ({ title, customTitle }) =>
customTitle ? (
customTitle
) : (
<Title headingLevel="h1" size="xl">
{title}
</Title>
);
interface DefaultStepTemplateProps {
formFields: ReactNode[];
formRef: React.RefObject<HTMLDivElement>;
title: string;
customTitle?: ReactNode;
showTitle?: boolean;
showTitles?: boolean;
[key: string]: any;
}
const DefaultStepTemplate: React.FC<DefaultStepTemplateProps> = ({ formFields, formRef, title, customTitle, showTitle, showTitles }) => (
<div ref={formRef} className="pf-c-form">
{((showTitles && showTitle !== false) || showTitle) && <RenderTitle title={title} customTitle={customTitle} />}
{formFields}
</div>
);
interface WizardStepProps {
name: string;
title?: string;
description?: string;
fields?: any[];
formOptions: any;
showTitles?: boolean;
showTitle?: boolean;
customTitle?: ReactNode;
hasNoBodyPadding?: boolean;
StepTemplate?: React.ComponentType<any>;
[key: string]: any;
}
const WizardStep: React.FC<WizardStepProps> = ({
name,
title,
description,
fields = [],
formOptions,
showTitles,
showTitle,
customTitle,
hasNoBodyPadding,
StepTemplate = DefaultStepTemplate,
wizardFields,
...rest
}) => {
const formRef = useRef<HTMLDivElement>(null);
useEffect(() => {
// HACK: I can not pass ref to WizardBody because it is not
// wrapped by forwardRef. However, the step body (the one that overflows)
// is the grand parent of the form element.
const stepBody = formRef.current && (formRef.current.parentNode?.parentNode as HTMLElement);
stepBody && stepBody.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
}, [name]);
return (
<Fragment>
<WizardBody hasNoPadding={hasNoBodyPadding}>
<StepTemplate
formFields={fields.map((item) => formOptions.renderForm([item]))}
name={name}
title={title}
description={description}
formOptions={formOptions}
showTitles={showTitles}
showTitle={showTitle}
customTitle={customTitle}
hasNoBodyPadding={hasNoBodyPadding}
formRef={formRef}
fields={fields}
{...rest}
/>
</WizardBody>
<WizardStepButtons formOptions={formOptions} wizardFields={wizardFields} {...rest} />
</Fragment>
);
};
export default WizardStep;