Giter Site home page Giter Site logo

vildanbina / livewire-wizard Goto Github PK

View Code? Open in Web Editor NEW
314.0 314.0 29.0 36 KB

Livewire component that provides you with a wizard that supports multiple steps form while maintaining state.

License: MIT License

PHP 84.07% Blade 15.93%
forms laravel livewire multistep wizard

livewire-wizard's Introduction

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

A dynamic Laravel Livewire component for multi steps form.

Multi steps form

Installation

You can install the package via composer:

composer require vildanbina/livewire-wizard

For UI design this package require WireUI package for details.

Alpine

Livewire Wizard requires Alpine. You can use the official CDN to quickly include Alpine:

<!-- Alpine v2 -->
<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js" defer></script>

<!-- Alpine v3 -->
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>

TailwindCSS

The base modal is made with TailwindCSS. If you use a different CSS framework I recommend that you publish the modal template and change the markup to include the required classes for your CSS framework.

php artisan vendor:publish --tag=livewire-wizard-views

Usage

Creating a wizard form

You can create livewire component php artisan make:livewire UserWizard to make the initial Livewire component. Open your component class and make sure it extends the WizardComponent class:

<?php

namespace App\Livewire;

use Vildanbina\LivewireWizard\WizardComponent;
use App\Models\User;

class UserWizard extends WizardComponent
{
    // My custom class property
    public $userId;
    
    /*
     * Will return App\Models\User instance or will create empty User (based on $userId parameter) 
     */
    public function model()
    {
        return User::findOrNew($this->userId);
    }
}

When you need to display wizard form, based on above example we need to pass $userId value and to display wizard form:

<livewire:user-wizard user-id="3"/>

Or when you want to create new user, let blank user-id attribute, or don't put that.

When you want to reset form, ex. To reset to the first step, and clear filled fields. You can use:

$wizardFormInstance->resetForm();

When you want to have current step instance. You can use:

$wizardFormInstance->getCurrentStep();

When you want to go to specific step. You can use:

$wizardFormInstance->setStep($step);

Or, you want to go in the next step:

$wizardFormInstance->goToNextStep();

Or, you want to go in the prev step:

$wizardFormInstance->goToPrevStep();

Creating a wizard step

You can create wizard form step. Open or create your step class (at App\Steps folder) and make sure it extends the Step class:

<?php

namespace App\Steps;

use Vildanbina\LivewireWizard\Components\Step;
use Illuminate\Validation\Rule;

class General extends Step
{
    // Step view located at resources/views/steps/general.blade.php 
    protected string $view = 'steps.general';

    /*
     * Initialize step fields
     */
    public function mount()
    {
        $this->mergeState([
            'name'                  => $this->model->name,
            'email'                 => $this->model->email,
        ]);
    }
    
    /*
    * Step icon 
    */
    public function icon(): string
    {
        return 'check';
    }

    /*
     * When Wizard Form has submitted
     */
    public function save($state)
    {
        $user = $this->model;

        $user->name     = $state['name'];
        $user->email    = $state['email'];
        
        $user->save();
    }

    /*
     * Step Validation
     */
    public function validate()
    {
        return [
            [
                'state.name'     => ['required', Rule::unique('users', 'name')->ignoreModel($this->model)],
                'state.email'    => ['required', Rule::unique('users', 'email')->ignoreModel($this->model)],
            ],
            [],
            [
                'state.name'     => __('Name'),
                'state.email'    => __('Email'),
            ],
        ];
    }

    /*
     * Step Title
     */
    public function title(): string
    {
        return __('General');
    }
}

Note: Remember to use the prefix state. in the wire:model attribute in views, for example: wire:model="state.name"

In Step class, you can use livewire hooks example:

use Vildanbina\LivewireWizard\Components\Step;

class General extends Step
{
    public function onStepIn($newStep)
    {
        // Something you want
    }

    public function onStepOut($oldStep)
    {
        // Something you want
    }

    public function updating($name, $value)
    {
        // Something you want
    }

    public function updatingState($name, $value)
    {
        // Something you want
    }
    
    public function updated($name, $value)
    {
        // Something you want
    }

    public function updatedState($name, $value)
    {
        // Something you want
    }
}

Each step need to have view, you can pass view path in $view property.

After create step class, you need to put that step to wizard form:

<?php

namespace App\Livewire;

use App\Steps\General;
use Vildanbina\LivewireWizard\WizardComponent;

class UserWizard extends WizardComponent
{
    public array $steps = [
        General::class,
        // Other steps...
    ];
   
    ...
}

Building Tailwind CSS for production

Because some classes are dynamically build and to compile js you should add some classes to the purge safelist so your tailwind.config.js should look something like this:

module.exports = {
    presets: [
        require("./vendor/wireui/wireui/tailwind.config.js")
    ],
    content: [
        "./resources/**/*.blade.php",
        "./resources/**/*.js",
        "./resources/**/*.vue",

        "./vendor/vildanbina/livewire-wizard/resources/views/*.blade.php",
        "./vendor/wireui/wireui/resources/**/*.blade.php",
        "./vendor/wireui/wireui/ts/**/*.ts",
        "./vendor/wireui/wireui/src/View/**/*.php"
    ],
    plugins: [
        require("@tailwindcss/forms"),
    ],
};

