aliqasemzadeh/livewire-bootstrap-modal

动态Laravel Livewire 3 Bootstrap模态框。

v2.2.0 2024-03-15 07:35 UTC

This package is auto-updated.

Last update: 2024-09-08 18:03:58 UTC


README

此包允许您在Bootstrap模态框中动态显示Laravel Livewire 3组件。

警告:此包与Livewire 2不兼容。

文档

要求

  • Bootstrap 5和PopperJS必须先通过npm安装
npm install bootstrap
npm install @popperjs/core

安装

需要此包

composer require aliqasemzadeh/livewire-bootstrap-modal

livewire:modals组件添加到您的应用布局视图中

<livewire:modals/>
<livewire:scripts/>
<script src="{{ asset('js/app.js') }}"></script>

在您的应用JavaScript文件中需要../../vendor/aliqasemzadeh/livewire-bootstrap-modal/resources/js/modals

import('@popperjs/core');
import '../../node_modules/bootstrap/dist/js/bootstrap.bundle.min.js';
import '../../vendor/aliqasemzadeh/livewire-bootstrap-modal/resources/js/modals.js';

使用

模态视图

创建一个您想要作为模态框显示的Livewire组件。该组件的视图必须使用Bootstrap的modal-dialog容器

<div>
        <div class="modal-header">
            <h5 class="modal-title">Modal title</h5>
            <button type="button" class="btn-close" wire:click="$dispatch('hideModal')" aria-label="Close"></button>
        </div>
        <div class="modal-body">
            <p>Modal body text goes here.</p>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary"  wire:click="$dispatch('hideModal')">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
        </div>
</div>
 

显示模态框

通过发出带有组件别名的showModal事件来显示模态框

<button type="button" wire:click="$dispatch('showModal', {data: {'alias' : 'livewire.modal.user','params' :{'user':'Ali Qasemzadeh'}}})">
    {{ __('Update Profile') }}
</button>

挂载参数

在别名之后传递参数到组件的mount方法

<button type="button"wire:click="$dispatch('showModal', {data: {'alias' : 'livewire.modal.user','params' :{'user':'Ali Qasemzadeh'}}})">
    {{ __('Update User #' . $user->id) }}
</button>

上述示例中组件的mount方法看起来像这样

namespace App\Http\Livewire\Users;

use App\Models\User;
use Livewire\Component;

class Update extends Component
{
    public $user;
    
    public function mount(User $user)
    {
        $this->user = $user;
    }
    
    public function render()
    {
        return view('users.update');
    }
}

隐藏模态框

通过发出hideModal事件来隐藏当前打开的模态框

<button type="button" wire:click="$dispatch('hideModal')">
    {{ __('Close') }}
</button>

取消模态框

您可以在视图中发出事件

<button type="button" wire:click="$dispatch('hideModal')">
    {{ __('Close') }}
</button>

或在组件内部,就像任何正常的Livewire事件一样

public function save()
{
    $this->validate();

    // save the record

    $this->dispatch('hideModal');
}

自定义模态框大小

现在您可以通过默认使用modal-lg来自定义模态框大小

<button type="button"wire:click="$dispatch('showModal', {data: {'alias' : 'livewire.modal.user','size' :'modal-xl')">
  {{ __('Show XL Modal') }}
</button>

发布资源

自定义视图

使用您自己的模态框视图,通过发布包的视图

php artisan vendor:publish --tag=livewire-bootstrap-modal:views

现在编辑位于resources/views/vendor/livewire-bootstrap-modal内的视图文件。包将使用此视图来渲染组件。

注意事项

1- 您应该在主槽中使用按钮或@click。

2- 我们正在努力解决一些问题,我们希望很快修复它们。

3- 我们将很快添加一些选项。

4- wire:navigate不工作(只有第一个模态框打开)。