Skip to content

Commit

Permalink
Merge pull request #1249 from lee-to/table-modifiers
Browse files Browse the repository at this point in the history
feat: Table modifiers
  • Loading branch information
lee-to authored Sep 5, 2024
2 parents 8b2a476 + 57b675e commit 5409fdc
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 30 deletions.
51 changes: 35 additions & 16 deletions resources/views/components/table/builder.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
'searchable' => false,
'searchValue' => '',
'name' => 'default',
'sticky' => false
'sticky' => false,
'thead' => null,
'tbody' => null,
'bodyBefore' => null,
'bodyAfter' => null,
'tfoot' => null,
])

<div x-data="tableBuilder(
Expand Down Expand Up @@ -89,18 +94,26 @@
>
@if(!$vertical)
<x-slot:thead>
<x-moonshine::table.head
:rows="$rows"
:fields="$fields"
:actions="$bulkButtons"
:asyncUrl="$asyncUrl"
:preview="$preview"
/>
@if($thead ?? false)
{!! $thead !!}
@else
<x-moonshine::table.head
:rows="$rows"
:fields="$fields"
:actions="$bulkButtons"
:asyncUrl="$asyncUrl"
:preview="$preview"
/>
@endif
</x-slot:thead>
@endif

@if($rows->isNotEmpty())
<x-slot:tbody>
{!! $bodyBefore ?? '' !!}
@if($tbody ?? false)
{!! $tbody !!}
@else
<x-moonshine::table.body
:rows="$rows"
:vertical="$vertical"
Expand All @@ -111,18 +124,24 @@
:hasActions="$bulkButtons->isNotEmpty()"
:has-click-action="$attributes->get('data-click-action') !== null"
/>
@endif
{!! $bodyAfter ?? '' !!}
</x-slot:tbody>
@endif

@if(!$preview)
<x-slot:tfoot
::class="actionsOpen ? 'translate-y-none ease-out' : '-translate-y-full ease-in hidden'"
>
<x-moonshine::table.foot
:rows="$rows"
:actions="$bulkButtons"
/>
</x-slot:tfoot>
<x-slot:tfoot
::class="actionsOpen ? 'translate-y-none ease-out' : '-translate-y-full ease-in hidden'"
>
@if($tfoot ?? false)
{!! $tfoot !!}
@else
<x-moonshine::table.foot
:rows="$rows"
:actions="$bulkButtons"
/>
@endif
</x-slot:tfoot>
@endif
</x-moonshine::table>

Expand Down
75 changes: 72 additions & 3 deletions src/Components/TableBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\View\ComponentAttributeBag;
use MoonShine\ActionButtons\ActionButtons;
use MoonShine\Contracts\Table\TableContract;
use MoonShine\Enums\JsEvent;
use MoonShine\Fields\Fields;
Expand Down Expand Up @@ -37,6 +38,16 @@ final class TableBuilder extends IterableComponent implements TableContract

protected ?Closure $tdAttributes = null;

protected ?Closure $thead = null;

protected ?Closure $tbody = null;

protected ?Closure $bodyBefore = null;

protected ?Closure $bodyAfter = null;

protected ?Closure $tfoot = null;

public function __construct(
Fields|array $fields = [],
Paginator|iterable $items = [],
Expand Down Expand Up @@ -207,23 +218,81 @@ function (mixed $data, int $index, ComponentAttributeBag $attr, TableRow $row) u
return $this;
}

/**
* @param Closure(Fields $fields, self $ctx): string $thead
*/
public function thead(Closure $thead): self
{
$this->thead = $thead;

return $this;
}

/**
* @param Closure(Collection $rows, self $ctx): string $tbody
*/
public function tbody(Closure $tbody): self
{
$this->tbody = $tbody;

return $this;
}

/**
* @param Closure(ActionButtons $bulkButtons, self $ctx): string $tfoot
*/
public function tfoot(Closure $tfoot): self
{
$this->tfoot = $tfoot;

return $this;
}

/**
* @param Closure(Collection $rows, self $ctx): string $before
*/
public function bodyBefore(Closure $before): self
{
$this->bodyBefore = $before;

return $this;
}

/**
* @param Closure(Collection $rows, self $ctx): string $after
*/
public function bodyAfter(Closure $after): self
{
$this->bodyAfter = $after;

return $this;
}

