Skip to content
This repository was archived by the owner on Aug 7, 2020. It is now read-only.

Commit 1441a71

Browse files
marie-jAxelPeter
authored andcommitted
feat(oui-stepper): add ability to set opened step (#393)
1 parent 882530c commit 1441a71

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

packages/oui-stepper/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@
9898
</oui-stepper>
9999
```
100100

101+
### Open Steps
102+
103+
```html:preview
104+
<oui-stepper
105+
current-index="$ctrl.currentStep">
106+
<oui-step-form
107+
header="Step1">
108+
<p>Random content</p>
109+
</oui-step-form>
110+
<oui-step-form
111+
header="Step2">
112+
<oui-button variant="link" on-click="$ctrl.currentStep = 0">
113+
<span>Go to Step 1</span>
114+
</oui-button>
115+
</oui-step-form>
116+
</oui-stepper>
117+
```
118+
101119
### Events on `oui-stepper`
102120

103121
**Note**: If you want to access the forms inside `on-finish` callback, you need to use the `forms` variable as below.
@@ -154,6 +172,7 @@
154172
| ---- | ---- | ---- | ---- | ---- | ---- | ----
155173
| `name` | string | @? | yes | n/a | n/a | stepper name used to identify step
156174
| `id` | string | @? | yes | n/a | n/a | stepper id used to identify step
175+
| `current-index` | number | =? | no | n/a | 0 | current step index
157176
| `on-init` | function | & | no | n/a | n/a | initialization function
158177
| `on-finish` | function | & | no | n/a | n/a | submit all steps function
159178

packages/oui-stepper/src/index.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,33 @@ describe("ouiStepper", () => {
230230
expect(form2.hasClass(disabledClass)).toBe(false);
231231
expect(form3.hasClass(disabledClass)).toBe(true);
232232
});
233+
234+
it("should open designated step", () => {
235+
const element = TestUtils.compileTemplate(`
236+
<oui-stepper current-index="$ctrl.index">
237+
<oui-step-form name="form1"></oui-step-form>
238+
<oui-step-form name="form2"></oui-step-form>
239+
<oui-step-form name="form3"></oui-step-form>
240+
</oui-stepper>`, { index: 1 });
241+
242+
$timeout.flush();
243+
244+
const form1 = element.find("form").eq(0);
245+
const form2 = element.find("form").eq(1);
246+
const form3 = element.find("form").eq(2);
247+
248+
expect(form1.scope().$ctrl.stepper.focused).toBe(false);
249+
expect(form2.scope().$ctrl.stepper.focused).toBe(true);
250+
expect(form3.scope().$ctrl.stepper.focused).toBe(false);
251+
252+
element.scope().$ctrl.index = 2;
253+
254+
element.scope().$digest();
255+
256+
expect(form1.scope().$ctrl.stepper.focused).toBe(false);
257+
expect(form2.scope().$ctrl.stepper.focused).toBe(false);
258+
expect(form3.scope().$ctrl.stepper.focused).toBe(true);
259+
});
233260
});
234261
});
235262
});

packages/oui-stepper/src/stepper.component.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export default {
66
name: "@",
77
id: "@",
88
onInit: "&",
9-
onFinish: "&"
9+
onFinish: "&",
10+
currentIndex: "=?"
1011
},
1112
controller,
1213
template,

packages/oui-stepper/src/stepper.controller.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
import get from "lodash/get";
2+
13
export default class {
2-
constructor ($attrs, $element, $timeout) {
4+
constructor ($attrs, $element, $scope, $timeout) {
35
"ngInject";
46

57
this.$attrs = $attrs;
68
this.$element = $element;
9+
this.$scope = $scope;
710
this.$timeout = $timeout;
811
}
912

1013
$onInit () {
1114
this.forms = [];
1215
this.steps = [];
13-
this.currentIndex = 0;
16+
this.currentIndex = get(this, "currentIndex", 0);
1417
this.onInit();
1518
}
1619

@@ -21,6 +24,11 @@ export default class {
2124
.removeAttr("name")
2225
.addClass("oui-stepper")
2326
);
27+
28+
this.$scope.$watch(
29+
() => this.currentIndex,
30+
(index) => this.focusStep(index)
31+
);
2432
}
2533

2634
addStep (step) {

0 commit comments

Comments
 (0)