From bd864ce279b263f081f420013a79591635bae79a Mon Sep 17 00:00:00 2001 From: meriadec Date: Tue, 26 Jun 2018 14:04:10 +0200 Subject: [PATCH] Add `onStepChange`, `disabledSteps` and other stuff on Stepper --- src/components/base/Stepper/index.js | 48 ++++++++++++++++++++++---- src/components/base/Stepper/stories.js | 2 +- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/components/base/Stepper/index.js b/src/components/base/Stepper/index.js index 038aef23..8daf4acb 100644 --- a/src/components/base/Stepper/index.js +++ b/src/components/base/Stepper/index.js @@ -12,9 +12,12 @@ import Breadcrumb from 'components/Breadcrumb' type Props = { t: T, title: string, + steps: Step[], initialStepId: string, onClose: void => void, - steps: Step[], + onStepChange?: Step => void, + disabledSteps?: number[], + errorSteps?: number[], children: any, } @@ -23,7 +26,8 @@ export type Step = { label: string, component: StepProps => React$Node, footer: StepProps => React$Node, - preventClose?: boolean, + shouldRenderFooter?: StepProps => boolean, + shouldPreventClose?: boolean | (StepProps => boolean), onBack?: StepProps => void, } @@ -41,10 +45,20 @@ class Stepper extends PureComponent { stepId: this.props.initialStepId, } - transitionTo = stepId => this.setState({ stepId }) + transitionTo = stepId => { + const { onStepChange, steps } = this.props + this.setState({ stepId }) + if (onStepChange) { + const stepIndex = steps.findIndex(s => s.id === stepId) + const step = steps[stepIndex] + if (step) { + onStepChange(step) + } + } + } render() { - const { t, steps, title, onClose, children, ...props } = this.props + const { t, steps, title, onClose, disabledSteps, errorSteps, children, ...props } = this.props const { stepId } = this.state const stepIndex = steps.findIndex(s => s.id === stepId) @@ -52,7 +66,13 @@ class Stepper extends PureComponent { invariant(step, `Stepper: step ${stepId} doesn't exists`) - const { component: StepComponent, footer: StepFooter, onBack, preventClose } = step + const { + component: StepComponent, + footer: StepFooter, + onBack, + shouldPreventClose, + shouldRenderFooter, + } = step const stepProps: StepProps = { t, @@ -60,15 +80,29 @@ class Stepper extends PureComponent { ...props, } + const renderFooter = + !!StepFooter && (shouldRenderFooter === undefined || shouldRenderFooter(stepProps)) + + const preventClose = + typeof shouldPreventClose === 'function' + ? shouldPreventClose(stepProps) + : !!shouldPreventClose + return ( onBack(stepProps) : undefined}>{title} - + {children} - {StepFooter && ( + {renderFooter && ( diff --git a/src/components/base/Stepper/stories.js b/src/components/base/Stepper/stories.js index 4cb41487..b03a0be9 100644 --- a/src/components/base/Stepper/stories.js +++ b/src/components/base/Stepper/stories.js @@ -27,7 +27,7 @@ const steps: Step[] = [ { id: 'second', label: 'second step', - preventClose: true, + shouldPreventClose: true, onBack: ({ transitionTo }: StepProps) => transitionTo('first'), component: () =>
second step (you cant close on this one)
, footer: ({ transitionTo }: StepProps) => (