Skip to content

Commit a57be52

Browse files
committed
Wizard - wip
1 parent b036731 commit a57be52

File tree

4 files changed

+105
-20
lines changed

4 files changed

+105
-20
lines changed

components/src/main/java/org/patternfly/component/wizard/Wizard.java

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.patternfly.component.Closeable;
2727
import org.patternfly.component.ComponentType;
2828
import org.patternfly.component.HasItems;
29-
import org.patternfly.core.Attributes;
3029
import org.patternfly.handler.CloseHandler;
3130
import org.patternfly.handler.ComponentHandler;
3231
import org.patternfly.style.Classes;
@@ -39,13 +38,12 @@
3938
import static org.jboss.elemento.Elements.div;
4039
import static org.jboss.elemento.Elements.failSafeRemoveFromParent;
4140
import static org.jboss.elemento.Elements.insertFirst;
42-
import static org.jboss.elemento.Elements.setVisible;
41+
import static org.patternfly.component.wizard.WizardStepType.review;
4342
import static org.patternfly.core.Aria.expanded;
4443
import static org.patternfly.core.Aria.label;
4544
import static org.patternfly.handler.CloseHandler.fireEvent;
4645
import static org.patternfly.handler.CloseHandler.shouldClose;
4746
import static org.patternfly.style.Classes.component;
48-
import static org.patternfly.style.Classes.main;
4947
import static org.patternfly.style.Classes.outerWrap;
5048
import static org.patternfly.style.Classes.toggle;
5149
import static org.patternfly.style.Classes.wizard;
@@ -77,16 +75,18 @@ public static Wizard wizard() {
7775
private final List<ComponentHandler<Wizard>> backHandler;
7876
private final List<ComponentHandler<Wizard>> nextHandler;
7977
private final List<ComponentHandler<Wizard>> cancelHandler;
78+
private final List<ComponentHandler<Wizard>> finishHandler;
8079
private final HTMLContainerBuilder<HTMLDivElement> innerWrap;
8180
private boolean progressive;
8281
private boolean visitRequired;
82+
private WizardStep currentStep;
8383
private WizardHeader header; // optional
8484
private HTMLContainerBuilder<HTMLButtonElement> toggleButton;
8585
final WizardNav nav;
8686
final WizardFooter footer;
8787

8888
Wizard() {
89-
super(ComponentType.Wizard, div().element());
89+
super(ComponentType.Wizard, div().css(component(wizard)).element());
9090
this.items = new LinkedHashMap<>();
9191
this.closeHandler = new ArrayList<>();
9292
this.onAdd = new ArrayList<>();
@@ -95,6 +95,7 @@ public static Wizard wizard() {
9595
this.backHandler = new ArrayList<>();
9696
this.nextHandler = new ArrayList<>();
9797
this.cancelHandler = new ArrayList<>();
98+
this.finishHandler = new ArrayList<>();
9899

99100
add(toggleButton = button().css(component(wizard, toggle))
100101
.aria(label, "Wizard toggle")
@@ -130,6 +131,10 @@ public Wizard add(WizardHeader header) {
130131

131132
// ------------------------------------------------------ builder
132133

134+
public Wizard height(int height) {
135+
return style("--pf-v6-c-wizard--Height", height + "px");
136+
}
137+
133138
/** Same as {@linkplain #progressive(boolean) progressive(true)} */
134139
public Wizard progressive() {
135140
return progressive(true);
@@ -201,6 +206,11 @@ public Wizard onCancel(ComponentHandler<Wizard> cancelHandler) {
201206
return this;
202207
}
203208

209+
public Wizard onFinish(ComponentHandler<Wizard> finishHandler) {
210+
this.finishHandler.add(finishHandler);
211+
return this;
212+
}
213+
204214
// ------------------------------------------------------ api
205215

206216
@Override
@@ -267,6 +277,10 @@ public WizardFooter footer() {
267277
return footer;
268278
}
269279

280+
public WizardStep currentStep() {
281+
return currentStep;
282+
}
283+
270284
// ------------------------------------------------------ state api
271285

272286
public void back(Event e) {
@@ -301,6 +315,13 @@ public void select(String identifier) {
301315
}
302316
}
303317

318+
public void finish(Event e) {
319+
for (ComponentHandler<Wizard> handler : finishHandler) {
320+
handler.handle(e, this);
321+
}
322+
updateState();
323+
}
324+
304325
// ------------------------------------------------------ internal
305326

306327
private void internalAddStep(WizardStep step) {
@@ -317,35 +338,55 @@ private void internalAddStep(WizardStep step) {
317338
nav.add(navItem);
318339
if (firstStep) {
319340
internalSelectStep(step);
320-
// updateState() is called by internalSelectStep()
321341
} else {
322-
updateState();
342+
step.select(false);
323343
}
324344
}
325345

326346
private void internalSelectStep(WizardStep step) {
327347
nav.select(step.identifier());
328-
for (WizardStep ws : this) {
329-
boolean equals = step.identifier().equals(ws.identifier());
330-
setVisible(ws.element(), equals);
331-
ws.classList().toggle(component(wizard, main), equals);
332-
if (equals) {
333-
ws.element().tabIndex = -1;
334-
} else {
335-
ws.element().removeAttribute(Attributes.tabindex);
336-
}
348+
for (WizardStep currentStep : this) {
349+
currentStep.select(step.identifier().equals(currentStep.identifier()));
337350
}
351+
this.currentStep = step;
338352
updateState();
339353
}
340354

341355
private void internalRemoveStep(WizardStep step, boolean updateState) {
356+
// TODO Handle current step removal
342357
failSafeRemoveFromParent(step);
343358
if (updateState) {
344359
updateState();
345360
}
346361
}
347362

348363
private void updateState() {
364+
// no steps -> no update!
365+
if (currentStep != null && !isEmpty()) {
366+
boolean isFirstStep = items.firstEntry().getValue().identifier().equals(currentStep.identifier());
367+
if (isFirstStep) {
368+
footer.firstStep();
369+
} else {
370+
WizardStep reviewStep = typeStep(review);
371+
boolean isReviewStep = reviewStep != null && reviewStep.identifier().equals(currentStep.identifier());
372+
}
373+
}
374+
}
375+
376+
private WizardStep firstStep() {
377+
if (isEmpty()) {
378+
return null;
379+
}
380+
return items.firstEntry().getValue();
381+
}
382+
383+
private WizardStep typeStep(WizardStepType type) {
384+
for (WizardStep step : this) {
385+
if (step.type == type) {
386+
return step;
387+
}
388+
}
389+
return null;
349390

350391
}
351392
}

components/src/main/java/org/patternfly/component/wizard/WizardFooter.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ public class WizardFooter extends WizardSubComponent<HTMLElement, WizardFooter>
2020
// ------------------------------------------------------ instance
2121

2222
public static final String SUB_COMPONENT_NAME = "wzf";
23-
final Button backButton;
24-
final Button nextButton;
25-
final Button cancelButton;
23+
private final Button backButton;
24+
private final Button nextButton;
2625

2726
WizardFooter() {
2827
super(SUB_COMPONENT_NAME, footer().css(component(wizard, footer)).element());
@@ -42,7 +41,7 @@ public class WizardFooter extends WizardSubComponent<HTMLElement, WizardFooter>
4241
}))))
4342
.addItem(actionListGroup()
4443
.addItem(actionListItem()
45-
.add(cancelButton = button().link().text("Cancel")
44+
.add(button().link().text("Cancel")
4645
.onClick((e, c) -> {
4746
Wizard wizard = lookupComponent();
4847
wizard.cancel(e);
@@ -55,4 +54,21 @@ public class WizardFooter extends WizardSubComponent<HTMLElement, WizardFooter>
5554
public WizardFooter that() {
5655
return this;
5756
}
57+
58+
// ------------------------------------------------------ internal
59+
60+
void firstStep() {
61+
backButton.disabled(true);
62+
nextButton.text("Next");
63+
}
64+
65+
void middleStep() {
66+
backButton.disabled(false);
67+
nextButton.text("Next");
68+
}
69+
70+
void reviewStep() {
71+
backButton.disabled(false);
72+
nextButton.text("Review");
73+
}
5874
}

components/src/main/java/org/patternfly/component/wizard/WizardStep.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import org.jboss.elemento.logger.Logger;
88
import org.patternfly.component.HasIdentifier;
99
import org.patternfly.component.ValidationStatus;
10+
import org.patternfly.core.Attributes;
1011
import org.patternfly.core.ComponentContext;
1112
import elemental2.dom.Element;
1213
import elemental2.dom.HTMLElement;
1314

1415
import static org.jboss.elemento.Elements.div;
16+
import static org.jboss.elemento.Elements.setVisible;
1517
import static org.patternfly.component.ValidationStatus.error;
1618
import static org.patternfly.component.ValidationStatus.success;
1719
import static org.patternfly.component.ValidationStatus.warning;
@@ -131,4 +133,17 @@ public void clearStatus() {
131133
public ValidationStatus status() {
132134
return status;
133135
}
136+
137+
// ------------------------------------------------------ internal
138+
139+
void select(boolean selected) {
140+
setVisible(element(), selected);
141+
classList().toggle(component(wizard, main), selected);
142+
if (selected) {
143+
element().tabIndex = -1;
144+
} else {
145+
element().removeAttribute(Attributes.tabindex);
146+
}
147+
148+
}
134149
}

showcase/src/main/java/org/patternfly/showcase/component/WizardComponent.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
import org.jboss.elemento.router.Route;
1919
import org.patternfly.component.wizard.Wizard;
2020
import org.patternfly.component.wizard.WizardStep;
21+
import org.patternfly.showcase.LoremIpsum;
2122
import org.patternfly.showcase.Snippet;
2223
import org.patternfly.showcase.SnippetPage;
2324

2425
import static org.jboss.elemento.Elements.div;
26+
import static org.jboss.elemento.Elements.p;
2527
import static org.patternfly.component.wizard.Wizard.wizard;
28+
import static org.patternfly.component.wizard.WizardStep.wizardStep;
29+
import static org.patternfly.component.wizard.WizardStepType.review;
2630
import static org.patternfly.showcase.ApiDoc.Type.component;
2731
import static org.patternfly.showcase.ApiDoc.Type.subcomponent;
2832
import static org.patternfly.showcase.Code.code;
@@ -39,7 +43,16 @@ public WizardComponent() {
3943
code("wizard-basic"), () ->
4044
// @code-start:wizard-basic
4145
div()
42-
.add(wizard())
46+
.add(wizard().height(400)
47+
.addItem(wizardStep("wizard-basic-step-0", "Step 1")
48+
.add(p().text(LoremIpsum.words(60)))
49+
.add(p().text(LoremIpsum.words(40)))
50+
.add(p().text(LoremIpsum.words(30)))
51+
.add(p().text(LoremIpsum.words(50))))
52+
.addItem(wizardStep("wizard-basic-step-1", "Step 2")
53+
.add(p().text("Step 2 content")))
54+
.addItem(wizardStep("wizard-basic-step-2", "Review", review)
55+
.add(p().text("Review content"))))
4356
.element()
4457
// @code-end:wizard-basic
4558
));

0 commit comments

Comments
 (0)