Skip to content

Commit ec5401a

Browse files
authored
Add setIconLeft/setIconRight (rappasoft#1877)
* Add setIconLeft/setIconRight * Fix styling * Adjust Test * Adjust ActionTest --------- Co-authored-by: lrljoe <[email protected]>
1 parent f0c5d04 commit ec5401a

File tree

4 files changed

+106
-8
lines changed

4 files changed

+106
-8
lines changed

docs/misc/actions.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,38 @@ public function actions(): array
8484
}
8585
```
8686

87+
### setIconLeft
88+
89+
setIconLeft is used to prepend the Icon to the Text (Non-Default Behaviour)
90+
91+
```php
92+
public function actions(): array
93+
{
94+
return [
95+
Action::make('Edit Item')
96+
->setIcon("fas fa-edit")
97+
->setIconAttributes(['class' => 'font-4xl text-4xl'])
98+
->setIconLeft(),
99+
];
100+
}
101+
```
102+
103+
### setIconRight
104+
105+
setIconRight is used to append the Icon to the Text (Default Behaviour)
106+
107+
```php
108+
public function actions(): array
109+
{
110+
return [
111+
Action::make('Edit Item')
112+
->setIcon("fas fa-edit")
113+
->setIconAttributes(['class' => 'font-4xl text-4xl'])
114+
->setIconRight(),
115+
];
116+
}
117+
```
118+
87119
### setRoute
88120

89121
Used for non-wireable butons, to set the route that the action button should take the user to upon clicking.
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<a {{ $attributes->merge()
2-
->class(['justify-center text-center items-center inline-flex rounded-md border shadow-sm px-4 py-2 text-sm font-medium focus:ring focus:ring-opacity-50' => $isTailwind && $attributes['default-styling'] ?? true])
2+
->class(['justify-center text-center items-center inline-flex space-x-2 rounded-md border shadow-sm px-4 py-2 text-sm font-medium focus:ring focus:ring-opacity-50' => $isTailwind && $attributes['default-styling'] ?? true])
33
->class(['focus:border-indigo-300 focus:ring-indigo-200' => $isTailwind && $attributes['default-colors'] ?? true])
44
->class(['btn btn-sm btn-success' => $isBootstrap && $attributes['default-styling'] ?? true])
55
->class(['' => $isBootstrap && $attributes['default-colors'] ?? true])
@@ -12,13 +12,22 @@
1212
wire:navigate
1313
@endif
1414
>
15-
{{ $action->getLabel() }}
1615

17-
@if($action->hasIcon())
16+
@if($action->hasIcon() && $action->getIconRight())
17+
<span>{{ $action->getLabel() }}</span>
1818
<i {{ $action->getIconAttributes()
19-
->class(["ms-1 ". $action->getIcon() => $isBootstrap])
20-
->class(["ml-1 ". $action->getIcon() => $isTailwind])
21-
->except('default-styling')
22-
}}></i>
19+
->class(["ms-1 ". $action->getIcon() => $isBootstrap])
20+
->class(["ml-1 ". $action->getIcon() => $isTailwind])
21+
->except('default-styling')
22+
}}
23+
></i>
24+
@elseif($action->hasIcon() && !$action->getIconRight())
25+
<i {{ $action->getIconAttributes()
26+
->class(["ms-1 ". $action->getIcon() => $isBootstrap])
27+
->class(["ml-1 ". $action->getIcon() => $isTailwind])
28+
->except('default-styling')
29+
}}
30+
></i>
31+
<span>{{ $action->getLabel() }}</span>
2332
@endif
2433
</a>

src/Views/Traits/Core/HasIcon.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ trait HasIcon
1111

1212
public array $iconAttributes = ['default-styling' => true];
1313

14+
public bool $iconRight = true;
15+
1416
public function setIcon(string $icon): self
1517
{
1618
$this->icon = $icon;
@@ -39,4 +41,23 @@ public function getIconAttributes(): ComponentAttributeBag
3941
{
4042
return new ComponentAttributeBag([...['default-styling' => true], ...$this->iconAttributes]);
4143
}
44+
45+
public function getIconRight(): bool
46+
{
47+
return $this->iconRight ?? true;
48+
}
49+
50+
public function setIconLeft(): self
51+
{
52+
$this->iconRight = false;
53+
54+
return $this;
55+
}
56+
57+
public function setIconRight(): self
58+
{
59+
$this->iconRight = true;
60+
61+
return $this;
62+
}
4263
}

tests/Views/Actions/ActionTest.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,42 @@ public function exportBulk($items)
296296

297297
}
298298

299+
public function test_can_set_icon_to_right_default(): void
300+
{
301+
$action = Action::make('Update Summaries')
302+
->setActionAttributes(['class' => 'dark:bg-green-500 dark:text-white dark:border-green-600 dark:hover:border-green-900 dark:hover:bg-green-800', 'default-styling' => true, 'default-colors' => true])
303+
->setIcon('fas fa-minus')
304+
->setIconAttributes(['class' => 'font-sm text-sm'])
305+
->setWireAction('wire:click')
306+
->setWireActionParams('testactionparams');
307+
$this->assertTrue($action->getIconRight());
308+
}
309+
310+
public function test_can_set_icon_to_left(): void
311+
{
312+
$action = Action::make('Update Summaries')
313+
->setActionAttributes(['class' => 'dark:bg-green-500 dark:text-white dark:border-green-600 dark:hover:border-green-900 dark:hover:bg-green-800', 'default-styling' => true, 'default-colors' => true])
314+
->setIcon('fas fa-minus')
315+
->setIconAttributes(['class' => 'font-sm text-sm'])
316+
->setIconLeft()
317+
->setWireAction('wire:click')
318+
->setWireActionParams('testactionparams');
319+
$this->assertFalse($action->getIconRight());
320+
}
321+
322+
public function test_can_set_icon_to_right(): void
323+
{
324+
$action = Action::make('Update Summaries')
325+
->setActionAttributes(['class' => 'dark:bg-green-500 dark:text-white dark:border-green-600 dark:hover:border-green-900 dark:hover:bg-green-800', 'default-styling' => true, 'default-colors' => true])
326+
->setIcon('fas fa-minus')
327+
->setIconAttributes(['class' => 'font-sm text-sm'])
328+
->setWireAction('wire:click')
329+
->setWireActionParams('testactionparams')
330+
->setIconLeft()
331+
->setIconRight();
332+
$this->assertTrue($action->getIconRight());
333+
}
334+
299335
public function test_action_renders_correctly(): void
300336
{
301337
$action = Action::make('Update Summaries')
@@ -305,6 +341,6 @@ public function test_action_renders_correctly(): void
305341
)
306342
->route('dashboard22');
307343

308-
$this->assertStringContainsString('<a class="focus:border-indigo-300 focus:ring-indigo-200 justify-center text-center items-center inline-flex rounded-md border shadow-sm px-4 py-2 text-sm font-medium focus:ring focus:ring-opacity-50 dark:bg-green-500 dark:text-white dark:border-green-600 dark:hover:border-green-900 dark:hover:bg-green-800" href="dashboard22"', $action->render());
344+
$this->assertStringContainsString('<a class="focus:border-indigo-300 focus:ring-indigo-200 justify-center text-center items-center inline-flex space-x-2 rounded-md border shadow-sm px-4 py-2 text-sm font-medium focus:ring focus:ring-opacity-50 dark:bg-green-500 dark:text-white dark:border-green-600 dark:hover:border-green-900 dark:hover:bg-green-800" href="dashboard22"', $action->render());
309345
}
310346
}

0 commit comments

Comments
 (0)