edwardhendrix / livewire-alert
本包为 Livewire 组件提供简单的警报工具。
Requires
- php: ^7.4|^8.0|^8.1|^8.2
- illuminate/support: ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
- livewire/livewire: ^3.0@beta
Requires (Dev)
- orchestra/testbench: ^6.15|^7.0|^8.0
- phpunit/phpunit: ^9.5|^10.0
This package is auto-updated.
Last update: 2024-09-19 17:10:59 UTC
README
Livewire Alert 是一个简单的警报工具包,旨在无缝集成到您的 Livewire 组件中。在底层,它使用 SweetAlert2,为您提供 SweetAlert2 的功能,而无需任何自定义 JavaScript。
交互式演示
在此处查看交互式演示:[https://livewire-alert.jantinnerezo.com](https://livewire-alert.jantinnerezo.com)
为交互式演示贡献力量
您有任何想法可以添加到交互式演示中吗?在此处 fork 并提交 PR:[https://github.com/jantinnerezo/livewire-alert-demo](https://github.com/jantinnerezo/livewire-alert-demo)
安装
您可以通过 composer 安装此包
composer require jantinnerezo/livewire-alert
接下来,在模板中在 @livewireScripts 之后添加 scripts 组件。
SweetAlert2 脚本默认不包含,确保在 livewire alert 脚本之前包含它。
<body> @livewireScripts <script src="//cdn.jsdelivr.net.cn/npm/sweetalert2@11"></script> <x-livewire-alert::scripts /> </body>
您也可以通过发布 livewire-alert.js 手动包含脚本
php artisan vendor:publish --tag=livewire-alert:assets
然后在您的视图中,您可以包含已发布的脚本,而不是包含内联脚本 <x-livewire-alert::scripts /> 组件。
如果您选择此路径,请确保在 livewire-alert 脚本之后立即包含
<x-livewire-alert::flash />,如果您仍然想要闪存功能。
<script src="{{ asset('vendor/livewire-alert/livewire-alert.js') }}"></script> <x-livewire-alert::flash />
要求
本包旨在与 Livewire 组件一起使用。请确保您只在使用 Livewire 项目时使用它。
-
PHP 7.2 或更高版本
-
Laravel 7, 8, 9 和 10
-
Livewire
-
SweetAlert2
用法
您可以通过使用 LivewireAlert trait 来使用 livewire alert。
use Jantinnerezo\LivewireAlert\LivewireAlert; class Index extends Component { use LivewireAlert; public function submit() { $this->alert('success', 'Basic Alert'); } }
显示不同的警报图标。
默认警报行为是 toast 通知。
$this->alert('success', 'Success is approaching!');
$this->alert('warning', 'The world has warned you.');
$this->alert('info', 'The fact is you know your name :D');
$this->alert('question', 'How are you today?');
禁用 toast 通知警报处理。
$this->alert('info', 'This is not as toast alert', [ 'toast' => false ]);
警报位置
$this->alert('info', 'Centering alert', [ 'position' => 'center' ]);
以下警报位置的列表
- top
- top-start
- top-end
- center
- center-start
- center-end
- bottom
- bottom-start
- bottom-end
按钮
SweetAlert2 有 3 个按钮,默认不显示。
要显示确认按钮,只需将 showConfirmButton 传递给警报配置并设置为 true。
$this->alert('question', 'How are you today?', [ 'showConfirmButton' => true ]);
更改确认按钮文本
$this->alert('question', 'How are you today?', [ 'showConfirmButton' => true, 'confirmButtonText' => 'Good' ]);
添加当确认按钮被点击时的事件。首先创建一个当确认按钮被点击时会触发的函数
public function confirmed() { // Do something }
将其添加到事件监听器数组中注册。
protected $listeners = [ 'confirmed' ];
或者
public function getListeners() { return [ 'confirmed' ]; }
然后将其传递到警报配置的 onConfirmed 键。
$this->alert('question', 'How are you today?', [ 'showConfirmButton' => true, 'confirmButtonText' => 'Good', 'onConfirmed' => 'confirmed' ]);
您还可以传递一个参数到事件以获取警报响应。
当您需要获取警报内部输入的值时非常有用。
$this->alert('warning', 'Please enter password', [ 'showConfirmButton' => true, 'confirmButtonText' => 'Submit', 'onConfirmed' => 'confirmed', 'input' => 'password', 'inputValidator' => '(value) => new Promise((resolve) => '. ' resolve('. ' /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$^+=!*()@%&]).{8,}$/.test(value) ?'. ' undefined : "Error in password"'. ' )'. ')', 'allowOutsideClick' => false, 'timer' => null ]);
public function confirmed($data) { // Get input value and do anything you want to it $password = $data['value']; }
只需对 deny 和 cancel 按钮做同样的事情。只需为每个按钮创建一个函数并将其注册到事件监听器中。
public function denied() { // Do something when denied button is clicked }
public function cancelled() { // Do something when cancel button is clicked }
public function getListeners() { return [ 'denied', 'dismissed' ]; }
确保将 showDenyButton 和 showCancelButton 设置为 true。
$this->alert('warning', 'Alert with deny and cancel button', [ 'showDenyButton' => true, 'denyButtonText' => 'Deny', 'showCancelButton' => true, 'cancelButtonText' => 'Cancel', 'onDenied' => 'denied', 'onDismissed' => 'cancelled' ]);
向特定组件发出事件。不要直接将监听器传递给事件,而是传递一个包含 component 和 listeners 键的数组。
'onConfirmed' => [ 'component' => 'livewire-component', 'listener' => 'confirmed' ];
不想每次显示警报确认时都定义额外的按钮配置?请使用 confirm 方法。
您始终可以通过调整配置来覆盖默认确认设置。
$this->confirm('Are you sure do want to leave?', [ 'onConfirmed' => 'confirmed', ]);
闪存通知
您还可以将警报用作闪存通知。您可以将重定向路由传递到第四个参数,默认重定向到 /。
$this->flash('success', 'Successfully submitted form', [], '/');
配置
通过发布 livewire-alert.php 配置文件来覆盖默认警报配置。
php artisan vendor:publish --tag=livewire-alert:config
[
'alert' => [
'position' => 'top-end',
'timer' => 3000,
'toast' => true,
'text' => null,
'showCancelButton' => false,
'showConfirmButton' => false
],
'confirm' => [
'icon' => 'warning',
'position' => 'center',
'toast' => false,
'timer' => null,
'showConfirmButton' => true,
'showCancelButton' => true,
'cancelButtonText' => 'No',
'confirmButtonColor' => '#3085d6',
'cancelButtonColor' => '#d33'
]
]
定制
您可以通过传入自定义类来自定义警告样式,与 TailwindCSS 完美兼容
[ 'customClass' => [ 'container' => '', 'popup' => '', 'header' => '', 'title' => '', 'closeButton' => '', 'icon' => '', 'image' => '', 'content' => '', 'htmlContainer' => '', 'input' => '', 'inputLabel' => '', 'validationMessage' => '', 'actions' => '', 'confirmButton' => '', 'denyButton' => '', 'cancelButton' => '', 'loader' => '', 'footer' => '' ] ];
有关自定义和配置的更多详细信息,请查看 SweetAlert2
贡献者
变更日志
请参阅 CHANGELOG 了解最近有哪些变化。
贡献
有关详细信息,请参阅 CONTRIBUTING
安全
如果您发现任何与安全相关的问题,请发送电子邮件至 erezojantinn@gmail.com,而不是使用问题跟踪器。
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件