Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev committed Dec 27, 2023
1 parent 7b1d0c5 commit 8baf91e
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 200 deletions.
1 change: 1 addition & 0 deletions resources/views/components/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
:$checkboxAttribute
:$theme
:$setUp
:$tableName
:parentId="$this->getName()"
:columns="$this->visibleColumns"
:data="$this->getCachedData
Expand Down
48 changes: 5 additions & 43 deletions src/Livewire/LazyChild.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
use Illuminate\Support\{Collection, Collection as BaseCollection};
use Livewire\Component;
use Livewire\Mechanisms\ComponentRegistry;
use PowerComponents\LivewirePowerGrid\Traits\ToggleDetail;

class LazyChild extends Component
{
use ToggleDetail;

public bool $checkbox = false;

public bool $checkboxAll = false;
Expand Down Expand Up @@ -38,54 +41,13 @@ public function mount(): void
$this->resolveDetailRow($this->data);
}

private function resolveDetailRow(mixed $results): void
{
if (!isset($this->setUp['detail'])) {
return;
}

$collection = $results;

if (!$results instanceof BaseCollection) {
/** @phpstan-ignore-next-line */
$collection = collect($results->items());
}

/** @phpstan-ignore-next-line */
$collection->each(function ($model) {
$id = strval($model->{$this->primaryKey});

data_set($this->setUp, 'detail', (array) $this->setUp['detail']);

$state = data_get($this->setUp, 'detail.state.' . $id, false);

data_set($this->setUp, 'detail.state.' . $id, $state);
});
}

public function toggleDetail(string $id): void
public function afterToggleDetail(string $id, string $state): void
{
$detailStates = (array) data_get($this->setUp, 'detail.state');

if (boolval(data_get($this->setUp, 'detail.collapseOthers'))) {
/** @var \Illuminate\Support\Enumerable<(int|string), (int|string)> $except */
$except = $id;
collect($detailStates)->except($except)
->filter(fn ($state) => $state)->keys()
->each(
fn ($key) => data_set($this->setUp, "detail.state.$key", false)
);
}

data_set($this->setUp, "detail.state.$id", !boolval(data_get($this->setUp, "detail.state.$id")));

$state = boolval(data_get($this->setUp, "detail.state.$id"));

$parentComponent = app(ComponentRegistry::class)->getClass($this->parentId);

$dispatchAfterToggleDetail = (array) data_get($this->setUp, 'lazy.dispatchAfterToggleDetail');

$this->dispatch($dispatchAfterToggleDetail, id: $id, state: $state)->to($parentComponent);
$this->dispatch($dispatchAfterToggleDetail, id: $id, state: $state ? 'true' : 'false')->to($parentComponent);
}

public function render(): View
Expand Down
Loading

0 comments on commit 8baf91e

Please sign in to comment.