Skip to content

Commit 04e0177

Browse files
authored
Merge pull request #755 from rappasoft/develop
v2.5
2 parents 489041c + ab019a9 commit 04e0177

File tree

10 files changed

+109
-33
lines changed

10 files changed

+109
-33
lines changed

CHANGELOG.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
44

55
## [Unreleased]
66

7+
## [2.5.0] - 2022-05-03
8+
9+
### Added
10+
11+
- Ability to pass mount parameters to configurable areas
12+
13+
### Changed
14+
15+
- Move configure call to boot instead of booteed.
16+
- Mount methods now available in `configure()` method.
17+
- Non-field columns with a searchable callback are now included in the search query.
18+
- Fixed debug query output duplicating select statements.
19+
- Fixed header issue on column hide - https://github.com/rappasoft/laravel-livewire-tables/pull/754
20+
21+
### Removed
22+
23+
- Calls to set builder and columns in render as it doesn't seem to make a difference since it's also called in booted.
24+
725
## [2.4.0] - 2022-04-30
826

927
### Added
@@ -633,7 +651,8 @@ Ground Up Rebuild
633651

634652
- Initial release
635653

636-
[Unreleased]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.4.0...development
654+
[Unreleased]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.5.0...development
655+
[2.5.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.4.0...v2.5.0
637656
[2.4.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.3.0...v2.4.0
638657
[2.3.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.2.1...v2.3.0
639658
[2.2.1]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.2.0...v2.2.1

docs/datatable/configurable-areas.md

+17
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ public function configure(): void
6565
}
6666
```
6767

68+
## Passing Parameters
69+
70+
You can pass parameters to configurable areas that come from your mount method or any other service by proving an array:
71+
72+
```php
73+
public function configure(): void
74+
{
75+
$this->setConfigurableAreas([
76+
'toolbar-left-start' => [
77+
'path.to.my.view', [
78+
'param1' => $this->user_id,
79+
'param2' => resolve(MyService::class)->getThing($this->user_id),
80+
],
81+
],
82+
]);
83+
}
84+
```
6885

6986
## Example View
7087

resources/views/components/pagination.blade.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@endphp
77

88
@if ($component->hasConfigurableAreaFor('before-pagination'))
9-
@include($component->getConfigurableAreaFor('before-pagination'))
9+
@include($component->getConfigurableAreaFor('before-pagination'), $component->getParametersForConfigurableArea('before-pagination'))
1010
@endif
1111

1212
@if ($theme === 'tailwind')
@@ -102,5 +102,5 @@
102102
@endif
103103

104104
@if ($component->hasConfigurableAreaFor('after-pagination'))
105-
@include($component->getConfigurableAreaFor('after-pagination'))
105+
@include($component->getConfigurableAreaFor('after-pagination'), $component->getParametersForConfigurableArea('after-pagination'))
106106
@endif

resources/views/components/table/th.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
{{
2626
$attributes->merge($customSortButtonAttributes)
2727
->class(['flex items-center space-x-1 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider group focus:outline-none dark:text-gray-400' => $customSortButtonAttributes['default'] ?? true])
28-
->except('default')
28+
->except(['default', 'wire:key'])
2929
}}
3030
>
3131
<span>{{ $column->getTitle() }}</span>

resources/views/components/tools/toolbar.blade.php

+14-14
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
@endphp
66

77
@if ($component->hasConfigurableAreaFor('before-toolbar'))
8-
@include($component->getConfigurableAreaFor('before-toolbar'))
8+
@include($component->getConfigurableAreaFor('before-toolbar'), $component->getParametersForConfigurableArea('before-toolbar'))
99
@endif
1010

1111
@if ($theme === 'tailwind')
1212
<div class="md:flex md:justify-between mb-4 px-4 md:p-0">
1313
<div class="w-full mb-4 md:mb-0 md:w-2/4 md:flex space-y-4 md:space-y-0 md:space-x-2">
1414
@if ($component->hasConfigurableAreaFor('toolbar-left-start'))
15-
@include($component->getConfigurableAreaFor('toolbar-left-start'))
15+
@include($component->getConfigurableAreaFor('toolbar-left-start'), $component->getParametersForConfigurableArea('toolbar-left-start'))
1616
@endif
1717

1818
@if ($component->reorderIsEnabled())
@@ -136,13 +136,13 @@ class="w-full inline-flex items-center justify-center px-3 py-2 border border-gr
136136
@endif
137137

138138
@if ($component->hasConfigurableAreaFor('toolbar-left-end'))
139-
@include($component->getConfigurableAreaFor('toolbar-left-end'))
139+
@include($component->getConfigurableAreaFor('toolbar-left-end'), $component->getParametersForConfigurableArea('toolbar-left-end'))
140140
@endif
141141
</div>
142142

143143
<div class="md:flex md:items-center space-y-4 md:space-y-0 md:space-x-2">
144144
@if ($component->hasConfigurableAreaFor('toolbar-right-start'))
145-
@include($component->getConfigurableAreaFor('toolbar-right-start'))
145+
@include($component->getConfigurableAreaFor('toolbar-right-start'), $component->getParametersForConfigurableArea('toolbar-right-start'))
146146
@endif
147147

148148
@if ($component->showBulkActionsDropdown())
@@ -287,7 +287,7 @@ class="block w-full border-gray-300 rounded-md shadow-sm transition duration-150
287287
@endif
288288

289289
@if ($component->hasConfigurableAreaFor('toolbar-right-end'))
290-
@include($component->getConfigurableAreaFor('toolbar-right-end'))
290+
@include($component->getConfigurableAreaFor('toolbar-right-end'), $component->getParametersForConfigurableArea('toolbar-right-end'))
291291
@endif
292292
</div>
293293
</div>
@@ -321,7 +321,7 @@ class="block text-sm font-medium leading-5 text-gray-700 dark:text-white">
321321
<div class="d-md-flex justify-content-between mb-3">
322322
<div class="d-md-flex">
323323
@if ($component->hasConfigurableAreaFor('toolbar-left-start'))
324-
@include($component->getConfigurableAreaFor('toolbar-left-start'))
324+
@include($component->getConfigurableAreaFor('toolbar-left-start'), $component->getParametersForConfigurableArea('toolbar-left-start'))
325325
@endif
326326

327327
@if ($component->reorderIsEnabled())
@@ -435,13 +435,13 @@ class="dropdown-item btn text-center"
435435
@endif
436436

437437
@if ($component->hasConfigurableAreaFor('toolbar-left-end'))
438-
@include($component->getConfigurableAreaFor('toolbar-left-end'))
438+
@include($component->getConfigurableAreaFor('toolbar-left-end'), $component->getParametersForConfigurableArea('toolbar-left-end'))
439439
@endif
440440
</div>
441441

442442
<div class="d-md-flex">
443443
@if ($component->hasConfigurableAreaFor('toolbar-right-start'))
444-
@include($component->getConfigurableAreaFor('toolbar-right-start'))
444+
@include($component->getConfigurableAreaFor('toolbar-right-start'), $component->getParametersForConfigurableArea('toolbar-right-start'))
445445
@endif
446446

447447
@if ($component->showBulkActionsDropdown())
@@ -532,7 +532,7 @@ class="form-control"
532532
@endif
533533

534534
@if ($component->hasConfigurableAreaFor('toolbar-right-end'))
535-
@include($component->getConfigurableAreaFor('toolbar-right-end'))
535+
@include($component->getConfigurableAreaFor('toolbar-right-end'), $component->getParametersForConfigurableArea('toolbar-right-end'))
536536
@endif
537537
</div>
538538
</div>
@@ -562,7 +562,7 @@ class="d-block">
562562
<div class="d-md-flex justify-content-between mb-3">
563563
<div class="d-md-flex">
564564
@if ($component->hasConfigurableAreaFor('toolbar-left-start'))
565-
@include($component->getConfigurableAreaFor('toolbar-left-start'))
565+
@include($component->getConfigurableAreaFor('toolbar-left-start'), $component->getParametersForConfigurableArea('toolbar-left-start'))
566566
@endif
567567

568568
@if ($component->reorderIsEnabled())
@@ -674,13 +674,13 @@ class="dropdown-item text-center"
674674
@endif
675675

676676
@if ($component->hasConfigurableAreaFor('toolbar-left-end'))
677-
@include($component->getConfigurableAreaFor('toolbar-left-end'))
677+
@include($component->getConfigurableAreaFor('toolbar-left-end'), $component->getParametersForConfigurableArea('toolbar-left-end'))
678678
@endif
679679
</div>
680680

681681
<div class="d-md-flex">
682682
@if ($component->hasConfigurableAreaFor('toolbar-right-start'))
683-
@include($component->getConfigurableAreaFor('toolbar-right-start'))
683+
@include($component->getConfigurableAreaFor('toolbar-right-start'), $component->getParametersForConfigurableArea('toolbar-right-start'))
684684
@endif
685685

686686
@if ($component->showBulkActionsDropdown())
@@ -771,7 +771,7 @@ class="form-control"
771771
@endif
772772

773773
@if ($component->hasConfigurableAreaFor('toolbar-right-end'))
774-
@include($component->getConfigurableAreaFor('toolbar-right-end'))
774+
@include($component->getConfigurableAreaFor('toolbar-right-end'), $component->getParametersForConfigurableArea('toolbar-righ-end'))
775775
@endif
776776
</div>
777777
</div>
@@ -800,5 +800,5 @@ class="d-block">
800800
@endif
801801

802802
@if ($component->hasConfigurableAreaFor('after-toolbar'))
803-
@include($component->getConfigurableAreaFor('after-toolbar'))
803+
@include($component->getConfigurableAreaFor('after-toolbar'), $component->getParametersForConfigurableArea('after-toolbar'))
804804
@endif

src/DataTableComponent.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,22 @@ public function boot(): void
5959

6060
// Set the filter defaults based on the filter type
6161
$this->setFilterDefaults();
62-
63-
// Call the child configuration, if any
64-
$this->configure();
65-
66-
// Make sure a primary key is set
67-
if (! $this->hasPrimaryKey()) {
68-
throw new DataTableConfigurationException('You must set a primary key using setPrimaryKey in the configure method.');
69-
}
7062
}
7163

7264
/**
7365
* Runs on every request, after the component is mounted or hydrated, but before any update methods are called
7466
*/
7567
public function booted(): void
7668
{
69+
$this->configure();
7770
$this->setTheme();
7871
$this->setBuilder($this->builder());
7972
$this->setColumns();
73+
74+
// Make sure a primary key is set
75+
if (! $this->hasPrimaryKey()) {
76+
throw new DataTableConfigurationException('You must set a primary key using setPrimaryKey in the configure method.');
77+
}
8078
}
8179

8280
/**
@@ -118,8 +116,6 @@ public function customView(): string
118116
*/
119117
public function render()
120118
{
121-
$this->setBuilder($this->builder());
122-
$this->setColumns();
123119
$this->setupColumnSelect();
124120
$this->setupPagination();
125121
$this->setupSecondaryHeader();

src/Traits/Helpers/ColumnHelpers.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function getSelectableColumns(): Collection
103103
public function getSearchableColumns(): Collection
104104
{
105105
return $this->getColumns()
106-
->filter(fn (Column $column) => $column->isSearchable())
106+
->filter(fn (Column $column) => $column->isSearchable() || $column->hasSearchCallback())
107107
->values();
108108
}
109109

src/Traits/Helpers/ComponentHelpers.php

+25-3
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,35 @@ public function hasConfigurableAreaFor(string $area): bool
362362
}
363363

364364
/**
365-
* @param string $area
365+
* @param string|array $area
366366
*
367367
* @return string|null
368368
*/
369-
public function getConfigurableAreaFor(string $area): ?string
369+
public function getConfigurableAreaFor($area): ?string
370+
{
371+
$area = $this->configurableAreas[$area] ?? null;
372+
373+
if (is_array($area)) {
374+
return $area[0];
375+
}
376+
377+
return $area;
378+
}
379+
380+
/**
381+
* @param string|array $area
382+
*
383+
* @return array
384+
*/
385+
public function getParametersForConfigurableArea($area): array
370386
{
371-
return $this->configurableAreas[$area] ?? null;
387+
$area = $this->configurableAreas[$area] ?? null;
388+
389+
if (is_array($area) && isset($area[1]) && is_array($area[1])) {
390+
return $area[1];
391+
}
392+
393+
return [];
372394
}
373395

374396
/**

src/Traits/WithData.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,6 @@ protected function getTableForColumn(Column $column): ?string
143143

144144
protected function getQuerySql(): string
145145
{
146-
return $this->baseQuery()->toSql();
146+
return (clone $this->getBuilder())->toSql();
147147
}
148148
}

tests/Traits/Helpers/ComponentHelpersTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,28 @@ public function can_get_configurable_areas(): void
182182
]);
183183

184184
$this->assertEquals('includes.areas.toolbar-left-start', $this->basicTable->getConfigurableAreaFor('toolbar-left-start'));
185+
186+
$this->basicTable->setConfigurableAreas([
187+
'toolbar-left-start' => ['includes.areas.toolbar-left-start', ['param1' => 'hello']],
188+
]);
189+
190+
$this->assertEquals('includes.areas.toolbar-left-start', $this->basicTable->getConfigurableAreaFor('toolbar-left-start'));
191+
}
192+
193+
/** @test */
194+
public function can_get_configurable_area_parameters(): void
195+
{
196+
$this->basicTable->setConfigurableAreas([
197+
'toolbar-left-start' => 'includes.areas.toolbar-left-start',
198+
]);
199+
200+
$this->assertEquals([], $this->basicTable->getParametersForConfigurableArea('toolbar-left-start'));
201+
202+
$this->basicTable->setConfigurableAreas([
203+
'toolbar-left-start' => ['includes.areas.toolbar-left-start', ['param1' => 'hello']],
204+
]);
205+
206+
$this->assertEquals(['param1' => 'hello'], $this->basicTable->getParametersForConfigurableArea('toolbar-left-start'));
185207
}
186208

187209
/** @test */

0 commit comments

Comments
 (0)