Skip to content

Commit 68d91a7

Browse files
committed
fix(pf4): convert wizardstep to function
1 parent f9afa52 commit 68d91a7

File tree

2 files changed

+22
-35
lines changed

2 files changed

+22
-35
lines changed

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

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Fragment } from 'react';
1+
import React, { Fragment, useEffect, useRef } from 'react';
22
import { Title, WizardBody } from '@patternfly/react-core';
33
import PropTypes from 'prop-types';
44
import WizardStepButtons from './step-buttons';
@@ -17,40 +17,29 @@ RenderTitle.propTypes = {
1717
customTitle: PropTypes.node
1818
};
1919

20-
class WizardStep extends React.Component {
21-
formRef = React.createRef();
22-
componentDidUpdate(prevProps) {
23-
// we want to scroll to top of the new step so
24-
// the user experience won't suck. For instance,
25-
// when the first step contains many fields that you have to scroll down
26-
// to fill all the data for the next step. If the next step contains instructions
27-
// at the top, the user will miss them because the scrollbar offset will stay at
28-
// the same place it was.
29-
if (prevProps.name !== this.props.name) {
30-
// HACK: I can not pass ref to WizardBody because it is not
31-
// wrapped by forwardRef. However, the step body (the one that overflows)
32-
// is the grand parent of the form element.
33-
const stepBody = this.formRef.current && this.formRef.current.parentNode.parentNode;
34-
stepBody.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
35-
}
36-
}
20+
const WizardStep = ({ name, title, description, fields, formOptions, showTitles, showTitle, customTitle, ...rest }) => {
21+
const formRef = useRef();
3722

38-
render() {
39-
const { title, description, fields, formOptions, showTitles, showTitle, customTitle, ...rest } = this.props;
23+
useEffect(() => {
24+
// HACK: I can not pass ref to WizardBody because it is not
25+
// wrapped by forwardRef. However, the step body (the one that overflows)
26+
// is the grand parent of the form element.
27+
const stepBody = formRef.current && formRef.current.parentNode.parentNode;
28+
stepBody.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
29+
}, [name]);
4030

41-
return (
42-
<Fragment>
43-
<WizardBody hasBodyPadding={true}>
44-
<div ref={this.formRef} className="pf-c-form">
45-
{((showTitles && showTitle !== false) || showTitle) && <RenderTitle title={title} customTitle={customTitle} />}
46-
{fields.map((item) => formOptions.renderForm([item], formOptions))}
47-
</div>
48-
</WizardBody>
49-
<WizardStepButtons formOptions={formOptions} {...rest} />
50-
</Fragment>
51-
);
52-
}
53-
}
31+
return (
32+
<Fragment>
33+
<WizardBody hasBodyPadding={true}>
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>
38+
</WizardBody>
39+
<WizardStepButtons formOptions={formOptions} {...rest} />
40+
</Fragment>
41+
);
42+
};
5443

5544
WizardStep.propTypes = {
5645
title: PropTypes.node,

packages/pf4-component-mapper/src/tests/wizard/__snapshots__/wizard.test.js.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,6 @@ exports[`<Wizard /> should render correctly and unmount 1`] = `
928928
}
929929
handleNext={[Function]}
930930
handlePrev={[Function]}
931-
name="1"
932931
nextStep="2"
933932
>
934933
<footer
@@ -2329,7 +2328,6 @@ exports[`<Wizard /> should render correctly in modal and unmount 1`] = `
23292328
}
23302329
handleNext={[Function]}
23312330
handlePrev={[Function]}
2332-
name="1"
23332331
nextStep="2"
23342332
>
23352333
<footer

0 commit comments

Comments
 (0)