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

v3 - Add "Confirmation" Option to Bulk Actions #1437

Merged
merged 22 commits into from
Oct 25, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to `laravel-livewire-tables` will be documented in this file

## UNRELEASED
- Fix Return Type hinting for Column Rendering to allow Enum columns
- Add Bulk Action Confirmations, using wire:confirm
- setBulkActionConfirms
- setBulkActionConfirmMessage
- setBulkActionConfirmMessages
- setBulkActionDefaultConfirmationMessage
- Localisation for confirmation message


## [3.0.0-beta.4] - 2023-10-17
- Introduction of Loading Placeholder
Expand Down
51 changes: 51 additions & 0 deletions docs/bulk-actions/available-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,54 @@ public function configure(): void
$this->setHideBulkActionsWhenEmptyDisabled();
}
```

## setBulkActionConfirms

When a bulk action is included in the array passed to setBulkActionConfirms, the default wire:confirm pop-up will appear prior to executing the bulk action. The default message is: "Are you sure?". This should only be used if you wish to use the default message.

```php
public function configure(): void
{
$this->setBulkActionConfirms([
'delete',
'reset'
]);
}
```
## setBulkActionDefaultConfirmationMessage

You may use this method to over-ride the default message. To override the confirmation message for an individual Bulk Action, see the below setBulkActionConfirmMessage and setBulkActionConfirmMessages. You may also use the language files to do this.

```php
public function configure(): void
{
$this->setBulkActionDefaultConfirmationMessage('Are you certain?');
}
```

## setBulkActionConfirmMessage

You may use this method to specify a message other than the default message.

```php
public function configure(): void
{
$this->setBulkActionConfirmMessage('delete', 'Do you want to delete these items?');
}
```

## setBulkActionConfirmMessages

You may pass an array to this method, to more effectively update the confirmation message for a larger quantity of bulk actions. This expects an array keyed by the bulk action name, with the value being the message that will be displayed to the user.

```php
public function configure(): void
{
$this->setBulkActionConfirmMessages([
'delete' => 'Are you sure you want to delete these items?',
'purge' => 'Are you sure you want to purge these items?',
'reassign' => 'This will reassign selected items, are you sure?',
]);
}
```

1 change: 1 addition & 0 deletions resources/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"Applied Filters": "Applied Filters",
"Applied Sorting": "Applied Sorting",
"Bulk Actions": "Bulk Actions",
"Bulk Actions Confirm": "Are you sure?",
"Clear": "Clear",
"Columns": "Columns",
"Debugging Values": "Debugging Values",
Expand Down
3 changes: 3 additions & 0 deletions resources/views/components/tools/toolbar/bootstrap.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@
<a
href="#"
wire:click="{{ $action }}"
@if($component->hasConfirmationMessage($action))
wire:confirm="{{ $component->getBulkActionConfirmMessage($action) }}"
@endif
wire:key="{{ $tableName }}-bulk-action-{{ $action }}"
@class([
'dropdown-item' => $component->isBootstrap(),
Expand Down
3 changes: 3 additions & 0 deletions resources/views/components/tools/toolbar/tailwind.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ class="origin-top-right absolute right-0 mt-2 w-full md:w-48 rounded-md shadow-l
@foreach ($component->getBulkActions() as $action => $title)
<button
wire:click="{{ $action }}"
@if($component->hasConfirmationMessage($action))
wire:confirm="{{ $component->getBulkActionConfirmMessage($action) }}"
@endif
wire:key="{{ $tableName }}-bulk-action-{{ $action }}"
type="button"
class="block w-full px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 hover:text-gray-900 focus:outline-none focus:bg-gray-100 focus:text-gray-900 flex items-center space-x-2 dark:text-white dark:hover:bg-gray-600"
Expand Down
34 changes: 34 additions & 0 deletions src/Traits/Configuration/BulkActionsConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,38 @@ public function setHideBulkActionsWhenEmptyDisabled(): self

return $this;
}

public function setBulkActionConfirms(array $bulkActionConfirms): self
{
foreach ($bulkActionConfirms as $bulkAction) {
if (! $this->hasConfirmationMessage($bulkAction)) {
$this->setBulkActionConfirmMessage($bulkAction, $this->getBulkActionDefaultConfirmationMessage());
}
}

return $this;
}

public function setBulkActionConfirmMessage(string $action, string $confirmationMessage): self
{
$this->bulkActionConfirms[$action] = $confirmationMessage;

return $this;
}

public function setBulkActionConfirmMessages(array $bulkActionMessages): self
{
foreach ($bulkActionMessages as $bulkAction => $confirmationMessage) {
$this->setBulkActionConfirmMessage($bulkAction, $confirmationMessage);
}

return $this;
}

public function setBulkActionDefaultConfirmationMessage(string $defaultConfirmationMessage): self
{
$this->bulkActionConfirmDefaultMessage = $defaultConfirmationMessage;

return $this;
}
}
22 changes: 20 additions & 2 deletions src/Traits/Helpers/BulkActionsHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,30 @@ public function setAllSelected(): void
{
$this->setSelectAllEnabled();
$this->setSelected((clone $this->baseQuery())->pluck($this->getBuilder()->getModel()->getTable().'.'.$this->getPrimaryKey())->map(fn ($item) => (string) $item)->toArray());

//$this->setSelected((clone $this->baseQuery())->pluck($this->getPrimaryKey())->map(fn ($item) => (string) $item)->toArray());
}

public function showBulkActionsDropdownAlpine(): bool
{
return $this->bulkActionsAreEnabled() && $this->hasBulkActions();
}

public function getBulkActionConfirms(): array
{
return array_keys($this->bulkActionConfirms);
}

public function hasConfirmationMessage(string $bulkAction): bool
{
return isset($this->bulkActionConfirms[$bulkAction]);
}

public function getBulkActionConfirmMessage(string $bulkAction): string
{
return $this->bulkActionConfirms[$bulkAction] ?? $this->getBulkActionDefaultConfirmationMessage();
}

public function getBulkActionDefaultConfirmationMessage(): string
{
return isset($this->bulkActionConfirmDefaultMessage) ? $this->bulkActionConfirmDefaultMessage : __('Bulk Actions Confirm');
}
}
10 changes: 5 additions & 5 deletions src/Traits/WithBulkActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ trait WithBulkActions

public array $bulkActions = [];

public array $bulkActionConfirms = [];

public array $selected = [];

public bool $hideBulkActionsWhenEmpty = false;

public ?string $bulkActionConfirmDefaultMessage;

public function bulkActions(): array
{
if (property_exists($this, 'bulkActions')) {
return $this->bulkActions;
}

return [];
return property_exists($this, 'bulkActions') ? $this->bulkActions : [];
}
}
59 changes: 59 additions & 0 deletions tests/Traits/Configuration/BulkActionsConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,63 @@ public function can_set_bulk_actions(): void

$this->assertTrue($this->basicTable->hasBulkActions());
}

/** @test */
public function can_set_bulk_action_confirms(): void
{
$this->assertSame([], $this->basicTable->getBulkActionConfirms());

$this->basicTable->setBulkActionConfirms(['deactivate', 'delete']);

$this->assertSame(['deactivate', 'delete'], $this->basicTable->getBulkActionConfirms());

}

/** @test */
public function can_set_bulk_action_custom_message(): void
{
$this->basicTable->setBulkActionConfirms(['deactivate', 'delete']);

$this->assertSame($this->basicTable->getBulkActionDefaultConfirmationMessage(), $this->basicTable->getBulkActionConfirmMessage('deactivate'));

$this->basicTable->setBulkActionConfirmMessage('deactivate', 'do you want to deactivate?');

$this->assertSame('do you want to deactivate?', $this->basicTable->getBulkActionConfirmMessage('deactivate'));

$this->assertSame($this->basicTable->getBulkActionDefaultConfirmationMessage(), $this->basicTable->getBulkActionConfirmMessage('delete'));

}

/** @test */
public function can_set_bulk_action_custom_messages(): void
{
$this->basicTable->setBulkActionConfirms(['purge', 'delete', 'reassign', 'deactivate']);

$this->assertSame($this->basicTable->getBulkActionDefaultConfirmationMessage(), $this->basicTable->getBulkActionConfirmMessage('deactivate'));
$this->assertSame($this->basicTable->getBulkActionDefaultConfirmationMessage(), $this->basicTable->getBulkActionConfirmMessage('reassign'));
$this->assertSame($this->basicTable->getBulkActionDefaultConfirmationMessage(), $this->basicTable->getBulkActionConfirmMessage('delete'));
$this->assertSame($this->basicTable->getBulkActionDefaultConfirmationMessage(), $this->basicTable->getBulkActionConfirmMessage('purge'));

$this->basicTable->setBulkActionConfirmMessages([
'delete' => 'Are you sure you want to delete these items?',
'purge' => 'Are you sure you want to purge these items?',
'reassign' => 'This will reassign selected items, are you sure?',
]);

$this->assertSame('Are you sure you want to delete these items?', $this->basicTable->getBulkActionConfirmMessage('delete'));
$this->assertSame('This will reassign selected items, are you sure?', $this->basicTable->getBulkActionConfirmMessage('reassign'));
$this->assertSame($this->basicTable->getBulkActionDefaultConfirmationMessage(), $this->basicTable->getBulkActionConfirmMessage('deactivate'));
$this->assertSame('Are you sure you want to purge these items?', $this->basicTable->getBulkActionConfirmMessage('purge'));
}

/** @test */
public function can_set_bulk_action_default_confirmation_message(): void
{
$this->assertSame('Are you sure?', $this->basicTable->getBulkActionDefaultConfirmationMessage());

$this->basicTable->setBulkActionDefaultConfirmationMessage('Test Default Message');

$this->assertSame('Test Default Message', $this->basicTable->getBulkActionDefaultConfirmationMessage());

}
}
24 changes: 24 additions & 0 deletions tests/Traits/Helpers/BulkActionsHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,28 @@ public function set_select_all_selects_all(): void

$this->assertSame(['1', '2', '3', '4', '5'], $this->basicTable->getSelected());
}

/** @test */
public function can_get_bulk_action_confirms(): void
{
$this->assertSame([], $this->basicTable->getBulkActionConfirms());
}

/** @test */
public function can_find_if_bulk_action_has_confirm_message(): void
{
$this->assertFalse($this->basicTable->hasConfirmationMessage('test123'));
}

/** @test */
public function bulk_action_confirm_returns_default_message_if_not_set(): void
{
$this->assertSame($this->basicTable->getBulkActionDefaultConfirmationMessage(), $this->basicTable->getBulkActionConfirmMessage('test'));
}

/** @test */
public function can_get_bulk_action_default_confirmation_message(): void
{
$this->assertSame('Are you sure?', $this->basicTable->getBulkActionDefaultConfirmationMessage());
}
}