Skip to content

Commit e9777a6

Browse files
authored
Merge pull request #631 from rvsia/updateWizard
fix(ant): update wizard to latest changes
2 parents 59a9159 + fb43b35 commit e9777a6

File tree

9 files changed

+432
-83
lines changed

9 files changed

+432
-83
lines changed

config/jest.setup.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,17 @@ import Adapter from 'enzyme-adapter-react-16';
33

44
configure({ adapter: new Adapter() });
55
Element.prototype.scrollTo = () => {};
6+
7+
Object.defineProperty(window, 'matchMedia', {
8+
writable: true,
9+
value: jest.fn().mockImplementation((query) => ({
10+
matches: false,
11+
media: query,
12+
onchange: null,
13+
addListener: jest.fn(), // deprecated
14+
removeListener: jest.fn(), // deprecated
15+
addEventListener: jest.fn(),
16+
removeEventListener: jest.fn(),
17+
dispatchEvent: jest.fn()
18+
}))
19+
});

packages/ant-component-mapper/demo/demo-schemas/wizard-schema.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ const wizardSchema = {
8383
label: 'Hello this is a test and this shall be a text'
8484
},
8585
{
86-
nextStep: 'holaname',
8786
fields: [
8887
{
8988
name: 'summary',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const App = () => (
2020
componentMapper={componentMapper}
2121
FormTemplate={(props) => <FormTemplate layout='vertical' {...props} />}
2222
onSubmit={console.log}
23-
schema={demoSchema}
23+
schema={wizardSchema}
2424
/>
2525
</div>
2626
);

packages/ant-component-mapper/src/common/form-wrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ AntForm.propTypes = {
1818
help: PropTypes.string,
1919
isRequired: PropTypes.bool,
2020
component: PropTypes.string,
21-
invalid: PropTypes.oneOfType(PropTypes.string, PropTypes.bool)
21+
invalid: PropTypes.oneOfType([PropTypes.string, PropTypes.bool])
2222
};
2323

2424
export default AntForm;
Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import React from 'react';
1+
import React, { useContext } from 'react';
22
import WizardStep from './wizard/wizard-step';
33
import PropTypes from 'prop-types';
44
import { Steps, Modal } from 'antd';
5-
import Wizard, { wizardProps } from '@data-driven-forms/common/src/wizard/wizard';
5+
import Wizard from '@data-driven-forms/common/src/wizard/wizard';
6+
import { WizardContext } from '@data-driven-forms/react-form-renderer';
67

78
const defaultButtonLabels = {
89
cancel: 'Cancel',
@@ -13,27 +14,17 @@ const defaultButtonLabels = {
1314

1415
const { Step } = Steps;
1516

16-
const WizardInternal = ({
17-
title,
18-
buttonLabels,
19-
stepsInfo,
20-
inModal,
21-
onKeyDown,
22-
formOptions,
23-
handleNext,
24-
handlePrev,
25-
prevSteps,
26-
currentStep,
27-
jumpToStep,
28-
activeStepIndex
29-
}) => {
17+
const WizardInternal = ({ title, buttonLabels, stepsInfo }) => {
18+
const { onKeyDown, formOptions, handleNext, handlePrev, prevSteps, currentStep, jumpToStep, activeStepIndex } = useContext(WizardContext);
19+
3020
const renderSteps = () =>
3121
stepsInfo.map((step, stepIndex) => <Step title={step.title} disabled={activeStepIndex < stepIndex} step={stepIndex} key={stepIndex} />);
3222

3323
const fullButtonLabels = {
3424
...defaultButtonLabels,
3525
...buttonLabels
3626
};
27+
3728
return (
3829
<div onKeyDown={onKeyDown}>
3930
{title && <Modal title={title} onCancel={formOptions.onCanel}></Modal>}
@@ -57,19 +48,9 @@ const WizardInternal = ({
5748
WizardInternal.propTypes = {
5849
title: PropTypes.string,
5950
buttonLabels: PropTypes.object,
60-
stepsInfo: PropTypes.array,
61-
inModal: PropTypes.bool,
62-
...wizardProps
51+
stepsInfo: PropTypes.array
6352
};
6453

65-
WizardInternal.defaultProps = {
66-
title: undefined,
67-
stepsInfo: undefined,
68-
inModal: false
69-
};
70-
71-
const WizardFinal = (props) => {
72-
return <Wizard Wizard={WizardInternal} {...props} />;
73-
};
54+
const WizardFinal = (props) => <Wizard Wizard={WizardInternal} {...props} />;
7455

7556
export default WizardFinal;

packages/ant-component-mapper/src/files/wizard/step-buttons.js

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,35 @@
11
import React, { Fragment } from 'react';
22
import PropTypes from 'prop-types';
33
import { Button } from 'antd';
4+
import selectNext from '@data-driven-forms/common/src/wizard/select-next';
45

5-
import { useFieldApi, useFormApi } from '@data-driven-forms/react-form-renderer';
6-
7-
const SimpleNext = ({ next, handleNext, submit, buttonLabels, disabled }) => {
8-
const { valid } = useFormApi();
9-
6+
const NextButton = ({ nextStep, handleNext, handleSubmit, buttonLabels, getState, valid }) => {
107
return (
11-
<Button type="primary" htmlType="button" onClick={() => (valid ? handleNext(next) : submit())} disabled={disabled}>
12-
{buttonLabels.next}
8+
<Button
9+
type="primary"
10+
htmlType="button"
11+
onClick={() => (nextStep ? handleNext(selectNext(nextStep, getState)) : handleSubmit())}
12+
disabled={!valid}
13+
>
14+
{nextStep ? buttonLabels.next : buttonLabels.submit}
1315
</Button>
1416
);
1517
};
1618

17-
SimpleNext.propTypes = {
18-
next: PropTypes.string,
19+
NextButton.propTypes = {
20+
nextStep: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.object]),
21+
handleSubmit: PropTypes.func.isRequired,
22+
valid: PropTypes.bool,
1923
handleNext: PropTypes.func.isRequired,
20-
submit: PropTypes.func.isRequired,
21-
buttonLabels: PropTypes.object.isRequired,
22-
disabled: PropTypes.bool
23-
};
24-
25-
const ConditionalNext = ({ nextStep, ...rest }) => {
26-
const {
27-
input: { value }
28-
} = useFieldApi({ name: nextStep.when, subscription: { value: true } });
29-
30-
const next = nextStep.stepMapper[value];
31-
32-
return <SimpleNext next={next} {...rest} disabled={!next} />;
33-
};
34-
35-
ConditionalNext.propTypes = {
36-
nextStep: PropTypes.shape({
37-
when: PropTypes.string.isRequired,
38-
stepMapper: PropTypes.object.isRequired
24+
getState: PropTypes.func.isRequired,
25+
buttonLabels: PropTypes.shape({
26+
submit: PropTypes.node.isRequired,
27+
cancel: PropTypes.node.isRequired,
28+
back: PropTypes.node.isRequired,
29+
next: PropTypes.node.isRequired
3930
}).isRequired
4031
};
4132

42-
const submitButton = (handleSubmit, submitText) => (
43-
<Button htmlType="button" type="primary" onClick={handleSubmit}>
44-
{submitText}
45-
</Button>
46-
);
47-
48-
const renderNextButton = ({ nextStep, handleSubmit, buttonLabels, ...rest }) =>
49-
!nextStep ? (
50-
submitButton(handleSubmit, buttonLabels.submit)
51-
) : typeof nextStep === 'object' ? (
52-
<ConditionalNext nextStep={nextStep} buttonLabels={buttonLabels} {...rest} />
53-
) : (
54-
<SimpleNext next={nextStep} buttonLabels={buttonLabels} {...rest} />
55-
);
56-
5733
const WizardStepButtons = ({ disableBack, handlePrev, nextStep, formOptions, handleNext, buttonLabels }) => (
5834
<Fragment>
5935
{formOptions.onCancel && (
@@ -64,12 +40,7 @@ const WizardStepButtons = ({ disableBack, handlePrev, nextStep, formOptions, han
6440
<Button htmlType="button" disabled={disableBack} onClick={handlePrev}>
6541
{buttonLabels.back}
6642
</Button>
67-
{renderNextButton({
68-
...formOptions,
69-
handleNext,
70-
nextStep,
71-
buttonLabels
72-
})}
43+
<NextButton {...formOptions} handleNext={handleNext} nextStep={nextStep} buttonLabels={buttonLabels} />
7344
</Fragment>
7445
);
7546

packages/ant-component-mapper/src/files/wizard/wizard-step.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import React, { Fragment } from 'react';
22
import PropTypes from 'prop-types';
33
import WizardStepButtons from './step-buttons';
44

5+
import './wizard-step.scss';
6+
57
const WizardStep = ({ fields, formOptions, ...rest }) => (
68
<Fragment>
7-
<div className="style">{fields.map((item) => formOptions.renderForm([item], formOptions))}</div>
8-
{/* maybe this button could be in a sperate footer such that the position remains constant */}
9+
<div className="ddorg__ant-component-mapper_wizard-step">{fields.map((item) => formOptions.renderForm([item], formOptions))}</div>
910
<WizardStepButtons formOptions={formOptions} {...rest} />
1011
</Fragment>
1112
);
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
.style {
1+
.ddorg__ant-component-mapper_wizard-step {
2+
margin-top: 32px;
23
min-height: 250px;
34
}

0 commit comments

Comments
 (0)