Skip to content

Commit 2601568

Browse files
authored
Merge pull request #648 from rvsia/pf4StepTemplate
feat(pf4): introduce StepTemplate
2 parents 6dca607 + 885dc36 commit 2601568

File tree

10 files changed

+1184
-240
lines changed

10 files changed

+1184
-240
lines changed

packages/pf4-component-mapper/demo/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const fieldArrayState = { schema: arraySchemaDDF, additionalOptions: {
2424
class App extends React.Component {
2525
constructor(props) {
2626
super(props);
27-
this.state = {schema: selectSchema, additionalOptions: {}}
27+
this.state = {schema: wizardSchema, additionalOptions: {}}
2828
}
2929

3030
render() {

packages/pf4-component-mapper/src/files/wizard.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ export interface SubstepOfObject {
5252
title?: ReactNode;
5353
}
5454

55+
export interface StepTemplateProps {
56+
title?: ReactNode;
57+
formFields: ReactNode;
58+
showTitles?: boolean;
59+
showTitle?: boolean;
60+
customTitle?: ReactNode;
61+
fields: Field[];
62+
}
63+
5564
export interface WizardField {
5665
name: string | number;
5766
fields: Field[];
@@ -62,6 +71,8 @@ export interface WizardField {
6271
customTitle?: ReactNode;
6372
disableForwardJumping?: boolean;
6473
buttons?: ReactNode | React.ComponentType<WizardButtonsProps>;
74+
StepTemplate?: React.ComponentType<StepTemplateProps>;
75+
hasNoBodyPadding?: boolean;
6576
}
6677

6778
export interface WizardProps {
@@ -80,6 +91,7 @@ export interface WizardProps {
8091
closeButtonAriaLabel?: string;
8192
hasNoBodyPadding?: boolean;
8293
navAriaLabel?: string;
94+
StepTemplate?: React.ComponentType<StepTemplateProps>;
8395
}
8496

8597
declare const Wizard: React.ComponentType<WizardProps>;

packages/pf4-component-mapper/src/files/wizard.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const WizardInternal = ({
3838
closeButtonAriaLabel,
3939
hasNoBodyPadding,
4040
navAriaLabel,
41+
StepTemplate,
4142
...rest
4243
}) => {
4344
const {
@@ -122,15 +123,16 @@ const WizardInternal = ({
122123
</FormSpy>
123124
</WizardNav>
124125
<WizardStep
125-
{...currentStep}
126-
formOptions={formOptions}
127126
buttonLabels={buttonLabels}
128127
buttonsClassName={buttonsClassName}
129128
showTitles={showTitles}
129+
hasNoBodyPadding={hasNoBodyPadding}
130+
StepTemplate={StepTemplate}
131+
{...currentStep}
132+
formOptions={formOptions}
130133
handleNext={(nextStep) => handleNext(nextStep)}
131134
handlePrev={handlePrev}
132135
disableBack={activeStepIndex === 0}
133-
hasNoBodyPadding={hasNoBodyPadding}
134136
/>
135137
</div>
136138
</div>
@@ -157,7 +159,8 @@ WizardInternal.propTypes = {
157159
closeButtonAriaLabel: PropTypes.string,
158160
hasNoBodyPadding: PropTypes.bool,
159161
navAriaLabel: PropTypes.string,
160-
container: PropTypes.instanceOf(Element)
162+
container: PropTypes.instanceOf(Element),
163+
StepTemplate: PropTypes.elementType
161164
};
162165

163166
const defaultLabels = {

packages/pf4-component-mapper/src/files/wizard/wizard-step.js

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,65 @@ RenderTitle.propTypes = {
1717
customTitle: PropTypes.node
1818
};
1919

20-
const WizardStep = ({ name, title, description, fields, formOptions, showTitles, showTitle, customTitle, hasNoBodyPadding, ...rest }) => {
20+
const DefaultStepTemplate = ({ formFields, formRef, title, customTitle, showTitle, showTitles }) => (
21+
<div ref={formRef} className="pf-c-form">
22+
{((showTitles && showTitle !== false) || showTitle) && <RenderTitle title={title} customTitle={customTitle} />}
23+
{formFields}
24+
</div>
25+
);
26+
27+
DefaultStepTemplate.propTypes = {
28+
title: PropTypes.node,
29+
formFields: PropTypes.array.isRequired,
30+
formOptions: PropTypes.shape({
31+
renderForm: PropTypes.func.isRequired
32+
}).isRequired,
33+
showTitles: PropTypes.bool,
34+
showTitle: PropTypes.bool,
35+
customTitle: PropTypes.node,
36+
formRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({ current: PropTypes.instanceOf(Element) })])
37+
};
38+
39+
const WizardStep = ({
40+
name,
41+
title,
42+
description,
43+
fields,
44+
formOptions,
45+
showTitles,
46+
showTitle,
47+
customTitle,
48+
hasNoBodyPadding,
49+
StepTemplate,
50+
...rest
51+
}) => {
2152
const formRef = useRef();
2253

2354
useEffect(() => {
2455
// HACK: I can not pass ref to WizardBody because it is not
2556
// wrapped by forwardRef. However, the step body (the one that overflows)
2657
// is the grand parent of the form element.
2758
const stepBody = formRef.current && formRef.current.parentNode.parentNode;
28-
stepBody.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
59+
stepBody && stepBody.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
2960
}, [name]);
3061

3162
return (
3263
<Fragment>
3364
<WizardBody hasNoBodyPadding={hasNoBodyPadding}>
34-
<div ref={formRef} className="pf-c-form">
35-
{((showTitles && showTitle !== false) || showTitle) && <RenderTitle title={title} customTitle={customTitle} />}
36-
{fields.map((item) => formOptions.renderForm([item], formOptions))}
37-
</div>
65+
<StepTemplate
66+
formFields={fields.map((item) => formOptions.renderForm([item], formOptions))}
67+
name={name}
68+
title={title}
69+
description={description}
70+
formOptions={formOptions}
71+
showTitles={showTitles}
72+
showTitle={showTitle}
73+
customTitle={customTitle}
74+
hasNoBodyPadding={hasNoBodyPadding}
75+
formRef={formRef}
76+
fields={fields}
77+
{...rest}
78+
/>
3879
</WizardBody>
3980
<WizardStepButtons formOptions={formOptions} {...rest} />
4081
</Fragment>
@@ -52,7 +93,12 @@ WizardStep.propTypes = {
5293
showTitle: PropTypes.bool,
5394
customTitle: PropTypes.node,
5495
name: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
55-
hasNoBodyPadding: PropTypes.bool
96+
hasNoBodyPadding: PropTypes.bool,
97+
StepTemplate: PropTypes.elementType
98+
};
99+
100+
WizardStep.defaultProps = {
101+
StepTemplate: DefaultStepTemplate
56102
};
57103

58104
export default WizardStep;

0 commit comments

Comments
 (0)