Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/components/Steps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default class Steps extends Component {
})
).isRequired,
onStart: PropTypes.func,
onBeforeExit: PropTypes.func,
onExit: PropTypes.func.isRequired,
onBeforeChange: PropTypes.func,
onAfterChange: PropTypes.func,
Expand All @@ -42,6 +43,7 @@ export default class Steps extends Component {
static defaultProps = {
enabled: false,
onStart: null,
onBeforeExit: null,
onBeforeChange: null,
onAfterChange: null,
onChange: null,
Expand Down Expand Up @@ -102,6 +104,19 @@ export default class Steps extends Component {
this.introJs.exit();
}

/**
* Trigger before exiting the steps
* @return {Boolean} Returning `false` will prevent the exit.
*/
onBeforeExit = () => {
const { onBeforeExit } = this.props;

if (onBeforeExit) {
return onBeforeExit(this.introJs._currentStep);
}
return true;
}

/**
* Triggered when Intro.js steps are exited.
*/
Expand Down Expand Up @@ -202,6 +217,7 @@ export default class Steps extends Component {
installIntroJs() {
this.introJs = introJs();

this.introJs.onbeforeexit(this.onBeforeExit);
this.introJs.onexit(this.onExit);
this.introJs.onbeforechange(this.onBeforeChange);
this.introJs.onafterchange(this.onAfterChange);
Expand Down
31 changes: 31 additions & 0 deletions src/components/Steps/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,37 @@ describe('Steps', () => {
expect(onChange).toHaveBeenCalledWith(1, null);
});

test('should not call the onBeforeExit callback when disabled', () => {
const onBeforeExit = jest.fn();

renderer.create(<Steps initialStep={0} steps={steps} onExit={() => {}} onBeforeExit={onBeforeExit} />);

expect(onBeforeExit).not.toHaveBeenCalled();
});

test('should call the onBeforeExit callback when enabled', () => {
const onBeforeExit = jest.fn();

const wrapper = shallow(<Steps initialStep={0} steps={steps} onExit={() => {}} onBeforeExit={onBeforeExit} />, {
lifecycleExperimental: true,
});
wrapper.setProps({ enabled: true });

expect(onBeforeExit).toHaveBeenCalledTimes(1);
});

test('should call the onBeforeExit callback with the step number', () => {
const onBeforeExit = jest.fn();

const wrapper = shallow(<Steps initialStep={0} steps={steps} onExit={() => {}} onBeforeExit={onBeforeExit} />, {
lifecycleExperimental: true,
});
wrapper.setProps({ enabled: true });

expect(onBeforeExit).toHaveBeenCalledWith(1);
});


test('should not call the onBeforeChange callback when disabled', () => {
const onBeforeChange = jest.fn();

Expand Down