Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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,
],
```
45 changes: 42 additions & 3 deletions WizardMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*/
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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[] = [
Expand All @@ -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;
}
}