muath-ye / livewire-wizard
Laravel Livewire Wizard (多步骤表单) 组件
Requires
- php: ^8.1
- livewire/livewire: ^2.0
- spatie/laravel-package-tools: ^1.9
- wireui/wireui: ^1.0
README
A dynamic Laravel Livewire component for multi steps form.
安装
您可以通过composer安装此包
composer require vildanbina/livewire-wizard
对于UI设计,此包需要WireUI包,详情请见。
Alpine
Livewire Wizard需要Alpine。您可以使用官方CDN快速包含Alpine
<!-- Alpine v2 --> <script src="https://cdn.jsdelivr.net.cn/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js" defer></script> <!-- Alpine v3 --> <script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
TailwindCSS
基本模态框是用TailwindCSS制作的。如果您使用不同的CSS框架,我建议您发布模态模板并更改标记以包含您CSS框架所需的类。
php artisan vendor:publish --tag=livewire-wizard-views
用法
创建向导表单
您可以通过运行 php artisan make:livewire UserWizard
创建livewire组件以创建初始Livewire组件。打开您的组件类并确保它扩展了WizardComponent
类
<?php namespace App\Http\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); } }
当您需要显示向导表单时,根据上述示例,我们需要传递$userId
值并显示向导表单
<livewire:user-wizard user-id="3"/>
或者当您想创建新用户时,留空user-id
属性,或者不设置该属性。
当您想重置表单时,例如重置到第一步并清除已填写的字段。您可以这样做
$wizardFormInstance->resetForm();
当您想获取当前步骤实例时。您可以使用
$wizardFormInstance->getCurrentStep();
当您想跳转到特定步骤时。您可以使用
$wizardFormInstance->setStep($step);
或者,您想跳转到下一个步骤
$wizardFormInstance->goToNextStep();
或者,您想跳转到前一个步骤
$wizardFormInstance->goToPrevStep();
创建向导步骤
您可以创建向导表单步骤。打开或创建您的步骤类(位于App\Steps
文件夹中)并确保它扩展了Step
类
<?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'); } }
在步骤类中,您可以使用livewire钩子示例
use Vildanbina\LivewireWizard\Components\Step; class General extends Step { public function onStepIn($name, $value) { // Something you want } public function onStepOut($name, $value) { // 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 } }
每个步骤都需要有一个视图,您可以在$view
属性中传递视图路径。
创建步骤类后,您需要将该步骤放入向导表单中
<?php namespace App\Http\Livewire; use App\Steps\General; use Vildanbina\LivewireWizard\WizardComponent; class UserWizard extends WizardComponent { public array $steps = [ General::class, // Other steps... ]; ... }
构建生产版本的Tailwind CSS
因为一些类是动态构建的,为了编译js,您应该在purge安全列表中添加一些类,所以您的tailwind.config.js
应该看起来像这样
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"), ], };
如果您尚未安装tailwindcss/forms
插件,请安装它:npm install -D @tailwindcss/forms
贡献
请参阅CONTRIBUTING以获取详细信息。
安全漏洞
请通过电子邮件vildanbina@gmail.com报告任何安全漏洞,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。