@@ -4,38 +4,59 @@ import { Title } from '@patternfly/react-core/dist/js/components/Title/Title';
4
4
import PropTypes from 'prop-types' ;
5
5
import WizardStepButtons from './step-buttons' ;
6
6
7
- export const RenderTitle = ( { title, customTitle } ) => customTitle ? customTitle : < Title headingLevel = "h1" size = "xl" > { title } </ Title > ;
7
+ export const RenderTitle = ( { title, customTitle } ) => customTitle ? customTitle : < Title headingLevel = "h1" size = "xl" > { title } </ Title > ;
8
8
9
9
RenderTitle . propTypes = {
10
10
title : PropTypes . node ,
11
11
customTitle : PropTypes . node ,
12
12
} ;
13
13
14
- const WizardStep = ( {
15
- title,
16
- description,
17
- fields,
18
- formOptions,
19
- showTitles,
20
- showTitle,
21
- customTitle,
22
- ...rest
23
- } ) => {
24
- return (
25
- < Fragment >
26
- < WizardBody hasBodyPadding = { true } >
27
- < div className = "pf-c-form" >
28
- { ( ( showTitles && showTitle !== false ) || showTitle ) && < RenderTitle title = { title } customTitle = { customTitle } /> }
29
- { fields . map ( item => formOptions . renderForm ( [ item ] , formOptions ) ) }
30
- </ div >
31
- </ WizardBody >
32
- < WizardStepButtons
33
- formOptions = { formOptions }
34
- { ...rest }
35
- />
36
- </ Fragment >
37
- ) ;
38
- } ;
14
+ class WizardStep extends React . Component {
15
+ formRef = React . createRef ( ) ;
16
+ componentDidUpdate ( prevProps ) {
17
+ // we want to scroll to top of the new step so
18
+ // the user experience won't suck. For instance,
19
+ // when the first step contains many fields that you have to scroll down
20
+ // to fill all the data for the next step. If the next step contains instructions
21
+ // at the top, the user will miss them because the scrollbar offset will stay at
22
+ // the same place it was.
23
+ if ( prevProps . stepKey !== this . props . stepKey ) {
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 = this . formRef . current && this . formRef . current . parentNode . parentNode ;
28
+ stepBody . scrollTo ( { top : 0 , left : 0 , behavior : 'smooth' } ) ;
29
+ }
30
+ }
31
+
32
+ render ( ) {
33
+ const {
34
+ title,
35
+ description,
36
+ fields,
37
+ formOptions,
38
+ showTitles,
39
+ showTitle,
40
+ customTitle,
41
+ ...rest
42
+ } = this . props ;
43
+
44
+ return (
45
+ < Fragment >
46
+ < WizardBody hasBodyPadding = { true } >
47
+ < div ref = { this . formRef } className = "pf-c-form" >
48
+ { ( ( showTitles && showTitle !== false ) || showTitle ) && < RenderTitle title = { title } customTitle = { customTitle } /> }
49
+ { fields . map ( item => formOptions . renderForm ( [ item ] , formOptions ) ) }
50
+ </ div >
51
+ </ WizardBody >
52
+ < WizardStepButtons
53
+ formOptions = { formOptions }
54
+ { ...rest }
55
+ />
56
+ </ Fragment >
57
+ ) ;
58
+ }
59
+ }
39
60
40
61
WizardStep . propTypes = {
41
62
title : PropTypes . node ,
0 commit comments