Skip to content

Commit 7c98357

Browse files
authored
Merge pull request #1578 from LightOfHeaven1994/show-submit-button-correctly-progress-wizard
fix(wizard): show submit button correctly
2 parents 0aa01a3 + c7f896f commit 7c98357

File tree

6 files changed

+79
-14
lines changed

6 files changed

+79
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ All packages are linked together by default, so if you run a `yarn build` in a p
270270
Each package has a small playground `package/demo`, where you can test your changes.
271271

272272
```bash
273-
cd packages/pf3-component-mapper
273+
cd packages/pf4-component-mapper
274274
yarn start
275275
```
276276

packages/common/src/wizard/wizard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ const WizardComponent: React.FC<WizardProps> = ({
127127
selectNext,
128128
}}
129129
>
130-
<WizardRenderer conditionalSubmitFlag={conditionalSubmitFlag} {...props} />
130+
<WizardRenderer conditionalSubmitFlag={conditionalSubmitFlag} fields={fields} {...props} />
131131
</WizardContext.Provider>
132132
);
133133
};

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,40 @@ describe('<WizardSTepButtons', () => {
287287
});
288288
});
289289

290+
it('shows submit button and advances when next step has isProgressAfterSubmissionStep', async () => {
291+
const handleSubmit = jest.fn();
292+
const handleNext = jest.fn();
293+
const wizardFields = [
294+
{ name: 'step-review', nextStep: 'step-finished', fields: [] },
295+
{ name: 'step-finished', isProgressAfterSubmissionStep: true, fields: [] },
296+
];
297+
298+
render(
299+
<RenderWithProvider>
300+
<WizardStepButtons
301+
{...initialProps}
302+
formOptions={{
303+
...initialProps.formOptions,
304+
handleSubmit,
305+
getState: () => ({ values: {}, validating: false }),
306+
}}
307+
handleNext={handleNext}
308+
nextStep="step-finished"
309+
wizardFields={wizardFields}
310+
buttonLabels={{ ...initialProps.buttonLabels, submit: 'Finish' }}
311+
/>
312+
</RenderWithProvider>
313+
);
314+
315+
expect(screen.getByText('Finish')).toBeInTheDocument();
316+
expect(screen.queryByText('Next')).not.toBeInTheDocument();
317+
318+
await userEvent.click(screen.getByText('Finish'));
319+
320+
expect(handleSubmit).toHaveBeenCalled();
321+
expect(handleNext).toHaveBeenCalledWith('step-finished');
322+
});
323+
290324
it('conditional submit step', async () => {
291325
const submit = jest.fn();
292326
const schema = {

packages/pf4-component-mapper/src/wizard/wizard-components/step-buttons.tsx

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface NextButtonProps {
1919
handleSubmit: () => void;
2020
submitLabel: string;
2121
conditionalSubmitFlag?: any;
22+
wizardFields?: Array<{ name: string; isProgressAfterSubmissionStep?: boolean }>;
2223
}
2324

2425
const NextButton: React.FC<NextButtonProps> = ({
@@ -30,15 +31,32 @@ const NextButton: React.FC<NextButtonProps> = ({
3031
handleSubmit,
3132
submitLabel,
3233
conditionalSubmitFlag,
34+
wizardFields,
3335
}) => {
3436
const nextResult = nextStep ? selectNext(nextStep, getState) : nextStep;
35-
const progressNext = nextResult !== conditionalSubmitFlag && nextStep;
37+
const isNextStepProgressAfterSubmission = Boolean(
38+
wizardFields?.find((f) => f.name === nextResult)?.isProgressAfterSubmissionStep
39+
);
40+
const progressNext =
41+
nextStep && nextResult !== conditionalSubmitFlag && !isNextStepProgressAfterSubmission;
42+
43+
const onClick = () => {
44+
if (progressNext) {
45+
handleNext(selectNext(nextStep, getState));
46+
} else {
47+
handleSubmit();
48+
if (isNextStepProgressAfterSubmission && nextResult) {
49+
handleNext(nextResult);
50+
}
51+
}
52+
};
53+
3654
return (
3755
<Button
3856
variant="primary"
3957
type="button"
4058
isDisabled={!valid || getState().validating}
41-
onClick={() => (progressNext ? handleNext(selectNext(nextStep, getState)) : handleSubmit())}
59+
onClick={onClick}
4260
>
4361
{progressNext ? nextLabel : submitLabel}
4462
</Button>
@@ -68,6 +86,7 @@ const WizardStepButtons: React.FC<WizardStepButtonsProps> = ({
6886
buttonLabels: { cancel, submit, back, next },
6987
formOptions,
7088
conditionalSubmitFlag,
89+
wizardFields,
7190
}) => (
7291
<footer className={`pf-v6-c-wizard__footer ${buttonsClassName ? buttonsClassName : ''}`}>
7392
<ActionList>
@@ -80,7 +99,15 @@ const WizardStepButtons: React.FC<WizardStepButtonsProps> = ({
8099
buttonsClassName={buttonsClassName}
81100
buttonLabels={{ cancel, submit, back, next }}
82101
renderNextButton={(args: any) => (
83-
<NextButton {...formOptions} handleNext={handleNext} nextStep={nextStep} nextLabel={next} submitLabel={submit} {...args} />
102+
<NextButton
103+
{...formOptions}
104+
handleNext={handleNext}
105+
nextStep={nextStep}
106+
nextLabel={next}
107+
submitLabel={submit}
108+
wizardFields={wizardFields}
109+
{...args}
110+
/>
84111
)}
85112
selectNext={selectNext}
86113
/>
@@ -90,14 +117,15 @@ const WizardStepButtons: React.FC<WizardStepButtonsProps> = ({
90117
<React.Fragment>
91118
<ActionListGroup>
92119
<ActionListItem>
93-
<NextButton
94-
{...formOptions}
95-
conditionalSubmitFlag={conditionalSubmitFlag}
96-
handleNext={handleNext}
97-
nextStep={nextStep}
98-
nextLabel={next}
99-
submitLabel={submit}
100-
/>
120+
<NextButton
121+
{...formOptions}
122+
conditionalSubmitFlag={conditionalSubmitFlag}
123+
handleNext={handleNext}
124+
nextStep={nextStep}
125+
nextLabel={next}
126+
submitLabel={submit}
127+
wizardFields={wizardFields}
128+
/>
101129
</ActionListItem>
102130
<ActionListItem>
103131
<Button type="button" variant="secondary" isDisabled={disableBack} onClick={handlePrev}>

packages/pf4-component-mapper/src/wizard/wizard-components/wizard-step.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const WizardStep: React.FC<WizardStepProps> = ({
5858
customTitle,
5959
hasNoBodyPadding,
6060
StepTemplate = DefaultStepTemplate,
61+
wizardFields,
6162
...rest
6263
}) => {
6364
const formRef = useRef<HTMLDivElement>(null);
@@ -88,7 +89,7 @@ const WizardStep: React.FC<WizardStepProps> = ({
8889
{...rest}
8990
/>
9091
</WizardBody>
91-
<WizardStepButtons formOptions={formOptions} {...rest} />
92+
<WizardStepButtons formOptions={formOptions} wizardFields={wizardFields} {...rest} />
9293
</Fragment>
9394
);
9495
};

packages/pf4-component-mapper/src/wizard/wizard.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const WizardInternal: React.FC<WizardInternalProps> = ({
6868
className,
6969
conditionalSubmitFlag,
7070
ModalProps = {},
71+
fields: wizardFields,
7172
...rest
7273
}) => {
7374
const {
@@ -199,6 +200,7 @@ const WizardInternal: React.FC<WizardInternalProps> = ({
199200
showTitles={showTitles}
200201
hasNoBodyPadding={hasNoBodyPadding}
201202
StepTemplate={StepTemplate}
203+
wizardFields={wizardFields}
202204
{...currentStep}
203205
formOptions={formOptions}
204206
handleNext={(nextStep: any) => handleNext(nextStep)}

0 commit comments

Comments
 (0)