Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support action groups in notifications #3454

Merged
merged 3 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/notifications/docs/01-installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ You're now ready to start [sending notifications](sending-notifications)!
The package uses the following dependencies:

- [Alpine.js](https://alpinejs.dev)
- [Alpine Floating UI](https://github.com/awcodes/alpine-floating-ui)
- [Tailwind CSS](https://tailwindcss.com)

You may install these through NPM:
Expand Down Expand Up @@ -146,12 +147,14 @@ In `/resources/css/app.css`, import [Tailwind CSS](https://tailwindcss.com):

### Configuring scripts

In `/resources/js/app.js`, import [Alpine.js](https://alpinejs.dev) and the `filament/notifications` plugin, and register it:
In `/resources/js/app.js`, import [Alpine.js](https://alpinejs.dev), Alpine Floating UI and the `filament/notifications` plugin, and register them:

```js
import Alpine from 'alpinejs'
import AlpineFloatingUI from '@awcodes/alpine-floating-ui'
import NotificationsAlpinePlugin from '../../vendor/filament/notifications/dist/module.esm'

Alpine.plugin(AlpineFloatingUI)
Alpine.plugin(NotificationsAlpinePlugin)

window.Alpine = Alpine
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<x-filament-support::actions.group
:actions="$getActions()"
:dark-mode="config('notifications.dark_mode')"
:color="$getColor()"
:icon="$getIcon()"
:label="$getLabel()"
:tooltip="$getTooltip()"
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<x-notifications::actions.action
:action="$action"
component="notifications::dropdown.item"
:icon-position="$getIconPosition()"
class="filament-notifications-grouped-action"
>
{{ $getLabel() }}
</x-notifications::actions.action>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@captureSlots([
'detail',
])

<x-filament-support::dropdown.item
:attributes="\Filament\Support\prepare_inherited_attributes($attributes)->merge($slots)"
:dark-mode="config('notifications.dark_mode')"
>
{{ $slot }}
</x-filament-support::dropdown.item>
7 changes: 7 additions & 0 deletions packages/notifications/src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public function button(): static
return $this;
}

public function grouped(): static
{
$this->view('notifications::actions.grouped-action');

return $this;
}

public function link(): static
{
$this->view('notifications::actions.link-action');
Expand Down
41 changes: 41 additions & 0 deletions packages/notifications/src/Actions/ActionGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Filament\Notifications\Actions;

use Filament\Support\Actions\ActionGroup as BaseActionGroup;
use Illuminate\Contracts\Support\Arrayable;

class ActionGroup extends BaseActionGroup implements Arrayable
{
protected string $view = 'notifications::actions.group';

public function toArray(): array
{
return [
'actions' => collect($this->getActions())->toArray(),
'color' => $this->getColor(),
'icon' => $this->getIcon(),
'iconPosition' => $this->getIconPosition(),
'label' => $this->getLabel(),
'tooltip' => $this->getTooltip(),
];
}

public static function fromArray(array $data): static
{
$static = static::make(
array_map(
fn (array $action): Action => Action::fromArray($action),
$data['actions'],
),
);

$static->color($data['color']);
$static->icon($data['icon']);
$static->iconPosition($data['iconPosition']);
$static->label($data['label']);
$static->tooltip($data['tooltip']);

return $static;
}
}
8 changes: 5 additions & 3 deletions packages/notifications/src/Concerns/HasActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace Filament\Notifications\Concerns;

use Closure;
use Filament\Notifications\Actions\ActionGroup;
use Illuminate\Support\Arr;

trait HasActions
{
protected array | Closure $actions = [];
protected array | ActionGroup | Closure $actions = [];

public function actions(array | Closure $actions): static
public function actions(array | ActionGroup | Closure $actions): static
{
$this->actions = $actions;

Expand All @@ -17,6 +19,6 @@ public function actions(array | Closure $actions): static

public function getActions(): array
{
return $this->evaluate($this->actions);
return Arr::wrap($this->evaluate($this->actions));
}
}
13 changes: 11 additions & 2 deletions packages/notifications/src/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Filament\Notifications;

use Filament\Notifications\Actions\Action;
use Filament\Notifications\Actions\ActionGroup;
use Filament\Notifications\Concerns\HasActions;
use Filament\Notifications\Concerns\HasBody;
use Filament\Notifications\Concerns\HasDuration;
Expand Down Expand Up @@ -43,7 +44,7 @@ public function toArray(): array
{
return [
'id' => $this->getId(),
'actions' => collect($this->getActions())->toArray(),
'actions' => array_map(fn (Action | ActionGroup $action): array => $action->toArray(), $this->getActions()),
'body' => $this->getBody(),
'duration' => $this->getDuration(),
'icon' => $this->getIcon(),
Expand All @@ -55,7 +56,15 @@ public function toArray(): array
public static function fromArray(array $data): static
{
$static = static::make($data['id']);
$static->actions(array_map(fn (array $action): Action => Action::fromArray($action), $data['actions']));
$static->actions(
array_map(
fn (array $action): Action | ActionGroup => match (array_key_exists('actions', $action)) {
true => ActionGroup::fromArray($action),
false => Action::fromArray($action),
},
$data['actions'],
),
);
$static->body($data['body']);
$static->duration($data['duration']);
$static->icon($data['icon']);
Expand Down
2 changes: 2 additions & 0 deletions packages/notifications/stubs/scaffolding/resources/js/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Alpine from 'alpinejs'
import AlpineFloatingUI from '@awcodes/alpine-floating-ui'
import NotificationsAlpinePlugin from '../../vendor/filament/notifications/dist/module.esm'

Alpine.plugin(AlpineFloatingUI)
Alpine.plugin(NotificationsAlpinePlugin)

window.Alpine = Alpine
Expand Down