Skip to content

Commit

Permalink
V3 Develop to V3 Master (#1439)
Browse files Browse the repository at this point in the history
Co-authored-by: lrljoe <[email protected]>

* v3 - Adding CollapseAlways Option for Columns (#1403)

* Add CollapseAlways & Fix ReorderColumn

* Remove superfluous md:hidden from the icon

* Tweaks for Column Collapsing For BS4/BS5

* Fix Collapsed on Bootstrap

* Tweak Bootstrap Theme

---------

Co-authored-by: lrljoe <[email protected]>

* Minor tweaks to blades for reorder cols (#1411)

* V3 - Fix Reorder For Bootstrap (#1412)

* Minor tweaks to blades for reorder cols

* Bootstrap Reorder Toggle

* Add DiscordBot (#1413)

* Docs livewire namespace fix (#1420)

* Fixed Livewire namespace in docs

* Updated livewire url in docs

* V3  - Fix Localisation (#1424)

* Fix Localisations

* Fix styling

* Load Original and Publish Localisation Paths

* Fix styling

---------

Co-authored-by: lrljoe <[email protected]>

* v3 - Add Loading Placeholder (#1421)

* Initial Loading Placeholder Code, Use Hourglass Spinner

* Add wrappers, Class Merging, Colouring

* Renaming Traits - Adding LoadingPlaceholder Tests, Add Placeholder Test Table

* Update Docs for Placeholder

---------

Co-authored-by: lrljoe <[email protected]>

* v3 - Column - Allow Enum in ReturnType for Column getContent/renderContent (#1436)

* Fix Enum Column Return TypeHints

* v3 - Add "Confirmation" Option to Bulk Actions (#1437)

* Add BulkActionConfirms

* Add wire:confirm capability to bulk actions

* Update Changelog

* Fix error with typehinting

* Add Tests

* Adding default confirmation message customisation

* Update Default Message

* Update Tests

---------

Co-authored-by: lrljoe <[email protected]>

---------

Co-authored-by: lrljoe <[email protected]>
Co-authored-by: Anthony Rappa <[email protected]>
  • Loading branch information
3 people authored Oct 25, 2023
1 parent 707b6b8 commit ca4ddd5
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 10 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

All notable changes to `laravel-livewire-tables` will be documented in this file

## [3.0.0-beta.4] - 2023-10-17
## [3.0.0-beta.4] - 2023-10-25
- 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
- Introduction of Loading Placeholder
- Docs livewire namespace fix [Here](https://github.com/rappasoft/laravel-livewire-tables/pull/1420)
- Add CollapseAlways capability for Columns
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 : [];
}
}
4 changes: 2 additions & 2 deletions src/Views/Traits/Helpers/ColumnHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function getColumnSelectName(): ?string
}

// TODO: Test
public function renderContents(Model $row): null|string|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
public function renderContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
{
if ($this->shouldCollapseOnMobile() && $this->shouldCollapseOnTablet()) {
throw new DataTableConfigurationException('You should only specify a columns should collapse on mobile OR tablet, not both.');
Expand All @@ -103,7 +103,7 @@ public function renderContents(Model $row): null|string|HtmlString|DataTableConf
}

// TODO: Test
public function getContents(Model $row): null|string|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
{
if ($this->isLabel()) {
$value = call_user_func($this->getLabelCallback(), $row, $this);
Expand Down
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());
}
}

0 comments on commit ca4ddd5

Please sign in to comment.