Skip to content

Commit a05b52e

Browse files
committed
fix(steps): fire enter event for initial step
1 parent ad5b7d0 commit a05b52e

File tree

4 files changed

+40
-46
lines changed

4 files changed

+40
-46
lines changed

packages/steps/src/LionStep.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,29 +100,11 @@ export class LionStep extends LionLitElement {
100100
firstUpdated() {
101101
super.firstUpdated();
102102
this.controller = this.parentNode;
103-
if (this.initialStep === true) {
104-
this.enter(true);
105-
}
106103
}
107104

108-
getControllerIndex() {
109-
const { steps } = this.controller;
110-
for (let i = 0; i < steps.length; i += 1) {
111-
if (steps[i] === this) {
112-
return i;
113-
}
114-
}
115-
return 0;
116-
}
117-
118-
enter(updateController) {
105+
enter() {
119106
this.status = 'entered';
120-
if (updateController === true) {
121-
this.controller.current = this.getControllerIndex();
122-
// controller will trigger enter() again which will dispatch the enter event
123-
} else {
124-
this.dispatchEvent(new CustomEvent('enter', { bubbles: true, composed: true }));
125-
}
107+
this.dispatchEvent(new CustomEvent('enter', { bubbles: true, composed: true }));
126108
}
127109

128110
leave() {

packages/steps/src/LionSteps.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ export class LionSteps extends ObserverMixin(LionLitElement) {
6464
firstUpdated() {
6565
super.firstUpdated();
6666
this._max = this.steps.length - 1;
67+
68+
let hasInitial = false;
69+
this.steps.forEach((step, i) => {
70+
if (step.initialStep && i !== 0) {
71+
this.current = i;
72+
hasInitial = true;
73+
}
74+
});
75+
if (!hasInitial) {
76+
this.steps[0].enter();
77+
}
6778
}
6879

6980
next() {

packages/steps/test/lion-step.test.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, fixture, fixtureSync, html, oneEvent } from '@open-wc/testing';
1+
import { expect, fixture, html, oneEvent } from '@open-wc/testing';
22

33
import '../lion-step.js';
44

@@ -35,30 +35,6 @@ describe('lion-step', () => {
3535
expect(el.children[0].passesCondition()).to.equal(false);
3636
});
3737

38-
it('allows to define it as the initial-step', async () => {
39-
const el = fixtureSync(html`
40-
<fake-lion-steps>
41-
<lion-step initial-step>Step 1</lion-step>
42-
</fake-lion-steps>
43-
`);
44-
el.steps = [el.children[0]];
45-
await el.updateComplete;
46-
expect(el.current).to.equal(0);
47-
expect(el.children[0].status).to.equal('entered');
48-
49-
const el2 = fixtureSync(html`
50-
<fake-lion-steps>
51-
<lion-step>Step 1</lion-step>
52-
<lion-step initial-step>Step 2</lion-step>
53-
</fake-lion-steps>
54-
`);
55-
el2.steps = [el2.children[0], el2.children[1]];
56-
await el2.updateComplete;
57-
expect(el2.current).to.equal(1);
58-
expect(el2.children[0].status).to.equal('untouched');
59-
expect(el2.children[1].status).to.equal('entered');
60-
});
61-
6238
it('has "untouched" status by default', async () => {
6339
const el = await fixture(html`
6440
<fake-lion-steps>

packages/steps/test/lion-steps.test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sinon from 'sinon';
2-
import { expect, fixture, html, oneEvent } from '@open-wc/testing';
2+
import { expect, fixture, html, oneEvent, nextFrame } from '@open-wc/testing';
33

44
import '../lion-steps.js';
55
import '../lion-step.js';
@@ -130,6 +130,7 @@ describe('lion-steps', () => {
130130
<lion-step>Step 4</lion-step>
131131
</lion-steps>
132132
`);
133+
await nextFrame();
133134

134135
el.current = 2;
135136
await checkWorkflow(el, {
@@ -191,6 +192,30 @@ describe('lion-steps', () => {
191192
expect(currentInEnterEvent).to.equal(1);
192193
});
193194

195+
it('will fire initial @enter event on first step with or with [initial-step] attribute', async () => {
196+
const firstEnterSpy = sinon.spy();
197+
await fixture(html`
198+
<lion-steps>
199+
<lion-step @enter=${firstEnterSpy}>
200+
<div>test1</div>
201+
</lion-step>
202+
</lion-steps>
203+
`);
204+
await nextFrame();
205+
expect(firstEnterSpy).to.have.been.calledOnce;
206+
207+
const firstEnterSpyWithInitial = sinon.spy();
208+
await fixture(html`
209+
<lion-steps>
210+
<lion-step initial-step @enter=${firstEnterSpyWithInitial}>
211+
<div>test1</div>
212+
</lion-step>
213+
</lion-steps>
214+
`);
215+
await nextFrame();
216+
expect(firstEnterSpyWithInitial).to.have.been.calledOnce;
217+
});
218+
194219
it('will fire initial @enter event only once if [initial-step] is not on first step', async () => {
195220
const firstEnterSpy = sinon.spy();
196221
const secondEnterSpy = sinon.spy();

0 commit comments

Comments
 (0)