Skip to content

Commit 44b857d

Browse files
authored
5.2.0 (#111)
* Added a new built-in `RedirectRowAction`, that is now used to render the pre-configured `ShowRowAction` * Added an optional second argument `bool $openInNewWindow = false` to the `ShowRowAction` * Added a new pre-configured `AddHeadAction`, that is using the built-in `RedirectHeadAction` * Added a new `Add` translation for it that you'll have to add to [your own translations](/README.md#translations) * Added a new `config('laravel-table.icon.add')` config for it with the `<i class="fa-solid fa-circle-plus fa-fw"></i>` default value that you'll also have to add to [your published configuration file](/README.md#configuration)
1 parent 661860c commit 44b857d

File tree

9 files changed

+150
-26
lines changed

9 files changed

+150
-26
lines changed

CHANGELOG.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Changelog
22

3+
All notable changes to this package will be documented in this file.
4+
5+
## [5.2.0](https://github.com/Okipa/laravel-table/compare/5.1.2...5.2.0)
6+
7+
2022-10-28
8+
9+
* Added a new built-in `RedirectRowAction`, that is now used to render the pre-configured `ShowRowAction`
10+
* Added an optional second argument `bool $openInNewWindow = false` to the `ShowRowAction`
11+
* Added a new pre-configured `AddHeadAction`, that is using the built-in `RedirectHeadAction`
12+
* Added a new `Add` translation for it that you'll have to add to [your own translations](/README.md#translations)
13+
* Added a new `config('laravel-table.icon.add')` config for it with the `<i class="fa-solid fa-circle-plus fa-fw"></i>` default value that you'll also have to add to [your published configuration file](/README.md#configuration)
14+
315
## [5.1.2](https://github.com/Okipa/laravel-table/compare/5.1.1...5.1.2)
416

517
2022-10-27
@@ -18,8 +30,8 @@
1830
2022-10-25
1931

2032
* Added ability to chain a `->when(bool $condition)` method to an instantiated head action, in order to enable it conditionally
21-
* Added a new built-in `RedirectHeadAction`, that will be used by the pre-configured `CreateHeadAction`
22-
* Added an optional `bool $openInNewWindow = false` to the `CreateHeadAction`
33+
* Added a new built-in `RedirectHeadAction`, that is now used to render the pre-configured `CreateHeadAction`
34+
* Added an optional second argument `bool $openInNewWindow = false` to the `CreateHeadAction`
2335
* Added a new [JavaScript snippet](/README.md#set-up-a-few-lines-of-javascript) to handle head action link opening in tab: you'll have to add it if you want to benefit from this new ability
2436

2537
## [5.0.2](https://github.com/Okipa/laravel-table/compare/5.0.1...5.0.2)

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ Status
185185
* `Actions`
186186
* `Bulk Actions`
187187
* `Create`
188+
* `Add`
188189
* `Show`
189190
* `Edit`
190191
* `Destroy`
@@ -475,10 +476,10 @@ If no head action is declared, the dedicated slot for it in the table head will
475476
This package provides the following built-in head actions:
476477
* `RedirectHeadAction`:
477478
* Requires `string $url`, `string $label`, `string $icon`, `array $class = ['btn', 'btn-success']` and `bool $openInNewWindow = false` arguments on instantiation
478-
* Redirects to the model create page from a click on a `Create` button
479+
* Redirects to the given URL from a click on the button
479480
* `CreateHeadAction`:
480481
* Requires `string $createUrl` and `bool $openInNewWindow = false` arguments on instantiation
481-
* Instantiate a pre-configured `RedirectHeadAction` with the given `$createUrl` as URL, `__('Create')` as label and `config('laravel-table.icon.create')` as icon
482+
* Instantiate a pre-configured `RedirectHeadAction` with `$createUrl` as URL, `__('Create')` as label and `config('laravel-table.icon.create')` as icon
482483

483484
To use one of them, you'll have to pass an instance of it to the `headAction` method.
484485

@@ -491,7 +492,7 @@ namespace App\Tables;
491492

492493
use App\Models\User;
493494
use Okipa\LaravelTable\Table;
494-
use Okipa\LaravelTable\HeadActions\CreateHeadAction;
495+
use Okipa\LaravelTable\HeadActions\AddHeadAction;
495496
use Okipa\LaravelTable\Abstracts\AbstractTableConfiguration;
496497

497498
class UsersTable extends AbstractTableConfiguration
@@ -501,7 +502,7 @@ class UsersTable extends AbstractTableConfiguration
501502
return Table::make()
502503
->model(User::class)
503504
// Create head action will not be available when authenticated user is not allowed to create users
504-
->headAction((new CreateHeadAction(route('user.create')))->when(Auth::user()->cannot('create_users')));
505+
->headAction((new AddHeadAction(route('user.create')))->when(Auth::user()->cannot('create_users')));
505506
}
506507
}
507508
```
@@ -638,9 +639,12 @@ If no row action is declared on your table, the dedicated `Actions` column at th
638639
**Important note:** [you'll have to set up a few lines of javascript](#set-up-a-few-lines-of-javascript) to allow row actions confirmation requests and feedback to be working properly.
639640

640641
This package provides the built-in following row actions:
642+
* `RedirectRowAction`:
643+
* Requires `string $url`, `string $title`, `string $icon`, `array $class = ['link-info']`, `string|null $defaultConfirmationQuestion = null`, `string|null $defaultFeedbackMessage = null` and `bool $openInNewWindow = false` arguments on instantiation
644+
* Redirects to the given URL from a click on the link
641645
* `ShowRowAction`:
642-
* Requires a `string $showUrl` argument on instantiation
643-
* Redirects to the model edit page on click
646+
* Requires `string $showUrl` and `bool $openInNewWindow = false` arguments on instantiation
647+
* Instantiate a pre-configured `RedirectRowAction` with `$showUrl` as URL, `__('Show')` as label and `config('laravel-table.icon.show')` as icon
644648
* `EditRowAction`:
645649
* Requires a `string $editUrl` argument on instantiation
646650
* Redirects to the model edit page on click

config/laravel-table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
'info' => '<i class="fa-solid fa-circle-info"></i>',
2424
'reset' => '<i class="fa-solid fa-rotate-left"></i>',
2525
'drag_drop' => '<i class="fa-solid fa-grip-vertical"></i>',
26+
'add' => '<i class="fa-solid fa-circle-plus fa-fw"></i>',
2627
'create' => '<i class="fa-solid fa-circle-plus fa-fw"></i>',
2728
'show' => '<i class="fa-solid fa-eye fa-fw"></i>',
2829
'edit' => '<i class="fa-solid fa-pencil fa-fw"></i>',

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ parameters:
88
- src/
99
- tests/
1010

11-
# The level 9 is the highest level
11+
# Level 9 is the highest level
1212
level: 5
1313

1414
databaseMigrationsPath:

src/Abstracts/AbstractRowAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ abstract class AbstractRowAction
1717

1818
public string $identifier;
1919

20-
protected string|null $class;
20+
protected array $class;
2121

2222
protected string $icon;
2323

src/HeadActions/AddHeadAction.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Okipa\LaravelTable\HeadActions;
4+
5+
use Livewire\Component;
6+
use Okipa\LaravelTable\Abstracts\AbstractHeadAction;
7+
8+
class AddHeadAction extends AbstractHeadAction
9+
{
10+
protected RedirectHeadAction $redirectHeadAction;
11+
12+
public function __construct(public string $createUrl, bool $openInNewWindow = false)
13+
{
14+
$this->redirectHeadAction = new RedirectHeadAction(
15+
url: $createUrl,
16+
label: __('Add'),
17+
icon: config('laravel-table.icon.add'),
18+
openInNewWindow: $openInNewWindow
19+
);
20+
}
21+
22+
protected function class(): array
23+
{
24+
return $this->redirectHeadAction->class();
25+
}
26+
27+
protected function title(): string
28+
{
29+
return $this->redirectHeadAction->title();
30+
}
31+
32+
protected function icon(): string
33+
{
34+
return $this->redirectHeadAction->icon();
35+
}
36+
37+
public function action(Component $livewire): void
38+
{
39+
$this->redirectHeadAction->action($livewire);
40+
}
41+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Okipa\LaravelTable\RowActions;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Str;
7+
use Livewire\Component;
8+
use Okipa\LaravelTable\Abstracts\AbstractRowAction;
9+
10+
class RedirectRowAction extends AbstractRowAction
11+
{
12+
public function __construct(
13+
public string $url,
14+
public string $title,
15+
public string $icon,
16+
public array $class = ['link-info'],
17+
public string|null $defaultConfirmationQuestion = null,
18+
public string|null $defaultFeedbackMessage = null,
19+
public bool $openInNewWindow = false,
20+
) {
21+
//
22+
}
23+
24+
protected function identifier(): string
25+
{
26+
return 'row_action_' . Str::snake($this->title);
27+
}
28+
29+
protected function class(Model $model): array
30+
{
31+
return $this->class;
32+
}
33+
34+
protected function icon(Model $model): string
35+
{
36+
return $this->icon;
37+
}
38+
39+
protected function title(Model $model): string
40+
{
41+
return $this->title;
42+
}
43+
44+
protected function defaultConfirmationQuestion(Model $model): string|null
45+
{
46+
return $this->defaultConfirmationQuestion;
47+
}
48+
49+
protected function defaultFeedbackMessage(Model $model): string|null
50+
{
51+
return $this->defaultFeedbackMessage;
52+
}
53+
54+
public function action(Model $model, Component $livewire): void
55+
{
56+
$this->openInNewWindow
57+
? $livewire->emit('laraveltable:link:open:newtab', $this->url)
58+
: redirect()->to($this->url);
59+
}
60+
}

src/RowActions/ShowRowAction.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,55 @@
33
namespace Okipa\LaravelTable\RowActions;
44

55
use Illuminate\Database\Eloquent\Model;
6-
use Illuminate\Http\RedirectResponse;
76
use Livewire\Component;
8-
use Livewire\Redirector;
97
use Okipa\LaravelTable\Abstracts\AbstractRowAction;
108

119
class ShowRowAction extends AbstractRowAction
1210
{
13-
public function __construct(public string $showUrl)
11+
protected RedirectRowAction $redirectRowAction;
12+
13+
public function __construct(public string $showUrl, public bool $openInNewWindow = false)
1414
{
15-
//
15+
$this->redirectRowAction = new RedirectRowAction(
16+
url: $showUrl,
17+
title: __('Show'),
18+
icon: config('laravel-table.icon.show'),
19+
openInNewWindow: $openInNewWindow
20+
);
1621
}
1722

1823
protected function identifier(): string
1924
{
20-
return 'row_action_show';
25+
return $this->redirectRowAction->identifier();
2126
}
2227

2328
protected function class(Model $model): array
2429
{
25-
return ['link-info'];
30+
return $this->redirectRowAction->class($model);
2631
}
2732

2833
protected function icon(Model $model): string
2934
{
30-
return config('laravel-table.icon.show');
35+
return $this->redirectRowAction->icon($model);
3136
}
3237

3338
protected function title(Model $model): string
3439
{
35-
return __('Show');
40+
return $this->redirectRowAction->title($model);
3641
}
3742

3843
protected function defaultConfirmationQuestion(Model $model): string|null
3944
{
40-
return null;
45+
return $this->redirectRowAction->defaultConfirmationQuestion($model);
4146
}
4247

4348
protected function defaultFeedbackMessage(Model $model): string|null
4449
{
45-
return null;
50+
return $this->redirectRowAction->defaultFeedbackMessage($model);
4651
}
4752

48-
public function action(Model $model, Component $livewire): RedirectResponse|Redirector
53+
public function action(Model $model, Component $livewire): void
4954
{
50-
return redirect()->to($this->showUrl);
55+
$this->redirectRowAction->action($model, $livewire);
5156
}
5257
}

tests/Unit/Bootstrap5/TableHeadActionTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Livewire\Livewire;
88
use Okipa\LaravelTable\Abstracts\AbstractTableConfiguration;
99
use Okipa\LaravelTable\Column;
10+
use Okipa\LaravelTable\HeadActions\AddHeadAction;
1011
use Okipa\LaravelTable\HeadActions\CreateHeadAction;
1112
use Okipa\LaravelTable\Table;
1213
use Tests\Models\User;
@@ -20,13 +21,13 @@ class TableHeadActionTest extends TestCase
2021
public function it_can_set_table_head_action(): void
2122
{
2223
app('router')->get('/user/create', ['as' => 'user.create']);
23-
Config::set('laravel-table.icon.create', 'create-icon');
24+
Config::set('laravel-table.icon.add', 'add-icon');
2425
$config = new class extends AbstractTableConfiguration
2526
{
2627
protected function table(): Table
2728
{
2829
return Table::make()->model(User::class)
29-
->headAction(new CreateHeadAction(route('user.create')));
30+
->headAction(new AddHeadAction(route('user.create')));
3031
}
3132

3233
protected function columns(): array
@@ -42,8 +43,8 @@ protected function columns(): array
4243
'<a wire:click.prevent="headAction()"',
4344
' class="btn btn-success"',
4445
' href=""',
45-
' title="Create">',
46-
'create-icon Create',
46+
' title="Add">',
47+
'add-icon Add',
4748
'</a>',
4849
])
4950
->call('headAction')

0 commit comments

Comments
 (0)