If you haven't installed tailwindcss/forms plugin, install it: npm install -D @tailwindcss/forms

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please e-mail [email protected] to report any security vulnerabilities instead of the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

livewire-wizard's People

Contributors

trippo avatar vildanbina avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

livewire-wizard's Issues

Adding custom function to the Step to manipulate the state

Can you assit i need a way to call the addToCollection function from the step view but it's complain that method not found since i need to maintain all the action that are happening with the step being done in the given Step

public function mount()
    {
        $this->mergeState([
            'locations' => collect([
                [
                    'name' => '',
                ]
            ])
        ]);
    }

//


 public function addToCollection()
    {
        $this->state['locations']->push(['name' => '']);
    }

    public function removeFromCollection($key)
    {
        $this->state['locations']->pull($key);
    }

Add support for livewire 3.0

Package cannot be upgraded with livewire 3.0:

vildanbina/livewire-wizard[v1.2, ..., v1.2.3] require livewire/livewire ^2.0 -> found livewire/livewire[v2.0.0, ..., 2.x-dev] but it conflicts with your root composer.json require (^3.0).
- Root composer.json requires vildanbina/livewire-wizard ^1.2 -> satisfiable by vildanbina/livewire-wizard[v1.2, v1.2.1, v1.2.2, v1.2.3].

Problems using Laravel 9.36.4

With Version 9.36.4 of Laravel i get the following error when trying to show a Wizard:

Declaration of Vildanbina\LivewireWizard\Components\Step::view(string $view): static must be compatible with Illuminate\View\Component::view($view, $data = [], $mergeData = [])

Have problem emitting from Javascript inside a step back to the component

I am working on a step where the user will be able to take a photo by using the camera. That is already working and the camera shows up and everything. BUT...

window.livewire.emit('storePhoto', canvas.toDataURL());

That line is doing nothing and it doesn't matter if I test

this.Livewire.emit('storePhoto', canvas.toDataURL());

or...

Livewire.emit('storePhoto', canvas.toDataURL());

Nothing happens and yes I have the protected $listeners setup and everything. Do you have any idea why I can't emit data to a function inside a step using the wizard?

pass value to parent componenet

Hello and thanks for good package
but I want to send some data from Step component to parent Component, how?
I was thinking about $this->emit($my_data), but Step extended component does not support it.
I want to pass some data to Parent livewire component to Step child component too, is it possible?

how to add separate model for each step

as support single model what if some one try to add multiple models for examples
on first step user info on second step post info on third step category info which is three separate table each table have their own
form

PHP 8.0 support

Can you please add PHP 8.0 support, as not many other Laravel packages has supported 8.1 yet

Example source code

Hi, do you have any example source code?

Because the View Blade part I can't see how.

Call to undefined function Vildanbina\LivewireWizard\str()

Hey,

Currently getting an error whenever I attempt to input something into an input field with a wire:model="state.example". I get this error: Call to undefined function Vildanbina\LivewireWizard\str() and it points me to the vendor/vildanbina/livewire-wizard/src/WizardComponent.php page on line 129 where it has $name = str($name);. All I'm doing is using the wire model like I've shown above, and on my save function I have

$model = $this-model;

$model->example = $state['example'];

$model->save();

and on my validation I'm doing this

return [
    [
        'state.example' => ['required']
    ],
    [
        'state.example' => __('Example')
    ]
]

This is creating a new model/entry so when using the wizard I've left it blank, e.g. <livewire:verify-wizard />.

Image upload (WithFileUploads) function don't work with livewire-wizard

When i try to use the script
In: Livewire wizardcomponent
use Livewire\WithFileUploads; class CreateCampanha extends WizardComponent { use WithFileUploads; public $photo; ...
Blade:
... <input type="file" wire:model="photo">
And in Step controller:
`
public function save()
{
$this->validate([
'photo' => 'image|max:1024', // 1MB Max
]);

    $this->photo->store('photos');
}

`
But when the send button is pressed it gives this error Call to a member function store() on null this error seems to inform that no file was selected in the input

Hook issue

Looks like the hook is not working as described in the doc

    public function onStepIn($name, $value)
    {
        // Something you want
        Log::debug("here :{$name}, {$value}" . get_class($this));
    }

Too few arguments to function App\Classes\WizardSteps\DocTemplate\UploadTemplate::onStepIn(), 0 passed in /Users/rabol/code/web/sign/vendor/vildanbina/livewire-wizard/src/Concerns/HasHooks.php on line 13 and exactly 2 expected

Does not like long class names

I have created a wizard like this:

php artisan make:livewire UserDocTemplateWizard

In a normal blade view I have this:

<livewire:user-doc-template-wizard user-id="{{auth()->user()->id}}"/>

That works fine, but when I extend my Livewire Component from WizardComponent

I get this error:

Unable to locate a class or view for component [errors].

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.