Skip to content

Commit e451d76

Browse files
authored
Merge pull request #15 from VictorioBerra/master
Allow preventing backward navigation on failed validation.
2 parents 9842560 + 38d3fd4 commit e451d76

File tree

4 files changed

+59
-30
lines changed

4 files changed

+59
-30
lines changed

dist/jquery-steps.js

Lines changed: 33 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/jquery-steps.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/jquery-steps.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Steps.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,28 @@ class Steps {
1313
this.init();
1414
}
1515

16+
stepClick(e) {
17+
e.preventDefault();
18+
const nextStep = $(this).closest('li').index();
19+
const stepIndex = e.data.self.getStepIndex();
20+
e.data.self.setActiveStep(stepIndex, nextStep);
21+
}
22+
23+
btnClick(e) {
24+
e.preventDefault();
25+
const statusAction = $(this).data('direction');
26+
e.data.self.setAction(statusAction);
27+
}
28+
1629
init() {
1730
this.hook('onInit');
1831
const self = this;
1932

2033
// step click event
21-
$(this.el).find(this.options.stepSelector).on('click', function (e) {
22-
e.preventDefault();
23-
const nextStep = $(this).closest('li').index();
24-
const stepIndex = self.getStepIndex();
25-
self.setActiveStep(stepIndex, nextStep);
26-
});
34+
$(this.el).find(this.options.stepSelector).on('click', { self }, this.stepClick);
2735

2836
// button click event
29-
$(this.el).find(`${this.options.footerSelector} ${this.options.buttonSelector}`).on('click', function (e) {
30-
e.preventDefault();
31-
const statusAction = $(this).data('direction');
32-
self.setAction(statusAction);
33-
});
37+
$(this.el).find(`${this.options.footerSelector} ${this.options.buttonSelector}`).on('click', { self }, this.btnClick);
3438

3539
// set default step
3640
this.setShowStep(this.options.startAt, '', this.options.activeClass);
@@ -50,7 +54,8 @@ class Steps {
5054
}
5155

5256
destroy() {
53-
this.el.empty();
57+
$(this.el).find(this.options.stepSelector).off('click', this.stepClick);
58+
$(this.el).find(`${this.options.footerSelector} ${this.options.buttonSelector}`).off('click', this.btnClick);
5459
this.el.removeData('plugin_Steps');
5560
this.hook('onDestroy');
5661
}
@@ -107,11 +112,16 @@ class Steps {
107112
if (currentIndex > newIndex) {
108113
for (let i = currentIndex; i >= newIndex; i -= 1) {
109114
const stepDirectionB = this.getStepDirection(i, newIndex);
110-
this.options.onChange(i, newIndex, stepDirectionB);
115+
const validStep = this.options.onChange(i, newIndex, stepDirectionB);
111116
this.setShowStep(i, `${this.options.doneClass} ${this.options.activeClass} ${this.options.errorClass}`);
112117
if (i === newIndex) {
113118
this.setShowStep(i, `${this.options.doneClass} ${this.options.errorClass}`, this.options.activeClass);
114119
}
120+
if (!validStep) {
121+
this.setShowStep(i, this.options.doneClass, `${this.options.activeClass} ${this.options.errorClass}`);
122+
this.setFooterBtns();
123+
break;
124+
}
115125
}
116126
}
117127

0 commit comments

Comments
 (0)