2626import org .patternfly .component .Closeable ;
2727import org .patternfly .component .ComponentType ;
2828import org .patternfly .component .HasItems ;
29- import org .patternfly .core .Attributes ;
3029import org .patternfly .handler .CloseHandler ;
3130import org .patternfly .handler .ComponentHandler ;
3231import org .patternfly .style .Classes ;
3938import static org .jboss .elemento .Elements .div ;
4039import static org .jboss .elemento .Elements .failSafeRemoveFromParent ;
4140import static org .jboss .elemento .Elements .insertFirst ;
42- import static org .jboss . elemento . Elements . setVisible ;
41+ import static org .patternfly . component . wizard . WizardStepType . review ;
4342import static org .patternfly .core .Aria .expanded ;
4443import static org .patternfly .core .Aria .label ;
4544import static org .patternfly .handler .CloseHandler .fireEvent ;
4645import static org .patternfly .handler .CloseHandler .shouldClose ;
4746import static org .patternfly .style .Classes .component ;
48- import static org .patternfly .style .Classes .main ;
4947import static org .patternfly .style .Classes .outerWrap ;
5048import static org .patternfly .style .Classes .toggle ;
5149import 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}
0 commit comments