Skip to content

Commit 8ca00b3

Browse files
committed
Pass parameters to configurable areas
1 parent 26c5395 commit 8ca00b3

File tree

6 files changed

+77
-27
lines changed

6 files changed

+77
-27
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
44

55
## [Unreleased]
66

7+
### Added
8+
9+
- Ability to pass mount parameters to configurable areas
10+
11+
### Changed
12+
13+
- Move configure call to boot instead of booteed.
14+
- Mount methods now available in `configure()` method.
15+
716
## [2.4.0] - 2022-04-30
817

918
### Added

resources/views/components/pagination.blade.php

Lines changed: 2 additions & 2 deletions
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/tools/toolbar.blade.php

Lines changed: 14 additions & 14 deletions
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

Lines changed: 6 additions & 8 deletions
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
/**

src/Traits/Helpers/ComponentHelpers.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,34 @@ 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
370370
{
371-
return $this->configurableAreas[$area] ?? null;
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 {
386+
$area = $this->configurableAreas[$area] ?? null;
387+
388+
if (is_array($area) && isset($area[1]) && is_array($area[1])) {
389+
return $area[1];
390+
}
391+
392+
return [];
372393
}
373394

374395
/**

tests/Traits/Helpers/ComponentHelpersTest.php

Lines changed: 22 additions & 0 deletions
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)