|
| 1 | +--- |
| 2 | +title: Actions (beta) |
| 3 | +weight: 4 |
| 4 | +--- |
| 5 | + |
| 6 | +Actions is a beta feature, that allows for the creation of Action Buttons that appear above the toolbar. These are ideal for common Actions that do not impact existing records, such as a "Create", "Assign", "Back" buttons. |
| 7 | + |
| 8 | +This is NOT recommended for production use at this point in time. |
| 9 | + |
| 10 | +## Usage |
| 11 | +To use "Actions", while it is in beta, you must include the following Trait in your table: |
| 12 | +```php |
| 13 | +use Rappasoft\LaravelLivewireTables\Traits\WithActions; |
| 14 | +``` |
| 15 | + |
| 16 | +Once out of beta, this will not be required. |
| 17 | + |
| 18 | +## Component Available Methods |
| 19 | +### setActionWrapperAttributes |
| 20 | + |
| 21 | +This is used to set attributes for the "div" that wraps all defined Action Buttons: |
| 22 | + |
| 23 | +```php |
| 24 | + public function configure(): void |
| 25 | + { |
| 26 | + $this->setActionWrapperAttributes([ |
| 27 | + 'class' => 'space-x-4' |
| 28 | + ]); |
| 29 | + } |
| 30 | +``` |
| 31 | + |
| 32 | +### actions() |
| 33 | + |
| 34 | +Define your actions using the actions() method: |
| 35 | + |
| 36 | +```php |
| 37 | +public function actions(): array |
| 38 | +{ |
| 39 | + return [ |
| 40 | + Action::make('View Dashboard') |
| 41 | + ->setRoute('dashboard'), |
| 42 | + ]; |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +## Button Available Methods |
| 47 | + |
| 48 | +### setActionAttributes |
| 49 | + |
| 50 | +setActionAttributes is used to pass any attributes that you wish to implement on the "button" element for the action button. By default it will merge with the default classes. |
| 51 | + |
| 52 | +You can set "default-styling" and "default-colors" to false to replace, rather than over-ride either default-styling or default-colors. |
| 53 | +```php |
| 54 | +public function actions(): array |
| 55 | +{ |
| 56 | + return [ |
| 57 | + Action::make('View Dashboard') |
| 58 | + ->setActionAttributes([ |
| 59 | + 'class' => 'dark:bg-blue-500 dark:text-white dark:border-blue-600 dark:hover:border-blue-900 dark:hover:bg-blue-800', |
| 60 | + 'default-colors' => false, // Will over-ride the default colors |
| 61 | + 'default-styling' => true // Will use the default styling |
| 62 | + ]), |
| 63 | + ]; |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +### setIcon |
| 68 | + |
| 69 | +setIcon is used to set an icon for the action button |
| 70 | + |
| 71 | +```php |
| 72 | +public function actions(): array |
| 73 | +{ |
| 74 | + return [ |
| 75 | + Action::make('Edit Item') |
| 76 | + ->setIcon("fas fa-edit"), |
| 77 | + ]; |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +### setIconAttributes |
| 82 | + |
| 83 | +setIconAttributes is used to set any additional attributes for the Icon for the action button |
| 84 | +```php |
| 85 | +public function actions(): array |
| 86 | +{ |
| 87 | + return [ |
| 88 | + Action::make('Edit Item') |
| 89 | + ->setIcon("fas fa-edit") |
| 90 | + ->setIconAttributes(['class' => 'font-4xl text-4xl']), |
| 91 | + ]; |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +### setRoute |
| 96 | + |
| 97 | +Used for non-wireable butons, to set the route that the action button should take the user to upon clicking. |
| 98 | +```php |
| 99 | +public function actions(): array |
| 100 | +{ |
| 101 | + return [ |
| 102 | + Action::make('Dashboard') |
| 103 | + ->setRoute('dashboard') |
| 104 | + ]; |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### wireNavigate |
| 109 | + |
| 110 | +Used in conjunction with setRoute - makes the link "wire:navigate" rather than default behaviour |
| 111 | +```php |
| 112 | +public function actions(): array |
| 113 | +{ |
| 114 | + return [ |
| 115 | + Action::make('Dashboard') |
| 116 | + ->setRoute('dashboard') |
| 117 | + ->wireNavigate() |
| 118 | + ]; |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +### setWireAction |
| 123 | +```php |
| 124 | +public function actions(): array |
| 125 | +{ |
| 126 | + return [ |
| 127 | + Action::make('Create 2') |
| 128 | + ->setWireAction("wire:click") |
| 129 | + ]; |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### setWireActionParams |
| 134 | +Specify the action & parameters to pass to the wire:click method |
| 135 | + |
| 136 | +```php |
| 137 | +public function actions(): array |
| 138 | +{ |
| 139 | + return [ |
| 140 | + Action::make('Create 2') |
| 141 | + ->setWireAction("wire:click") |
| 142 | + ->setWireActionParams(['id' => 'test']), |
| 143 | + ]; |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +### setWireActionDispatchParams |
| 148 | + |
| 149 | +Use $dispatch rather than a typical wire:click action |
| 150 | + |
| 151 | +```php |
| 152 | +public function actions(): array |
| 153 | +{ |
| 154 | + return [ |
| 155 | + Action::make('Create 2') |
| 156 | + ->setWireAction("wire:click") |
| 157 | + ->setWireActionDispatchParams("'openModal', { component: 'test-modal' }"), |
| 158 | + ]; |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +### setView |
| 163 | + |
| 164 | +This is used to set a Custom View for the Button |
| 165 | + |
| 166 | +```php |
| 167 | +public function actions(): array |
| 168 | +{ |
| 169 | + return [ |
| 170 | + Action::make('Edit Item') |
| 171 | + ->setView("buttons.edit"), |
| 172 | + ]; |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +## Extending |
| 177 | + |
| 178 | +You can extend the Base Action class which can be a useful timesaver, when you wish to re-use the same look/feel of an Action, but wish to set a different route (for example). |
| 179 | +You can set any defaults in the __construct method, ensuring that the parent constructor is called first! |
| 180 | + |
| 181 | +```php |
| 182 | +use Rappasoft\LaravelLivewireTables\Views\Action; |
| 183 | + |
| 184 | +class EditAction extends Action |
| 185 | +{ |
| 186 | + public function __construct(?string $label = null) |
| 187 | + { |
| 188 | + parent::__construct($label); |
| 189 | + $this |
| 190 | + ->setActionAttributes([ |
| 191 | + 'class' => 'dark:bg-blue-500 dark:text-white dark:border-blue-600 dark:hover:border-blue-900 dark:hover:bg-blue-800', |
| 192 | + 'default-colors' => false, |
| 193 | + 'default-styling' => true |
| 194 | + ]) |
| 195 | + ->setIcon("fas fa-edit") |
| 196 | + ->setIconAttributes([ |
| 197 | + 'class' => 'font-4xl text-4xl' |
| 198 | + ]); |
| 199 | + } |
| 200 | +} |
| 201 | +``` |
| 202 | + |
| 203 | +You may define a Custom View to be used via either the setView method, or by defining the view directly in your class. |
| 204 | +```php |
| 205 | + protected string $view = 'buttons.edit-action'; |
| 206 | +``` |
| 207 | + |
0 commit comments