Skip to content

Commit

Permalink
[upd] Remove unused functions from Wizard and WizardStep
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasklim committed Aug 29, 2020
1 parent 828a0db commit 8964c2d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 132 deletions.
71 changes: 14 additions & 57 deletions src/components/wizard/Wizard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,68 +14,29 @@ const Wizard = (props) => {
const wizardContext = React.useContext(WizardContext);
const configurationContext = React.useContext(ConfigurationContext);

const onAdvance = () => {
if (currentStep !== wizardContext.getStepData().length - 1) {
wizardContext.getStepData()[currentStep + 1].visited = true;
const onNextStep = () => {
const stepData = wizardContext.getStepData();
if (currentStep !== stepData.length - 1) {
stepData[currentStep + 1].visited = true;
setCurrentStep((prevCurrentStep) => prevCurrentStep + 1);
}
};

const onRetreat = () => {
const onPreviousStep = () => {
if (currentStep === 0) {
return;
}
setCurrentStep((prevCurrentStep) => prevCurrentStep - 1);
};

const onFinish = (errCallback) => {
const data = {
data: this.context.getData(),
stepData: this.context.getStepData()
};
this.context.reset();
this.props.onFinish(data, this.props.onClose, errCallback);
};

/**
* Insert the specified step after the current one.
* @param step The step to insert
*/
const onInsertStepAfterCurrent = (step) => {
configurationContext.getStepData().splice(currentStep + 1, 0, step);
configurationContext.insertStep(currentStep + 1, step);
};

/**
* Adds the specified step to the end of this wizard.
* @param step The step to add
*/
const onAddStep = (step) => {
wizardContext.getStepData().push(step);
wizardContext.insertStep(wizardContext.getStepData().length - 1, step);
};

const onRemoveStep = (stepId) => {
const navigate = (stepIndex) => {
const stepData = wizardContext.getStepData();

for (let i = 0; i < stepData.length; i++) {
if (stepData[i].id === stepId) {
wizardContext.getStepData().splice(i, 1);
wizardContext.removeStep(i);
if (i === currentStep && i !== 0) {
setCurrentStep((prevCurrentStep) => prevCurrentStep - 1);
}
break;
}
}
};

const navigate = (stepIndex) => {
if (stepIndex === currentStep || stepIndex >= wizardContext.getStepData().length) {
if (stepIndex === currentStep || stepIndex >= stepData.length) {
return;
}
// Can we jump forward?
if (stepIndex > currentStep && !wizardContext.getStepData()[stepIndex].visited && !props.enableForwardSkip) {
if (stepIndex > currentStep && !stepData[stepIndex].visited && !props.enableForwardSkip) {
return;
}
setCurrentStep(stepIndex);
Expand All @@ -86,10 +47,12 @@ const Wizard = (props) => {
return null;
}

const stepData = wizardContext.getStepData();

return configurationContext.options.horizontalWizardNav ? (
<HorizontalWizardNav currentStep={currentStep} steps={wizardContext.getStepData()} onNavigate={navigate} />
<HorizontalWizardNav currentStep={currentStep} steps={stepData} onNavigate={navigate} />
) : (
<VerticalWizardNav currentStep={currentStep} steps={wizardContext.getStepData()} onNavigate={navigate} />
<VerticalWizardNav currentStep={currentStep} steps={stepData} onNavigate={navigate} />
);
};

Expand Down Expand Up @@ -120,12 +83,8 @@ const Wizard = (props) => {
<WizardStep
key={'step' + currentStep}
step={step}
onFinish={onFinish}
onAdvance={onAdvance}
onRetreat={onRetreat}
onInsertStepAfterCurrent={onInsertStepAfterCurrent}
onAddStep={onAddStep}
onRemoveStep={onRemoveStep}
onNext={onNextStep}
onPrevious={onPreviousStep}
stepIndex={currentStep}
isFirstStep={currentStep === 0}
isLastStep={currentStep === wizardContext.getStepData().length - 1}
Expand All @@ -142,8 +101,6 @@ const Wizard = (props) => {

Wizard.propTypes = {
start: PropTypes.number,
onFinish: PropTypes.func,
onClose: PropTypes.func,
enableForwardSkip: PropTypes.bool // Whether to allow forward step skipping
};

Expand Down
62 changes: 9 additions & 53 deletions src/components/wizard/WizardStep.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,14 @@ const WizardStep = (props) => {
const wizardContext = React.useContext(WizardContext);
const { options } = React.useContext(ConfigurationContext);

const [advanceDisabled, setAdvanceDisabled] = useState(
props.step.defaultNextDisabled != null ? props.step.defaultNextDisabled : false
);
const [retreatDisabled, setRetreatDisabled] = useState(false);
const [currentError, setCurrentError] = useState(null);

const onAdvance = (err) => {
if (err) {
setAdvanceDisabled(true);
setRetreatDisabled(true);
setCurrentError(err);
} else {
wizardContext.updateStepData(props.stepIndex, wizardContext.getStepData());
props.onAdvance();
}
};

const onNext = () => {
setAdvanceDisabled(true);
setRetreatDisabled(true);

if (props.step.onNext) {
props.step.onNext.apply(this, [onAdvance]);
} else {
wizardContext.updateStepData(props.stepIndex, wizardContext.getStepData());
props.onAdvance();
}
};

const onPrevious = () => {
if (props.step.onPrevious) {
props.step.onPrevious.apply(this, [props.onRetreat]);
} else {
props.onRetreat();
}
};

const onFinish = () => {
const onNextStep = () => {
wizardContext.updateStepData(props.stepIndex, wizardContext.getStepData());
props.onFinish();
props.onNextStep();
};

const enableNext = () => setAdvanceDisabled(false);

const disableNext = () => setAdvanceDisabled(true);
const onPreviousStep = () => {
props.onPreviousStep();
};

const _renderHelpIcon = () => {
const question = wizardContext.getStepData([props.stepIndex]);
Expand All @@ -73,12 +36,12 @@ const WizardStep = (props) => {
return (
<ButtonToolbar className="m-3 float-right">
{!props.isFirstStep && (
<Button className="mr-2" onClick={onPrevious} disabled={retreatDisabled} variant="primary" size="sm">
<Button className="mr-2" onClick={onPreviousStep} variant="primary" size="sm">
{options.i18n['wizard.previous']}
</Button>
)}
{!props.isLastStep && (
<Button onClick={onNext} disabled={advanceDisabled} variant="primary" size="sm">
<Button onClick={onNextStep} variant="primary" size="sm">
{options.i18n['wizard.next']}
</Button>
)}
Expand All @@ -99,21 +62,14 @@ const WizardStep = (props) => {
</Card>

{options.wizardStepButtons && _renderWizardStepButtons()}

{currentError && (
<Alert variant="danger">
<p>{currentError.message}</p>
</Alert>
)}
</div>
);
};

WizardStep.propTypes = {
step: PropTypes.object.isRequired,
onFinish: PropTypes.func.isRequired,
onAdvance: PropTypes.func,
onRetreat: PropTypes.func,
onNextStep: PropTypes.func,
onPreviousStep: PropTypes.func,
stepIndex: PropTypes.number.isRequired,
isFirstStep: PropTypes.bool,
isLastStep: PropTypes.bool
Expand Down
22 changes: 0 additions & 22 deletions src/contexts/WizardContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,6 @@ const WizardContextProvider = (props) => {
}
};

const insertStep = (index, update) => {
const newStepData = [...stepData];

newStepData.splice(index, 0, update || {});
setStepData(newStepData);
};

const removeStep = (index) => {
const newStepData = [...stepData];

newStepData.splice(index, 1);
setStepData(newStepData);
};

const reset = () => {
setData(INITIAL_DATA);
setStepData(INITIAL_STEP_DATA);
};

const getData = () => {
return data;
};
Expand All @@ -69,9 +50,6 @@ const WizardContextProvider = (props) => {
() => ({
updateData,
updateStepData,
insertStep,
removeStep,
reset,
getData,
getStepData
}),
Expand Down

0 comments on commit 8964c2d

Please sign in to comment.