Skip to content

Commit 7eb78d3

Browse files
authored
Merge pull request #186 from rvsia/pf4-wiz-custom-func
feat(pf4wizard): allow functions in nextStep object
2 parents c93b44a + f775678 commit 7eb78d3

File tree

3 files changed

+76
-37
lines changed

3 files changed

+76
-37
lines changed

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

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
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+
};
419

520
const SimpleNext = ({
6-
next,
21+
nextStep,
722
valid,
823
handleNext,
924
submit,
1025
nextLabel,
11-
}) => (
26+
getState,
27+
}) => (
1228
<Button
1329
variant="primary"
1430
type="button"
1531
isDisabled={ !valid }
16-
onClick={ () => valid ? handleNext(next) : submit() }
32+
onClick={ () => valid ? handleNext(selectNext(nextStep, getState)) : submit() }
1733
>
1834
{ nextLabel }
1935
</Button>
@@ -25,37 +41,16 @@ SimpleNext.propTypes = {
2541
handleNext: PropTypes.func.isRequired,
2642
submit: PropTypes.func.isRequired,
2743
nextLabel: PropTypes.string.isRequired,
28-
};
29-
30-
const ConditionalNext = ({
31-
nextStep,
32-
FieldProvider,
33-
...rest
34-
}) => (
35-
<FieldProvider
36-
name={ nextStep.when }
37-
subscription={{ value: true }}
38-
>
39-
{ ({ input: { value }}) => <SimpleNext next={ nextStep.stepMapper[value] } { ...rest } /> }
40-
</FieldProvider>
41-
);
42-
43-
ConditionalNext.propTypes = {
44-
nextStep: PropTypes.shape({
45-
when: PropTypes.string.isRequired,
46-
stepMapper: PropTypes.object.isRequired,
47-
}).isRequired,
48-
FieldProvider: PropTypes.func.isRequired,
44+
getState: PropTypes.func.isRequired,
45+
nextStep: PropTypes.oneOfType([ PropTypes.string, PropTypes.object ]).isRequired,
4946
};
5047

5148
const SubmitButton = ({ handleSubmit, submitLabel }) => <Button type="button" variant="primary" onClick={ handleSubmit }>{ submitLabel }</Button>;
5249

5350
const renderNextButton = ({ nextStep, handleSubmit, submitLabel, ...rest }) =>
54-
!nextStep
55-
? <SubmitButton handleSubmit={ handleSubmit } submitLabel={ submitLabel } />
56-
: typeof nextStep === 'object'
57-
? <ConditionalNext nextStep={ nextStep } { ...rest }/>
58-
: <SimpleNext next={ nextStep } { ...rest } />;
51+
nextStep
52+
? <SimpleNext nextStep={ nextStep } { ...rest } />
53+
: <SubmitButton handleSubmit={ handleSubmit } submitLabel={ submitLabel } />;
5954

6055
const WizardStepButtons = ({
6156
buttons: Buttons,
@@ -74,7 +69,7 @@ const WizardStepButtons = ({
7469
}}) =>
7570
<footer className={ `pf-c-wizard__footer ${buttonsClassName ? buttonsClassName : ''}` }>
7671
{ Buttons ? <Buttons
77-
ConditionalNext={ ConditionalNext }
72+
ConditionalNext={ SimpleNext }
7873
SubmitButton={ SubmitButton }
7974
SimpleNext={ SimpleNext }
8075
formOptions={ formOptions }
@@ -89,18 +84,17 @@ const WizardStepButtons = ({
8984
...formOptions,
9085
handleNext,
9186
nextStep,
92-
FieldProvider,
9387
nextLabel: next,
9488
submitLabel: submit,
9589
...args,
9690
}) }
91+
selectNext={ selectNext }
9792
/>
9893
: <React.Fragment>
9994
{ renderNextButton({
10095
...formOptions,
10196
handleNext,
10297
nextStep,
103-
FieldProvider,
10498
nextLabel: next,
10599
submitLabel: submit,
106100
}) }
@@ -124,7 +118,7 @@ WizardStepButtons.propTypes = {
124118
stepMapper: PropTypes.object.isRequired,
125119
}),
126120
]),
127-
FieldProvider: PropTypes.func.isRequired,
121+
FieldProvider: PropTypes.func,
128122
buttonLabels: PropTypes.shape({
129123
submit: PropTypes.string.isRequired,
130124
cancel: PropTypes.string.isRequired,

packages/pf4-component-mapper/src/tests/wizard/step-buttons.test.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import React from 'react';
22
import { shallow, mount } from 'enzyme';
33
import { shallowToJson } from 'enzyme-to-json';
44

5-
import WizardStepButtons from '../../form-fields/wizard/step-buttons';
6-
import FieldProvider from '../../../../../__mocks__/mock-field-provider';
5+
import WizardStepButtons, { selectNext } from '../../form-fields/wizard/step-buttons';
76

87
describe('<WizardSTepButtons', () => {
98
let initialProps;
@@ -23,7 +22,6 @@ describe('<WizardSTepButtons', () => {
2322
},
2423
handlePrev: jest.fn(),
2524
handleNext: jest.fn(),
26-
FieldProvider,
2725
};
2826
});
2927

@@ -50,7 +48,10 @@ describe('<WizardSTepButtons', () => {
5048
<WizardStepButtons
5149
{ ...initialProps }
5250
handleNext={ handleNext }
53-
FieldProvider={ props => <FieldProvider input={{ value: 'foo' }} { ...props } /> }
51+
formOptions={{
52+
...initialProps.formOptions,
53+
getState: () => ({ values: { foo: 'foo' }}),
54+
}}
5455
nextStep={{
5556
when: 'foo',
5657
stepMapper: {
@@ -86,4 +87,42 @@ describe('<WizardSTepButtons', () => {
8687
wrapper.find('button').at(1).simulate('click');
8788
expect(handlePrev).toHaveBeenCalled();
8889
});
90+
91+
describe('.selectNext', () => {
92+
const VALUE = 'value';
93+
const EXPECTED_NEXT_STEP = 'barisko';
94+
95+
const GET_STATE = () => ({
96+
values: {
97+
foo: VALUE,
98+
},
99+
});
100+
101+
it('should return string nextstep', () => {
102+
const NEXTSTEP = EXPECTED_NEXT_STEP;
103+
104+
expect(selectNext(NEXTSTEP, GET_STATE)).toEqual(EXPECTED_NEXT_STEP);
105+
}),
106+
107+
it('should return stepmapper nextstep', () => {
108+
const NEXTSTEP = {
109+
when: 'foo',
110+
stepMapper: {
111+
[VALUE]: EXPECTED_NEXT_STEP,
112+
},
113+
};
114+
115+
expect(selectNext(NEXTSTEP, GET_STATE)).toEqual(EXPECTED_NEXT_STEP);
116+
});
117+
118+
it('should return custom func nextstep', () => {
119+
const NEXTSTEP = ({ values }) => {
120+
if (values.foo === VALUE) {
121+
return EXPECTED_NEXT_STEP;
122+
}
123+
};
124+
125+
expect(selectNext(NEXTSTEP, GET_STATE)).toEqual(EXPECTED_NEXT_STEP);
126+
});
127+
});
89128
});

packages/react-renderer-demo/src/docs-components/pf4-wizard.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ nextStep: {
6666
},
6767
```
6868

69+
- another option is to use custom function. The custom function receives as the first argument an object with values and the function has to return a `stepKey` in string.
70+
71+
```jsx
72+
nextStep: ({ values }) => (values.aws === '123' &&& values.password === 'secret') ? 'secretStep' : 'genericStep'
73+
```
74+
6975
#### Buttons
7076

7177
Each step can implement its own buttons.

0 commit comments

Comments
 (0)