diff --git a/README.md b/README.md index 1374498..0b9e699 100644 --- a/README.md +++ b/README.md @@ -55,4 +55,19 @@ This event's handler is responsible for validating submitted data and deciding i ### WizardBehavior::EVENT_AFTER_WIZARD -The handler for this event is responsible for taking the required action once the Wizard has completed; this could be rendering a page based on the user input and/or saving the data to persistant storage. \ No newline at end of file +The handler for this event is responsible for taking the required action once the Wizard has completed; this could be rendering a page based on the user input and/or saving the data to persistant storage. + +### WizardMenu +Widget used for rendering steps navigation: +```php +echo \beastbytes\wizard\WizardMenu::widget([ + 'step' => $event->step, + 'wizard' => $event->sender, +]); +``` +By default widget use \yii\widgets\Menu widget for rendering. You may redefine widget class for rendering between widgetConfig options: +```php + 'widgetConfig' => [ + 'class' => \yii\bootstrap\Tabs::class, + ], +``` \ No newline at end of file diff --git a/WizardMenu.php b/WizardMenu.php index 777669a..15a25d8 100644 --- a/WizardMenu.php +++ b/WizardMenu.php @@ -10,13 +10,14 @@ namespace beastbytes\wizard; +use yii\base\Widget; use yii\widgets\Menu; /** * WizardMenu class. * Creates a menu from the wizard steps. */ -class WizardMenu extends Menu +class WizardMenu extends Widget { /** * @var string The CSS class for the current step @@ -27,6 +28,9 @@ class WizardMenu extends Menu * e.g. ['label' => 'Done', 'url' => null] */ public $finalItem; + + public $isShowEmptySteps = true; + /** * @var string The CSS class for future steps */ @@ -44,11 +48,16 @@ class WizardMenu extends Menu */ public $wizard; + public $widgetConfig = []; + + public $items = []; + /** * Initialise the widget */ public function init() { + parent::init(); $route = ['/'.$this->wizard->owner->route]; $params = $this->wizard->owner->actionParams; $steps = $this->wizard->steps; @@ -62,7 +71,7 @@ public function init() $active = true; $class = $this->currentStepCssClass; $url = array_merge($route, $params); - } elseif ($stepIndex < $index) { + } else if ($stepIndex < $index) { $active = false; $class = $this->pastStepCssClass; $url = ($this->wizard->forwardOnly @@ -71,7 +80,11 @@ public function init() } else { $active = false; $class = $this->futureStepCssClass; - $url = null; + if ($this->isShowEmptySteps) { + $url = null; + } else { + $url = array_merge($route, $params); + } } $this->items[] = [ @@ -81,9 +94,35 @@ public function init() 'options' => compact('class') ]; + if (!$this->isShowEmptySteps && !$this->wizard->read($step)) { + break; + } + if (!empty($this->finalItem)) { $this->items[] = $this->finalItem; } } } + + public function run() + { + $widgetOptions = $this->widgetConfig; + $widgetOptions['items'] = $this->items; + if (empty($widgetOptions['class'])) { + $widgetClass = Menu::class; + } else { + $widgetClass = $widgetOptions['class']; + } + + $result = $widgetClass::widget($widgetOptions); + + return $result; + } + + public function __set($name, $value) + { + $this->widgetConfig[$name] = $value; + + return $this; + } }