Skip to content

Commit 1eac3d3

Browse files
Barthelewayalexlafroscia
authored andcommitted
Adding an action to go to the previous step (#77)
* Adding a way to go to the previous step * FIX: missing , * FIX: ESLint errors * FIX: ESLint errors * Using a block style approch * Add a test
1 parent 9c6cbc3 commit 1eac3d3

File tree

2 files changed

+92
-6
lines changed

2 files changed

+92
-6
lines changed

addon/components/step-manager.js

+21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const layout = hbs`
1212
)
1313
transition-to=(action 'transition-to-step')
1414
transition-to-next=(action 'transition-to-next-step')
15+
transition-to-previous=(action 'transition-to-previous-step')
1516
currentStep=transitions.currentStep
1617
steps=(if _hasRendered transitions.stepArray)
1718
)}}
@@ -226,6 +227,26 @@ export default Component.extend({
226227
const to = get(this, 'transitions').peek();
227228

228229
this.send('transition-to-step', to, value);
230+
},
231+
232+
/**
233+
* Transition to the "previous" step
234+
*
235+
* When called, this action will go back to the previous step according to
236+
* the step which was visited before entering the currentStep
237+
*
238+
* The first step will not transition to anything.
239+
*
240+
* @method transition-to-previous-step
241+
* @param {*} value the value to pass to the transition actions
242+
* @public
243+
*/
244+
'transition-to-previous-step'(value) {
245+
const to = get(this, '_lastStep');
246+
247+
if (to) {
248+
this.send('transition-to-step', to, value);
249+
}
229250
}
230251
}
231252
});

tests/integration/step-manager-test.js

+71-6
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,47 @@ describe('Integration: StepManagerComponent', function() {
391391
expect($hook('second')).not.to.be.visible;
392392
expect($hook('third')).not.to.be.visible;
393393
});
394+
395+
it('can transition to the previous step', function() {
396+
this.render(hbs`
397+
{{#step-manager as |w|}}
398+
<button id='previous' {{action w.transition-to-previous}}>
399+
Previous!
400+
</button>
401+
<button id='next' {{action w.transition-to-next}}>
402+
Next!
403+
</button>
404+
405+
{{#w.step name='first'}}
406+
<div data-test={{hook 'first'}}></div>
407+
{{/w.step}}
408+
409+
{{#w.step name='second'}}
410+
<div data-test={{hook 'second'}}></div>
411+
{{/w.step}}
412+
413+
{{#w.step name='third'}}
414+
<div data-test={{hook 'third'}}></div>
415+
{{/w.step}}
416+
{{/step-manager}}
417+
`);
418+
419+
expect($hook('first')).to.be.visible;
420+
expect($hook('second')).not.to.be.visible;
421+
expect($hook('third')).not.to.be.visible;
422+
423+
click('#next');
424+
425+
expect($hook('first')).not.to.be.visible;
426+
expect($hook('second')).to.be.visible;
427+
expect($hook('third')).not.to.be.visible;
428+
429+
click('#previous');
430+
431+
expect($hook('first')).to.be.visible;
432+
expect($hook('second')).not.to.be.visible;
433+
expect($hook('third')).not.to.be.visible;
434+
});
394435
});
395436

396437
describe('providing a `did-transition` action', function() {
@@ -662,8 +703,16 @@ describe('Integration: StepManagerComponent', function() {
662703
{{/step-manager}}
663704
`);
664705

665-
expect($hook('step', { index: 0 }).text().trim()).to.equal('Active');
666-
expect($hook('step', { index: 1 }).text().trim()).to.equal('Inactive');
706+
expect(
707+
$hook('step', { index: 0 })
708+
.text()
709+
.trim()
710+
).to.equal('Active');
711+
expect(
712+
$hook('step', { index: 1 })
713+
.text()
714+
.trim()
715+
).to.equal('Inactive');
667716
});
668717
});
669718

@@ -693,13 +742,21 @@ describe('Integration: StepManagerComponent', function() {
693742

694743
expect($hook('step', { name: 'foo' })).to.be.visible;
695744
expect($hook('step', { name: 'bar' })).not.to.be.visible;
696-
expect($hook('steps').text().trim()).to.equal('foo');
745+
expect(
746+
$hook('steps')
747+
.text()
748+
.trim()
749+
).to.equal('foo');
697750

698751
click('button');
699752

700753
expect($hook('step', { name: 'foo' })).not.to.be.visible;
701754
expect($hook('step', { name: 'bar' })).to.be.visible;
702-
expect($hook('steps').text().trim()).to.equal('bar');
755+
expect(
756+
$hook('steps')
757+
.text()
758+
.trim()
759+
).to.equal('bar');
703760
});
704761

705762
it('allows for adding more steps after the initial render', function() {
@@ -735,15 +792,23 @@ describe('Integration: StepManagerComponent', function() {
735792
expect($hook('step', { name: 'foo' })).not.to.be.visible;
736793
expect($hook('step', { name: 'bar' })).not.to.be.visible;
737794
expect($hook('step', { name: 'baz' })).to.be.visible;
738-
expect($hook('steps').text().trim()).to.equal('baz');
795+
expect(
796+
$hook('steps')
797+
.text()
798+
.trim()
799+
).to.equal('baz');
739800

740801
// Check that the new step now points to the first one
741802
click('button');
742803

743804
expect($hook('step', { name: 'foo' })).to.be.visible;
744805
expect($hook('step', { name: 'bar' })).not.to.be.visible;
745806
expect($hook('step', { name: 'baz' })).not.to.be.visible;
746-
expect($hook('steps').text().trim()).to.equal('foo');
807+
expect(
808+
$hook('steps')
809+
.text()
810+
.trim()
811+
).to.equal('foo');
747812
});
748813
});
749814
});

0 commit comments

Comments
 (0)