Skip to content

Commit

Permalink
Add primaryKeyAlias property to solve primarykeys with dot notation (
Browse files Browse the repository at this point in the history
…#1621)

* Add realPrimaryKey property

* Add realPrimaryKey property

* Disable L10 - larastan

---------

Co-authored-by: luanfreitasdev <[email protected]>
  • Loading branch information
luanfreitasdev and luanfreitasdev authored Jul 19, 2024
1 parent d7ac28c commit 0aefa68
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 38 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/code-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ 8.2, 8.1 ]
laravel: [ 11.*, 10.* ]
php: [ 8.2 ]
laravel: [ 11.* ]
exclude:
- php: 8.1
laravel: 11.*
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
fail-fast: false
matrix:
php: [ 8.2 ]
laravel: [ 11.*, 10.* ]
laravel: [ 11.* ]
dependency-version: [ prefer-stable ]

name: PHP:${{ matrix.php }} / L:${{ matrix.laravel }}
Expand Down
6 changes: 3 additions & 3 deletions resources/views/components/editable.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
$params = [
'theme' => data_get($theme, 'name'),
'tableName' => $tableName,
'id' => data_get($row, $primaryKey),
'id' => data_get($row, $this->realPrimaryKey),
'dataField' => $field,
'content' => $content,
'fallback' => $fallback,
Expand Down Expand Up @@ -74,9 +74,9 @@
<div x-html="editableInput"></div>
</template>
@if ($showErrorBag)
@error($field . '.' . $row->{$primaryKey})
@error($field . '.' . $row->{$this->realPrimaryKey})
<div class="text-sm text-red-800 p-1 transition-all duration-200">
{{ str($message)->replace($field . '.' . $row->{$primaryKey}, $field) }}
{{ str($message)->replace($field . '.' . $row->{$this->realPrimaryKey}, $field) }}
</div>
@enderror
@endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<x-livewire-powergrid::editable
:tableName="$tableName"
:primaryKey="$primaryKey"
:primaryKey="$this->realPrimaryKey"
:row="$row"
:field="$field"
:theme="$theme"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class="{{ data_get($theme, 'tdBodyClass') }}"
>
<div
class="cursor-pointer"
x-on:click.prevent="$wire.toggleDetail('{{ $row->{$primaryKey} }}')"
x-on:click.prevent="$wire.toggleDetail('{{ $row->{$this->realPrimaryKey} }}')"
>
@includeIf(data_get($setUp, 'detail.viewIcon'))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@php
$params = [
'id' => data_get($row, $primaryKey),
'id' => data_get($row, $this->realPrimaryKey),
'isHidden' => !$showToggleable,
'tableName' => $tableName,
'field' => $column->field,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<x-livewire-powergrid::editable
:tableName="$tableName"
:primaryKey="$primaryKey"
:primaryKey="$this->realPrimaryKey"
:row="$row"
:field="$field"
:theme="$theme"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class="{{ data_get($theme, 'tdBodyClass') }}"
>
<div
class="cursor-pointer"
x-on:click.prevent="$wire.toggleDetail('{{ $row->{$primaryKey} }}')"
x-on:click.prevent="$wire.toggleDetail('{{ $row->{$this->realPrimaryKey} }}')"
>
@includeIf(data_get($setUp, 'detail.viewIcon'))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
$falseValue = $column->toggleable['default'][1];
$params = [
'id' => data_get($row, $primaryKey),
'id' => data_get($row, $this->realPrimaryKey),
'isHidden' => !$showToggleable,
'tableName' => $tableName,
'field' => $column->field,
Expand Down
8 changes: 4 additions & 4 deletions resources/views/components/row.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<div class="pg-actions">
@if(empty(data_get($row, 'actions')) && $column->isAction)
@if (method_exists($this, 'actionsFromView') && $actionsFromView = $this->actionsFromView($row))
<div wire:key="actions-view-{{ data_get($row, $primaryKey) }}">
<div wire:key="actions-view-{{ data_get($row, $this->realPrimaryKey) }}">
{!! $actionsFromView !!}
</div>
@endif
Expand All @@ -80,7 +80,7 @@
@if (filled(data_get($row, 'actions')) && $column->isAction)
@foreach (data_get($row, 'actions') as $key => $action)
@if(filled($action))
<span wire:key="action-{{ data_get($row, $primaryKey) }}-{{ $key }}">
<span wire:key="action-{{ data_get($row, $this->realPrimaryKey) }}-{{ $key }}">
{!! $action !!}
</span>
@endif
Expand All @@ -92,7 +92,7 @@
$hasFieldRules = $actionRulesClass->recoverActionForField($row, $field);
// =============* Edit On Click *=====================
$showEditOnClick = false;
if (data_get($column->editable, 'hasPermission')) {
Expand Down Expand Up @@ -125,7 +125,7 @@
$showEditOnClick = false;
}
@endphp

@if($showEditOnClick === true)
<span @class([$contentClassField, $contentClass])>
@include(data_get($theme, 'editable.view') ?? null, ['editable' => $column->editable])
Expand Down
4 changes: 2 additions & 2 deletions resources/views/components/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@php throw new Exception('To use checkboxes, you must define a unique key attribute in your data source.') @endphp
@endif
@php
$rowId = data_get($row, $primaryKey);
$rowId = data_get($row, $this->realPrimaryKey);
$class = data_get($theme, 'table.trBodyClass');
Expand Down Expand Up @@ -96,7 +96,7 @@
<livewire:lazy-child
key="{{ $this->getLazyKeys }}"
:child-index="$item"
:$primaryKey
:$this->realPrimaryKey
:$radio
:$radioAttribute
:$checkbox
Expand Down
8 changes: 4 additions & 4 deletions resources/views/components/table/detail.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@

<td colspan="999">
@if (filled($rulesValues['detailView']))
@includeWhen(data_get($setUp, 'detail.state.' . $row->{$primaryKey}),
@includeWhen(data_get($setUp, 'detail.state.' . $row->{$this->realPrimaryKey}),
$rulesValues['detailView'][0]['detailView'],
[
'id' => data_get($row, $primaryKey),
'id' => data_get($row, $this->realPrimaryKey),
'options' => array_merge(
data_get($setUp, 'detail.options'),
$rulesValues['detailView']['0']['options']),
]
)
@else
@includeWhen(data_get($setUp, 'detail.state.' . $row->{$primaryKey}),
@includeWhen(data_get($setUp, 'detail.state.' . $row->{$this->realPrimaryKey}),
data_get($setUp, 'detail.view'),
[
'id' => data_get($row, $primaryKey),
'id' => data_get($row, $this->realPrimaryKey),
'options' => data_get($setUp, 'detail.options'),
]
)
Expand Down
2 changes: 1 addition & 1 deletion resources/views/livewire/lazy-child.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<tbody>
@foreach ($data as $row)
@php
$rowId = data_get($row, $primaryKey);
$rowId = data_get($row, $this->realPrimaryKey);
$class = data_get($theme, 'table.trBodyClass');
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Actions/Macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static function boot(): void
"component" => "button",
"attribute" => "wire:click",
"value" => function ($component, $row) {
return 'toggleDetail(\'' . data_get($row, $component->primaryKey) . '\')';
return 'toggleDetail(\'' . data_get($row, $component->realPrimaryKey) . '\')';
},
];

Expand Down
9 changes: 9 additions & 0 deletions src/Concerns/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace PowerComponents\LivewirePowerGrid\Concerns;

use Livewire\Attributes\Computed;
use PowerComponents\LivewirePowerGrid\ProcessDataSource;

trait Base
{
public string $primaryKey = 'id';

public ?string $primaryKeyAlias = null;

public bool $ignoreTablePrefix = true;

public string $tableName = 'default';
Expand Down Expand Up @@ -44,6 +47,12 @@ trait Base

public int $totalCurrentPage = 0;

#[Computed]
public function realPrimaryKey(): string
{
return $this->primaryKeyAlias ?? $this->primaryKey;
}

public function template(): ?string
{
return null;
Expand Down
4 changes: 3 additions & 1 deletion src/Concerns/ToggleDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ private function resolveDetailRow(mixed $results): void
$collection = !is_array($results) && method_exists($results, 'items') ? collect($results->items()) : collect($results);
}

$collection->pluck($this->primaryKey)
$primaryKey = $this->primaryKeyAlias ?? $this->primaryKey;

$collection->pluck($primaryKey)
->map(fn ($id) => strval($id))
->each(function ($id) {
$state = data_get($this->setUp, "detail.state.$id", false);
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/PowerGridServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private function macros(): void

$perPage = $perPage ?: $this->model->getPerPage();

$results = $this->model->newCollection(
$results = $this->model->newCollection( //@phpstan-ignore-line
$engine->map(
$this,
$rawResults = $engine->paginate($this, $perPage, $page),
Expand Down
58 changes: 48 additions & 10 deletions tests/Feature/Buttons/ToggleDetailTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

use Illuminate\Database\Eloquent\Builder;
use PowerComponents\LivewirePowerGrid\Button;
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\DishTableBase;

use PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Dish;

use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;

$component = new class () extends DishTableBase {
Expand All @@ -16,17 +19,32 @@ public function actions($row): array
}
};

dataset('action:toggleDetail', [
'tailwind' => [$component::class, (object) ['theme' => 'tailwind', 'join' => false]],
'bootstrap' => [$component::class, (object) ['theme' => 'bootstrap', 'join' => false]],
'tailwind join' => [$component::class, (object) ['theme' => 'tailwind', 'join' => true]],
'bootstrap join' => [$component::class, (object) ['theme' => 'bootstrap', 'join' => true]],
]);
$dotNotationPrimaryKeyComponent = new class () extends DishTableBase {
public string $primaryKey = 'dishes.id';

public ?string $primaryKeyAlias = 'id';

public function dataSource(): Builder
{
return Dish::query()
->join('categories as newCategories', function ($categories) {
$categories->on('dishes.category_id', '=', 'newCategories.id');
})
->select('dishes.*', 'newCategories.name as category_name');
}

public function actions($row): array
{
return [
Button::make('toggleDetail')
->slot('toggleDetail: ' . $row->id)
->toggleDetail(),
];
}
};

it('properly displays "toggleDetail" on edit button', function (string $component, object $params) {
livewire($component, [
'join' => $params->join,
])
livewire($component)
->call($params->theme)
->set('setUp.footer.perPage', 6)
->assertSeeHtml("wire:click=\"toggleDetail(&#039;1&#039;)\">toggleDetail: 1</button>")
Expand All @@ -36,5 +54,25 @@ public function actions($row): array
->assertSeeHtml("wire:click=\"toggleDetail(&#039;7&#039;)\">toggleDetail: 7</button>")
->assertDontSeeHtml("wire:click=\"toggleDetail(&#039;1&#039;)\">toggleDetail: 1</button>");
})
->with('action:toggleDetail')
->with([
'tailwind' => [$component::class, (object) ['theme' => 'tailwind']],
'bootstrap' => [$component::class, (object) ['theme' => 'bootstrap']],
])
->group('action');

it('properly displays "toggleDetail" on edit button using dot notation in primaryKey', function (string $component, object $params) {
livewire($component)
->call($params->theme)
->set('setUp.footer.perPage', 6)
->assertSeeHtml("\"toggleDetail(&#039;1&#039;)\">toggleDetail: 1</button>")
->assertSeeHtml("\"toggleDetail(&#039;2&#039;)\">toggleDetail: 2</button>")
->assertDontSeeHtml("wire:click=\"toggleDetail(&#039;7&#039;)\">toggleDetail: 7</button>")
->call('setPage', 2)
->assertSeeHtml("wire:click=\"toggleDetail(&#039;7&#039;)\">toggleDetail: 7</button>")
->assertDontSeeHtml("wire:click=\"toggleDetail(&#039;1&#039;)\">toggleDetail: 1</button>");
})
->with([
'tailwind join primary key alias' => [$dotNotationPrimaryKeyComponent::class, (object) ['theme' => 'tailwind']],
'bootstrap join primary key alias' => [$dotNotationPrimaryKeyComponent::class, (object) ['theme' => 'bootstrap']],
])
->group('action');
4 changes: 2 additions & 2 deletions tests/Feature/DetailRowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
}

$component->assertSeeHtmlInOrder($xData);
})->skip();
});

it('collapse detail row with collapseOthers', function () {
livewire(RulesToggleDetailTable::class, [
Expand Down Expand Up @@ -184,4 +184,4 @@
4 => false,
5 => false,
]);
})->skip();
});

0 comments on commit 0aefa68

Please sign in to comment.