1- import React , { Fragment } from 'react' ;
1+ import React , { Fragment , useEffect , useRef } from 'react' ;
22import { Title , WizardBody } from '@patternfly/react-core' ;
33import PropTypes from 'prop-types' ;
44import 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
5544WizardStep . propTypes = {
5645 title : PropTypes . node ,
0 commit comments