Skip to content

Commit 65319fd

Browse files
committed
Handle enter on PF3 submit wizard and share code
1 parent 7d05ca2 commit 65319fd

File tree

6 files changed

+52
-42
lines changed

6 files changed

+52
-42
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import selectNext from './select-next';
2+
3+
const enterHandler = (e, formOptions, activeStep, findCurrentStep, handleNext, handleSubmit) => {
4+
if (e.key === 'Enter'){
5+
const isNotButton = e.target.type !== 'button';
6+
7+
if (isNotButton) {
8+
e.preventDefault();
9+
10+
const schemaNextStep = findCurrentStep(activeStep).nextStep;
11+
const hasCustomButtons = findCurrentStep(activeStep).buttons;
12+
13+
let nextStep;
14+
if (schemaNextStep) {
15+
nextStep = selectNext(schemaNextStep, formOptions.getState);
16+
}
17+
18+
if (formOptions.valid && nextStep && !hasCustomButtons) {
19+
handleNext(nextStep, formOptions.getRegisteredFields);
20+
} else if (formOptions.valid && !schemaNextStep && !hasCustomButtons) {
21+
handleSubmit();
22+
}
23+
}
24+
}
25+
};
26+
27+
export default enterHandler;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import get from 'lodash/get';
2+
3+
const selectNext = (nextStep, getState) => {
4+
if (typeof nextStep === 'string') {
5+
return nextStep;
6+
}
7+
8+
if (typeof nextStep === 'function') {
9+
return nextStep({ values: getState().values });
10+
}
11+
12+
const selectedValue = get(getState().values, nextStep.when);
13+
14+
return nextStep.stepMapper[selectedValue];
15+
};
16+
17+
export default selectNext;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class App extends React.Component {
114114
schemaType="default"
115115
formFieldsMapper={formFieldsMapper}
116116
layoutMapper={layoutMapper}
117-
schema={selectSchema}
117+
schema={sandbox}
118118
onCancel={() => console.log('cancel')}
119119
/>
120120
</Row>

packages/pf3-component-mapper/src/form-fields/wizzard/wizzard.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import WizardStep from './wizard-step';
33
import PropTypes from 'prop-types';
44
import { Wizard as PfWizard, Modal, Icon } from 'patternfly-react';
5+
import handleEnter from '@data-driven-forms/common/src/wizard/enter-handler';
56

67
const defaultButtonLabels = {
78
cancel: 'Cancel',
@@ -51,7 +52,8 @@ class Wizard extends React.Component {
5152
};
5253

5354
return (
54-
<React.Fragment>
55+
<div
56+
onKeyDown={ e => handleEnter(e, formOptions, this.state.activeStep, this.findCurrentStep, this.handleNext, handleSubmit) }>
5557
{ title && <Modal.Header>
5658
{ inModal && <button className="close" onClick={ formOptions.onCancel } aria-hidden="true" aria-label="Close">
5759
<Icon type="pf" name="close" />
@@ -71,7 +73,7 @@ class Wizard extends React.Component {
7173
}}
7274
FieldProvider={ FieldProvider }
7375
/>
74-
</React.Fragment>
76+
</div>
7577
);
7678
}
7779
}

packages/pf4-component-mapper/src/form-fields/wizard/step-buttons.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import { Button } from '@patternfly/react-core';
4-
import get from 'lodash/get';
5-
6-
export const selectNext = (nextStep, getState) => {
7-
if (typeof nextStep === 'string') {
8-
return nextStep;
9-
}
10-
11-
if (typeof nextStep === 'function') {
12-
return nextStep({ values: getState().values });
13-
}
14-
15-
const selectedValue = get(getState().values, nextStep.when);
16-
17-
return nextStep.stepMapper[selectedValue];
18-
};
4+
import selectNext from '@data-driven-forms/common/src/wizard/select-next';
195

206
const SimpleNext = ({
217
nextStep,

packages/pf4-component-mapper/src/form-fields/wizard/wizard.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import './wizard-styles.scss';
77
import get from 'lodash/get';
88
import set from 'lodash/set';
99
import flattenDeep from 'lodash/flattenDeep';
10-
import { selectNext } from './step-buttons';
10+
import handleEnter from '@data-driven-forms/common/src/wizard/enter-handler';
1111

1212
const DYNAMIC_WIZARD_TYPES = [ 'function', 'object' ];
1313

@@ -247,29 +247,7 @@ class Wizard extends React.Component {
247247
<div className={ `pf-c-wizard ${inModal ? '' : 'no-shadow'} ${isCompactNav ? 'pf-m-compact-nav' : ''} ${setFullWidth ? 'pf-m-full-width' : ''} ${setFullHeight ? 'pf-m-full-height' : ''}` }
248248
role="dialog"
249249
aria-modal={ inModal ? 'true' : undefined }
250-
onKeyDown={ e => {
251-
if (e.key === 'Enter'){
252-
const isNotButton = e.target.type !== 'button';
253-
254-
if (isNotButton) {
255-
e.preventDefault();
256-
257-
const schemaNextStep = this.findCurrentStep(this.state.activeStep).nextStep;
258-
const hasCustomButtons = this.findCurrentStep(this.state.activeStep).buttons;
259-
260-
let nextStep;
261-
if (schemaNextStep) {
262-
nextStep = selectNext(schemaNextStep, formOptions.getState);
263-
}
264-
265-
if (formOptions.valid && nextStep && !hasCustomButtons) {
266-
this.handleNext(nextStep, formOptions.getRegisteredFields);
267-
} else if (formOptions.valid && !schemaNextStep && !hasCustomButtons) {
268-
handleSubmit();
269-
}
270-
}
271-
}
272-
} }
250+
onKeyDown={ e => handleEnter(e, formOptions, this.state.activeStep, this.findCurrentStep, this.handleNext, handleSubmit) }
273251
>
274252
{ title && <WizardHeader
275253
title={ title }

0 commit comments

Comments
 (0)