sietse85/nova-button

(Nova 4+) 为您的资源添加按钮的 Laravel Nova 扩展包。

1.0.26 2023-08-21 15:30 UTC

README

一个 Nova 扩展包,用于在索引、详细和透镜视图中渲染按钮。

使用按钮来触发后端事件、导航 Nova 路由或访问链接。

example-users

此扩展包是 dillingham/nova-button 的延续。

创建以兼容 Nova 4.0+

要求

安装

您可以通过运行以下命令来安装此扩展包:

composer require sietse85/nova-button

要发布配置文件,请运行以下命令:

php artisan vendor:publish --tag="nova-button-config"

使用方法

use Sietse85\NovaButton\Button;
public function fields(Request $request)
{
    return [
        ID::make('ID', 'id')->sortable(),
        Text::make('Name', 'name'),
        Button::make('Notify'),
    ];
}

快速链接

确认

您可以为破坏性行为要求确认。

Button::make('Cancel Account')->confirm('Are you sure?'),
Button::make('Cancel Account')->confirm('Confirmation', 'Are you sure you want to cancel your account?'),
Button::make('Cancel Account')->confirm('Confirmation', 'Are you sure you want to cancel your account?', 'Cancel'),

重新加载

您可以在所有事件完成后重新加载页面。

Button::make('Notify')->reload()

如果您点击多个按钮,重新加载将等待所有按钮完成。

如果发生错误,页面将不会重新加载。

操作

您现在可以触发操作。不能使用 ActionFields。将只为当前资源触发。将立即调用处理器。

Button::make('Send mail')->action(SendConfirmationMail::class)

如果由于某种原因操作模型的解析不正确,您可以使用 modelForAction 来纠正它。

事件

默认情况下,点击按钮将通过 AJAX 触发事件。

默认事件: Sietse85\NovaButton\Events\ButtonClick

该事件将接收触发事件的资源模型和键

  • $event->resource = model
  • $event->key = "notify"

您可以覆盖键

Button::make('Notify', 'notify-some-user')

以及事件

Button::make('Notify')->event(App\Events\NotifyRequested::class)

您可以通过创建监听器并在您的 EventServiceProvider 中注册它来监听事件。

文本

标题

您可以为按钮设置标题属性

Button::make('Notify')->title('Button title')

标签

您可以为按钮设置标签,该标签在详细、创建和更新视图中显示

Button::make('Notify')->label('Button label')

索引名称

您可以为按钮设置索引名称,该名称在索引视图中作为表头显示

Button::make('Notify')->indexName('Actions')

默认设置为按钮名称。您还可以传递 null 以不设置索引名称。

状态

可见性

您可以根据条件显示按钮

Button::make('Activate')->visible($this->is_active === false),
Button::make('Deactivate')->visible($this->is_active === true),

或者,如果您只想让特定用户看到按钮

Button::make('Notify')->visible($request->user()->can('notifyUser', $this))

当然,您也可以使用 Nova 的内置方法,如用于 授权 或将可见性限制为 特定视图

如果您想在创建或更新视图中显示按钮,您可以使用 Nova 的内置方法

Button::make('Notify')->showOnCreating()->showOnUpdating()

禁用

您可以使按钮不可用

Button::make('Notify')->disabled()
Button::make('Notify')->disabled($this->is_complete === false)

反馈

当使用事件时,您可能希望向最终用户提供视觉反馈。这对于长时间运行的监听器特别有用。

Button::make('Notify')
    ->loadingText('Sending...')
    ->successText('Sent!')
    ->errorText('Something went wrong...')

有 3 个事件,并且对于每个事件,您都可以提供文本和样式

默认值在 配置文件 中定义。

Nova 路由

您还可以选择导航到任何 Nova 路由

Button::make('Text')->index(App\Nova\User::class)
Button::make('Text')->detail(App\Nova\User::class, $this->user_id)
Button::make('Text')->create(App\Nova\User::class)
Button::make('Text')->edit(App\Nova\User::class, $this->user_id)
Button::make('Text')->lens(App\Nova\User::class, 'users-without-confirmation')

还可以使用资源过滤器

Button::make('Text')
    ->index(App\Nova\Order::class)
    ->withFilters([
        App\Nova\Filters\UserOrders::class => $this->user_id,
        App\Nova\Filters\OrderStatus::class => 'active',
    ])

链接

您可以配置按钮以打开外部链接

Button::make('Text')->link('https://nova.laravel.net.cn')
Button::make('Text')->link('https://nova.laravel.net.cn', '_self')

按钮使用了以下类,您可以按自己的喜好进行样式设置

.nova-button
.nova-button-{resource-name}
.nova-button-success
.nova-button-error
.nova-button-loading

您还可以为按钮添加更多类

// One class
Button::make('Notify')->classes(['some-class'])

// Or multiple classes
Button::make('Notify')->classes(['some-class', 'another-class'])

您还可以为确认模态的内部div添加更多类

// One class
Button::make('Notify')->confirm('sure?')->modalClasses(['some-class'])

// Or multiple classes
Button::make('Notify')->confirm('sure?')->modalClasses(['some-class', 'another-class'])

样式

此包使用 tailwind-css 类。默认使用的类是 link 类。

您可以定义按钮应使用的类

Button::make('Delete')->style('danger')

默认可用的类如下

传递的键指的是在 配置文件 中定义的类之一。您可以自由更改这些类或添加自己的类。

示例

透镜

您可以使用带有 透镜 的按钮。

lens-example

首先设置透镜

<?php

namespace App\Nova\Lenses;

class UsersWithoutConfirmation extends Lens
{
    public static function query(LensRequest $request, $query)
    {
        return $query
            ->select(['users.id', 'users.name'])
            ->whereNull('email_verified_at');
    }

    public function fields(Request $request)
    {
        return [
            ID::make('ID', 'id'),
            Text::make('Name', 'name'),
            Button::make('Mark As Confirmed'),
        ];
    }
}

接下来,在您的 事件服务提供者 中注册对 Sietse85\NovaButton\Events\ButtonClick 事件的监听器

<?php

namespace App\Listeners;

use Sietse85\NovaButton\Events\ButtonClick;

class ConfirmUser
{
    public function handle(ButtonClick $event)
    {
        if ($event->key == 'mark-as-confirmed') {
            $event->resource->email_verified_at = now();
            $event->resource->save();
        }
    }
}

要确认上述功能中使用的功能和事件数据,请参阅下面的 Telescope 检查

telescope

更新日志

有关最近更改的更多信息,请参阅 更新日志

贡献

有关详细信息,请参阅 贡献

致谢

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件