Skip to content

Commit 37d0db5

Browse files
Max LarionovMikhail Bashkirov
authored andcommitted
fix(steps): allow extensions to introduce new non-default slots
1 parent 72516ac commit 37d0db5

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

packages/steps/src/LionStep.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,18 @@ export class LionStep extends LionLitElement {
9797
`;
9898
}
9999

100-
connectedCallback() {
101-
// eslint-disable-next-line wc/guard-super-call
102-
super.connectedCallback();
100+
firstUpdated() {
101+
super.firstUpdated();
103102
this.controller = this.parentNode;
104103
if (this.initialStep === true) {
105104
this.enter(true);
106105
}
107106
}
108107

109108
getControllerIndex() {
110-
const controllerChildren = this.controller.children;
111-
for (let i = 0; i < controllerChildren.length; i += 1) {
112-
if (controllerChildren[i] === this) {
109+
const { steps } = this.controller;
110+
for (let i = 0; i < steps.length; i += 1) {
111+
if (steps[i] === this) {
113112
return i;
114113
}
115114
}

packages/steps/src/LionSteps.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class LionSteps extends ObserverMixin(LionLitElement) {
6363

6464
firstUpdated() {
6565
super.firstUpdated();
66-
this._max = this.children.length - 1;
66+
this._max = this.steps.length - 1;
6767
}
6868

6969
next() {
@@ -74,12 +74,17 @@ export class LionSteps extends ObserverMixin(LionLitElement) {
7474
this._goTo(this.current - 1, this.current);
7575
}
7676

77+
get steps() {
78+
const defaultSlot = this.shadowRoot.querySelector('slot:not([name])');
79+
return defaultSlot.assignedNodes().filter(node => node.nodeType === Node.ELEMENT_NODE);
80+
}
81+
7782
_goTo(newCurrent, oldCurrent) {
7883
if (newCurrent < 0 || newCurrent > this._max) {
7984
throw new Error(`There is no step at index ${newCurrent}.`);
8085
}
8186

82-
const nextStep = this.children[newCurrent];
87+
const nextStep = this.steps[newCurrent];
8388
const back = newCurrent < oldCurrent;
8489

8590
if (nextStep.passesCondition(this.data)) {
@@ -99,8 +104,8 @@ export class LionSteps extends ObserverMixin(LionLitElement) {
99104
}
100105

101106
_changeStep(newCurrent, oldCurrent) {
102-
const oldStepElement = this.children[oldCurrent];
103-
const newStepElement = this.children[newCurrent];
107+
const oldStepElement = this.steps[oldCurrent];
108+
const newStepElement = this.steps[newCurrent];
104109
const fromStep = { number: oldCurrent, element: oldStepElement };
105110
const toStep = { number: newCurrent, element: newStepElement };
106111

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

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

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

@@ -36,20 +36,24 @@ describe('lion-step', () => {
3636
});
3737

3838
it('allows to define it as the initial-step', async () => {
39-
const el = await fixture(html`
39+
const el = fixtureSync(html`
4040
<fake-lion-steps>
4141
<lion-step initial-step>Step 1</lion-step>
4242
</fake-lion-steps>
4343
`);
44+
el.steps = [el.children[0]];
45+
await el.updateComplete;
4446
expect(el.current).to.equal(0);
4547
expect(el.children[0].status).to.equal('entered');
4648

47-
const el2 = await fixture(html`
49+
const el2 = fixtureSync(html`
4850
<fake-lion-steps>
4951
<lion-step>Step 1</lion-step>
5052
<lion-step initial-step>Step 2</lion-step>
5153
</fake-lion-steps>
5254
`);
55+
el2.steps = [el2.children[0], el2.children[1]];
56+
await el2.updateComplete;
5357
expect(el2.current).to.equal(1);
5458
expect(el2.children[0].status).to.equal('untouched');
5559
expect(el2.children[1].status).to.equal('entered');

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ async function checkWorkflow(steps, expected) {
2727
}
2828

2929
describe('lion-steps', () => {
30+
it('has "steps" getter that returns default slot elements', async () => {
31+
const el = await fixture(html`
32+
<lion-steps>
33+
<lion-step>Step 1</lion-step>
34+
<lion-step initial-step>Step 2</lion-step>
35+
<other-step-element>Step 3</other-step-element>
36+
<steps-extension-element slot="new-slot">e.g. steps indicator</steps-extension-element>
37+
</lion-steps>
38+
`);
39+
expect(el.steps.length).to.equal(3);
40+
expect(el.steps[0].tagName).to.equal('LION-STEP');
41+
expect(el.steps[1].tagName).to.equal('LION-STEP');
42+
expect(el.steps[2].tagName).to.equal('OTHER-STEP-ELEMENT');
43+
});
44+
3045
describe('initialization', () => {
3146
it('activates step with an "initial-step" attribute', async () => {
3247
const el = await fixture(html`

0 commit comments

Comments
 (0)