Skip to content

Commit

Permalink
Merge pull request #755 from rappasoft/develop
Browse files Browse the repository at this point in the history
v2.5
  • Loading branch information
rappasoft authored May 3, 2022
2 parents 489041c + ab019a9 commit 04e0177
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 33 deletions.
21 changes: 20 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ All notable changes to `laravel-livewire-tables` will be documented in this file

## [Unreleased]

## [2.5.0] - 2022-05-03

### Added

- Ability to pass mount parameters to configurable areas

### Changed

- Move configure call to boot instead of booteed.
- Mount methods now available in `configure()` method.
- Non-field columns with a searchable callback are now included in the search query.
- Fixed debug query output duplicating select statements.
- Fixed header issue on column hide - https://github.com/rappasoft/laravel-livewire-tables/pull/754

### Removed

- Calls to set builder and columns in render as it doesn't seem to make a difference since it's also called in booted.

## [2.4.0] - 2022-04-30

### Added
Expand Down Expand Up @@ -633,7 +651,8 @@ Ground Up Rebuild

- Initial release

[Unreleased]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.4.0...development
[Unreleased]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.5.0...development
[2.5.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.4.0...v2.5.0
[2.4.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.3.0...v2.4.0
[2.3.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.2.1...v2.3.0
[2.2.1]: https://github.com/rappasoft/laravel-livewire-tables/compare/v2.2.0...v2.2.1
Expand Down
17 changes: 17 additions & 0 deletions docs/datatable/configurable-areas.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ public function configure(): void
}
```

## Passing Parameters

You can pass parameters to configurable areas that come from your mount method or any other service by proving an array:

```php
public function configure(): void
{
$this->setConfigurableAreas([
'toolbar-left-start' => [
'path.to.my.view', [
'param1' => $this->user_id,
'param2' => resolve(MyService::class)->getThing($this->user_id),
],
],
]);
}
```

## Example View

Expand Down
4 changes: 2 additions & 2 deletions resources/views/components/pagination.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@endphp

@if ($component->hasConfigurableAreaFor('before-pagination'))
@include($component->getConfigurableAreaFor('before-pagination'))
@include($component->getConfigurableAreaFor('before-pagination'), $component->getParametersForConfigurableArea('before-pagination'))
@endif

@if ($theme === 'tailwind')
Expand Down Expand Up @@ -102,5 +102,5 @@
@endif

@if ($component->hasConfigurableAreaFor('after-pagination'))
@include($component->getConfigurableAreaFor('after-pagination'))
@include($component->getConfigurableAreaFor('after-pagination'), $component->getParametersForConfigurableArea('after-pagination'))
@endif
2 changes: 1 addition & 1 deletion resources/views/components/table/th.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{{
$attributes->merge($customSortButtonAttributes)
->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])
->except('default')
->except(['default', 'wire:key'])
}}
>
<span>{{ $column->getTitle() }}</span>
Expand Down
28 changes: 14 additions & 14 deletions resources/views/components/tools/toolbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
@endphp

@if ($component->hasConfigurableAreaFor('before-toolbar'))
@include($component->getConfigurableAreaFor('before-toolbar'))
@include($component->getConfigurableAreaFor('before-toolbar'), $component->getParametersForConfigurableArea('before-toolbar'))
@endif

@if ($theme === 'tailwind')
<div class="md:flex md:justify-between mb-4 px-4 md:p-0">
<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">
@if ($component->hasConfigurableAreaFor('toolbar-left-start'))
@include($component->getConfigurableAreaFor('toolbar-left-start'))
@include($component->getConfigurableAreaFor('toolbar-left-start'), $component->getParametersForConfigurableArea('toolbar-left-start'))
@endif

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

@if ($component->hasConfigurableAreaFor('toolbar-left-end'))
@include($component->getConfigurableAreaFor('toolbar-left-end'))
@include($component->getConfigurableAreaFor('toolbar-left-end'), $component->getParametersForConfigurableArea('toolbar-left-end'))
@endif
</div>

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

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

@if ($component->hasConfigurableAreaFor('toolbar-right-end'))
@include($component->getConfigurableAreaFor('toolbar-right-end'))
@include($component->getConfigurableAreaFor('toolbar-right-end'), $component->getParametersForConfigurableArea('toolbar-right-end'))
@endif
</div>
</div>
Expand Down Expand Up @@ -321,7 +321,7 @@ class="block text-sm font-medium leading-5 text-gray-700 dark:text-white">
<div class="d-md-flex justify-content-between mb-3">
<div class="d-md-flex">
@if ($component->hasConfigurableAreaFor('toolbar-left-start'))
@include($component->getConfigurableAreaFor('toolbar-left-start'))
@include($component->getConfigurableAreaFor('toolbar-left-start'), $component->getParametersForConfigurableArea('toolbar-left-start'))
@endif

@if ($component->reorderIsEnabled())
Expand Down Expand Up @@ -435,13 +435,13 @@ class="dropdown-item btn text-center"
@endif

@if ($component->hasConfigurableAreaFor('toolbar-left-end'))
@include($component->getConfigurableAreaFor('toolbar-left-end'))
@include($component->getConfigurableAreaFor('toolbar-left-end'), $component->getParametersForConfigurableArea('toolbar-left-end'))
@endif
</div>

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

@if ($component->showBulkActionsDropdown())
Expand Down Expand Up @@ -532,7 +532,7 @@ class="form-control"
@endif

@if ($component->hasConfigurableAreaFor('toolbar-right-end'))
@include($component->getConfigurableAreaFor('toolbar-right-end'))
@include($component->getConfigurableAreaFor('toolbar-right-end'), $component->getParametersForConfigurableArea('toolbar-right-end'))
@endif
</div>
</div>
Expand Down Expand Up @@ -562,7 +562,7 @@ class="d-block">
<div class="d-md-flex justify-content-between mb-3">
<div class="d-md-flex">
@if ($component->hasConfigurableAreaFor('toolbar-left-start'))
@include($component->getConfigurableAreaFor('toolbar-left-start'))
@include($component->getConfigurableAreaFor('toolbar-left-start'), $component->getParametersForConfigurableArea('toolbar-left-start'))
@endif

@if ($component->reorderIsEnabled())
Expand Down Expand Up @@ -674,13 +674,13 @@ class="dropdown-item text-center"
@endif

@if ($component->hasConfigurableAreaFor('toolbar-left-end'))
@include($component->getConfigurableAreaFor('toolbar-left-end'))
@include($component->getConfigurableAreaFor('toolbar-left-end'), $component->getParametersForConfigurableArea('toolbar-left-end'))
@endif
</div>

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

@if ($component->showBulkActionsDropdown())
Expand Down Expand Up @@ -771,7 +771,7 @@ class="form-control"
@endif

@if ($component->hasConfigurableAreaFor('toolbar-right-end'))
@include($component->getConfigurableAreaFor('toolbar-right-end'))
@include($component->getConfigurableAreaFor('toolbar-right-end'), $component->getParametersForConfigurableArea('toolbar-righ-end'))
@endif
</div>
</div>
Expand Down Expand Up @@ -800,5 +800,5 @@ class="d-block">
@endif

@if ($component->hasConfigurableAreaFor('after-toolbar'))
@include($component->getConfigurableAreaFor('after-toolbar'))
@include($component->getConfigurableAreaFor('after-toolbar'), $component->getParametersForConfigurableArea('after-toolbar'))
@endif
16 changes: 6 additions & 10 deletions src/DataTableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,22 @@ public function boot(): void

// Set the filter defaults based on the filter type
$this->setFilterDefaults();

// Call the child configuration, if any
$this->configure();

// Make sure a primary key is set
if (! $this->hasPrimaryKey()) {
throw new DataTableConfigurationException('You must set a primary key using setPrimaryKey in the configure method.');
}
}

/**
* Runs on every request, after the component is mounted or hydrated, but before any update methods are called
*/
public function booted(): void
{
$this->configure();
$this->setTheme();
$this->setBuilder($this->builder());
$this->setColumns();

// Make sure a primary key is set
if (! $this->hasPrimaryKey()) {
throw new DataTableConfigurationException('You must set a primary key using setPrimaryKey in the configure method.');
}
}

/**
Expand Down Expand Up @@ -118,8 +116,6 @@ public function customView(): string
*/
public function render()
{
$this->setBuilder($this->builder());
$this->setColumns();
$this->setupColumnSelect();
$this->setupPagination();
$this->setupSecondaryHeader();
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/Helpers/ColumnHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getSelectableColumns(): Collection
public function getSearchableColumns(): Collection
{
return $this->getColumns()
->filter(fn (Column $column) => $column->isSearchable())
->filter(fn (Column $column) => $column->isSearchable() || $column->hasSearchCallback())
->values();
}

Expand Down
28 changes: 25 additions & 3 deletions src/Traits/Helpers/ComponentHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,35 @@ public function hasConfigurableAreaFor(string $area): bool
}

/**
* @param string $area
* @param string|array $area
*
* @return string|null
*/
public function getConfigurableAreaFor(string $area): ?string
public function getConfigurableAreaFor($area): ?string
{
$area = $this->configurableAreas[$area] ?? null;

if (is_array($area)) {
return $area[0];
}

return $area;
}

/**
* @param string|array $area
*
* @return array
*/
public function getParametersForConfigurableArea($area): array
{
return $this->configurableAreas[$area] ?? null;
$area = $this->configurableAreas[$area] ?? null;

if (is_array($area) && isset($area[1]) && is_array($area[1])) {
return $area[1];
}

return [];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/WithData.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,6 @@ protected function getTableForColumn(Column $column): ?string

protected function getQuerySql(): string
{
return $this->baseQuery()->toSql();
return (clone $this->getBuilder())->toSql();
}
}
22 changes: 22 additions & 0 deletions tests/Traits/Helpers/ComponentHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,28 @@ public function can_get_configurable_areas(): void
]);

$this->assertEquals('includes.areas.toolbar-left-start', $this->basicTable->getConfigurableAreaFor('toolbar-left-start'));

$this->basicTable->setConfigurableAreas([
'toolbar-left-start' => ['includes.areas.toolbar-left-start', ['param1' => 'hello']],
]);

$this->assertEquals('includes.areas.toolbar-left-start', $this->basicTable->getConfigurableAreaFor('toolbar-left-start'));
}

/** @test */
public function can_get_configurable_area_parameters(): void
{
$this->basicTable->setConfigurableAreas([
'toolbar-left-start' => 'includes.areas.toolbar-left-start',
]);

$this->assertEquals([], $this->basicTable->getParametersForConfigurableArea('toolbar-left-start'));

$this->basicTable->setConfigurableAreas([
'toolbar-left-start' => ['includes.areas.toolbar-left-start', ['param1' => 'hello']],
]);

$this->assertEquals(['param1' => 'hello'], $this->basicTable->getParametersForConfigurableArea('toolbar-left-start'));
}

/** @test */
Expand Down

0 comments on commit 04e0177

Please sign in to comment.