/**
* @return array<string, mixed>
* @throws Throwable
*/
protected function viewData(): array
{
$this->performBeforeRender();
$fields = $this->preparedFields()->onlyVisible();
$rows = $this->rows();
$bulkButtons = $this->getBulkButtons();

return [
'rows' => $this->rows(),
'fields' => $this->preparedFields()->onlyVisible(),
'rows' => $rows,
'fields' => $fields,
'thead' => value($this->thead, $fields, $this),
'tbody' => value($this->tbody, $rows, $this),
'bodyBefore' => value($this->bodyBefore, $rows, $this),
'bodyAfter' => value($this->bodyAfter, $rows, $this),
'tfoot' => value($this->tfoot, $bulkButtons, $this),
'name' => $this->getName(),
'hasPaginator' => $this->hasPaginator(),
'simple' => $this->isSimple(),
'simplePaginate' => ! $this->getPaginator() instanceof LengthAwarePaginator,
'paginator' => $this->getPaginator(),
'bulkButtons' => $this->getBulkButtons(),
'bulkButtons' => $bulkButtons,
'async' => $this->isAsync(),
'asyncUrl' => $this->asyncUrl(),
'createButton' => $this->creatableButton,
Expand Down
30 changes: 30 additions & 0 deletions src/Pages/Crud/IndexPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,36 @@ protected function itemsComponent(iterable $items, Fields $fields): MoonShineRen
$this->getResource()->tdAttributes()
)
)
->when(
! is_null($this->getResource()->thead()),
fn (TableBuilder $table): TableBuilder => $table->thead(
$this->getResource()->thead()
)
)
->when(
! is_null($this->getResource()->tbody()),
fn (TableBuilder $table): TableBuilder => $table->tbody(
$this->getResource()->tbody()
)
)
->when(
! is_null($this->getResource()->tfoot()),
fn (TableBuilder $table): TableBuilder => $table->tfoot(
$this->getResource()->tfoot()
)
)
->when(
! is_null($this->getResource()->tbodyBefore()),
fn (TableBuilder $table): TableBuilder => $table->bodyBefore(
$this->getResource()->tbodyBefore()
)
)
->when(
! is_null($this->getResource()->tbodyAfter()),
fn (TableBuilder $table): TableBuilder => $table->bodyAfter(
$this->getResource()->tbodyAfter()
)
)
->buttons($this->getResource()->getIndexItemButtons())
->customAttributes([
'data-click-action' => $this->getResource()->getClickAction(),
Expand Down
14 changes: 3 additions & 11 deletions src/Resources/ModelResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\QueryException;
use Illuminate\View\ComponentAttributeBag;
use MoonShine\ActionButtons\ActionButtons;
use MoonShine\Enums\ClickAction;
use MoonShine\Enums\JsEvent;
use MoonShine\Exceptions\ResourceException;
Expand All @@ -29,6 +30,7 @@
use MoonShine\Traits\Resource\ResourceModelValidation;
use MoonShine\Traits\Resource\ResourceWithButtons;
use MoonShine\Traits\Resource\ResourceWithFields;
use MoonShine\Traits\Resource\ResourceWithTableModifiers;
use MoonShine\Traits\WithIsNowOnRoute;
use MoonShine\TypeCasts\ModelCast;
use Throwable;
Expand All @@ -40,6 +42,7 @@ abstract class ModelResource extends Resource
{
use ResourceWithFields;
use ResourceWithButtons;
use ResourceWithTableModifiers;

/** @use ResourceModelValidation<TModel> */
use ResourceModelValidation;
Expand Down Expand Up @@ -191,7 +194,6 @@ public function hasErrorsAbove(): bool
return $this->errorsAbove;
}


/**
* @return list<Metric>
*/
Expand All @@ -200,16 +202,6 @@ public function metrics(): array
return [];
}

public function trAttributes(): Closure
{
return fn (mixed $data, int $row, ComponentAttributeBag $attr): ComponentAttributeBag => $attr;
}

public function tdAttributes(): Closure
{
return fn (mixed $data, int $row, int $cell, ComponentAttributeBag $attr): ComponentAttributeBag => $attr;
}

/**
* @return string[]
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Table/TableRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use MoonShine\Traits\Makeable;
use Throwable;

/**
* @method static static make(mixed $data, Fields $fields, ActionButtons $actions, ?Closure $trAttributes = null, ?Closure $tdAttributes = null, ?Closure $systemTrAttributes = null)
*/
final class TableRow implements MoonShineRenderable
{
use Makeable;
Expand Down
67 changes: 67 additions & 0 deletions src/Traits/Resource/ResourceWithTableModifiers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace MoonShine\Traits\Resource;

use Closure;
use Illuminate\Support\Collection;
use Illuminate\View\ComponentAttributeBag;
use MoonShine\ActionButtons\ActionButtons;
use MoonShine\Components\TableBuilder;
use MoonShine\Fields\Fields;

trait ResourceWithTableModifiers
{

public function trAttributes(): Closure
{
return fn (mixed $data, int $row, ComponentAttributeBag $attr): ComponentAttributeBag => $attr;
}

public function tdAttributes(): Closure
{
return fn (mixed $data, int $row, int $cell, ComponentAttributeBag $attr): ComponentAttributeBag => $attr;
}

/**
* @return ?Closure(Fields $fields, TableBuilder $ctx): string
*/
public function thead(): ?Closure
{
return null;
}

/**
* @return ?Closure(Collection $rows, TableBuilder $ctx): string
*/
public function tbody(): ?Closure
{
return null;
}

/**
* @return ?Closure(ActionButtons $bulkButtons, TableBuilder $ctx): string
*/
public function tfoot(): ?Closure
{
return null;
}

/**
* @return ?Closure(Collection $rows, TableBuilder $ctx): string
*/
public function tbodyBefore(): ?Closure
{
return null;
}

/**
* @return ?Closure(Collection $rows, TableBuilder $ctx): string
*/
public function tbodyAfter(): ?Closure
{
return null;
}

}

0 comments on commit 5409fdc

Please sign in to comment.