New syntax for adding custom buttons to the index table

By @samuel · 2021-08-30 13:13

New syntax for adding custom buttons to the index table

Recently I added a feature which simplified the syntax for adding buttons for customs actions — see the tweet here.

For those who aren't familiar with Lean's feature set, one of the most important features is action configuration.

Basically, you can make changes to how the default index/show/edit/create actions work (and you can also add multiple of those actions, e.g. multiple index pages with different default filters).

The feature mentioned in the tweet basically lets you add a button next to the default Show/Edit/Delete buttons — using a nice fluent syntax with a click() handler. It looks like this:

public static function actions(): array
{
    return [
        Index::make()
            ->buttons(append: TextButton::make('Impersonate')
                ->icon('heroicon-o-login')
                ->click(function (User $user, Component $livewire) {
                    Auth::loginUsingId($user->id);

                    Lean::notifyOnNextPage('Logged in as ' . $user->name);

                    $livewire->redirect(Lean::$homeUrl);
                })
            ),
        Show::make(),
        Edit::make(),
        Create::make(),
    ];
}

I'll make a few more posts to showcase the action configuration, because that's easily one of Lean's most powerful features.