Skip to content

Commit

Permalink
add onStepChange callback
Browse files Browse the repository at this point in the history
  • Loading branch information
loverajoel committed Mar 30, 2016
1 parent 3eb0736 commit 93f9f4d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/Steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export default class Steps extends React.Component {

this._moveStep = this._moveStep.bind(this);
this._printNav = this._printNav.bind(this);

// Call onStepChange for first time
this.props.onStepChange(this.state.currentStep);
}

render() {
Expand Down Expand Up @@ -97,19 +100,22 @@ export default class Steps extends React.Component {
this.setState({
currentStep: step
});
this.props.onStepChange(step);
}
}
}

Steps.propTypes = {
currentStep: React.PropTypes.number,
stepShouldChange: React.PropTypes.func,
onStepChange: React.PropTypes.func,
mountOnlySiblings: React.PropTypes.bool
};

Steps.defaultProps = {
currentStep: 1,
stepShouldChange: () => {return true;},
onStepChange: () => {},
prevButton: 'Prev',
nextButton: 'Next',
mountOnlySiblings: false
Expand Down
32 changes: 32 additions & 0 deletions lib/__tests__/Steps-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,36 @@ describe('Steps',() => {
expect(step5[0].textContent).toEqual('');
});

it('should call onStepChange when render for first time', () => {
let futureStepNumber;
let onStepChangeCallback = (stepNumber) => {
futureStepNumber = stepNumber;
};

let myComponent = testRender(
<Steps onStepChange={onStepChangeCallback}>
<Step>Step 1</Step>
<Step>Step 2</Step>
<Step>Step 3</Step>
</Steps>)
expect(futureStepNumber).toEqual(1);
});

it('should call onStepChange callback after change of step', () => {
let futureStepNumber;
let onStepChangeCallback = (stepNumber) => {
futureStepNumber = stepNumber;
};

let myComponent = testRender(
<Steps onStepChange={onStepChangeCallback}>
<Step>Step 1</Step>
<Step>Step 2</Step>
<Step>Step 3</Step>
</Steps>)
let nextButton = TestUtils.scryRenderedDOMComponentsWithClass(myComponent, 'steps-nav-next');
TestUtils.Simulate.click(nextButton[0]);
expect(futureStepNumber).toEqual(2);
});

});
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ import { Steps, Step } from 'react-multistep-component';
* `stepShouldChange` is called whenever a step is changed. This method can be used for
* validations. By default will return `true`. *optional
*
* `onStepChange` is called after step change (include the first time when render) and have
* the current step how attribute
*
* `prevButton`/`nextButton` is a wrapper for the buttons, `html`, `jsx` or `string` can be included.
* ex: `prevButton={<span><img src="..."/>Step 1</span>}`. *optional
*
Expand Down

0 comments on commit 93f9f4d

Please sign in to comment.