Skip to content

Commit

Permalink
refactor: DynamicComponents
Browse files Browse the repository at this point in the history
Removed dynamic-components
  • Loading branch information
lee-to committed Dec 14, 2023
1 parent 9a54c12 commit 20b04d7
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 22 deletions.
20 changes: 15 additions & 5 deletions resources/views/actions/default.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,24 @@

</x-moonshine::modal>
@else
<x-dynamic-component
@if($action->inDropdown())
<x-moonshine::link-native
:attributes="$attributes"
:component="'moonshine::link-' . ($action->inDropdown() ? 'native' : 'button')"
@class(['p-2' => $action->inDropdown()])
:href="$action->url()"
:icon="$action->iconValue()"
>
{{ $action->label() }}
</x-dynamic-component>
>
{{ $action->label() }}
</x-moonshine::link-native>
@else
<x-moonshine::link-button
:attributes="$attributes"
@class(['p-2' => $action->inDropdown()])
:href="$action->url()"
:icon="$action->iconValue()"
>
{{ $action->label() }}
</x-moonshine::link-button>
@endif
@endif

5 changes: 1 addition & 4 deletions resources/views/components/form/input-extensions.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
{{ $slot }}

@foreach($extensions as $extension)
<x-dynamic-component
:component="$extension->getView()"
:extension="$extension"
/>
{{ $extension->render() }}
@endforeach
</div>
@else
Expand Down
18 changes: 13 additions & 5 deletions resources/views/components/menu/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@
@endif
>
@foreach($data as $item)
<x-dynamic-component
component="moonshine::menu.{{ $item->isGroup() ? 'group' : 'item' }}"
:item="$item"
:top="$top"
/>
@if($item->isGroup())
<x-moonshine::menu.group
:item="$item"
:top="$top"
/>
@else
<x-moonshine::menu.item
:item="$item"
:top="$top"
/>
@endif


@endforeach
</ul>
@endif
Expand Down
5 changes: 4 additions & 1 deletion src/Components/TableBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ public function getItems(): Collection
return collect($this->items);
}

/**
* @throws Throwable
*/
public function rows(): Collection
{
$tableFields = $this->getFields()->onlyFields();

return $this->getItems()->filter()->map(function (mixed $data, $index) use ($tableFields): TableRow {
return $this->getItems()->filter()->map(function (mixed $data, int $index) use ($tableFields): TableRow {
$casted = $this->castData($data);
$raw = $this->unCastData($data);

Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Fields/HasFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function fields(Fields|Closure|array $fields): static;

public function hasFields(): bool;

public function getFields(mixed $data = null): Fields;
public function getFields(): Fields;

public function preparedFields(): Fields;
}
2 changes: 1 addition & 1 deletion src/Decorations/Tabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static function (Fields $tabs): void {
);
}

public function getFields(mixed $data = null): Fields
public function getFields(): Fields
{
return $this->tabs();
}
Expand Down
15 changes: 14 additions & 1 deletion src/Fields/FormElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,32 @@
*/
abstract class FormElements extends MoonShineRenderElements
{
protected bool $onlyFieldsCalled = false;

public function onlyFieldsCalled(): static
{
$this->onlyFieldsCalled = true;

return $this;
}

/**
* @throws Throwable
*/
public function onlyFields(bool $withWrappers = false): Fields
{
if($this->onlyFieldsCalled) {
return Fields::make($this->toArray());
}

$data = [];

$this->extractFields($this->toArray(), $data);

return Fields::make($data)->when(
! $withWrappers,
fn (Fields $fields): Fields|FormElements => $fields->withoutWrappers()
);
)->onlyFieldsCalled();
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/InputExtensions/InputExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MoonShine\InputExtensions;

use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use MoonShine\Traits\WithComponentAttributes;
use MoonShine\Traits\WithView;
Expand Down Expand Up @@ -35,4 +36,19 @@ public function xInit(): Collection
{
return collect($this->xInit);
}

public function render(): View
{
$view = str_contains('components.', $this->getView())
? $this->getView()
: str_replace(
'moonshine::',
'moonshine::components.',
$this->getView()
);

return view($view, [
'extension' => $this,
]);
}
}
9 changes: 7 additions & 2 deletions src/Traits/Resource/ResourceModelQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public function queryTags(): array
return [];
}

protected function itemsPerPage(): int
{
return $this->itemsPerPage;
}

/**
* @throws Throwable
*/
Expand All @@ -141,10 +146,10 @@ public function paginate(): Paginator
->when(
$this->isSimplePaginate(),
fn (Builder $query): Paginator => $query->simplePaginate(
$this->itemsPerPage
$this->itemsPerPage()
),
fn (Builder $query): LengthAwarePaginator => $query->paginate(
$this->itemsPerPage
$this->itemsPerPage()
),
)
->appends(request()->except('page'));
Expand Down
7 changes: 5 additions & 2 deletions src/Traits/WithFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ public function preparedFields(): Fields
/**
* @throws Throwable
*/
public function getFields(mixed $data = null): Fields
public function getFields(): Fields
{
return Fields::make($this->fields);
}

/**
* @throws Throwable
*/
public function hasFields(): bool
{
return $this->getFields()->isNotEmpty();
Expand All @@ -40,7 +43,7 @@ public function hasFields(): bool
public function fields(Fields|Closure|array $fields): static
{
if(is_closure($fields)) {
$fields = call_user_func($fields);
$fields = $fields();
}

$this->fields = $fields instanceof Fields
Expand Down
33 changes: 33 additions & 0 deletions tests/Unit/Components/TableBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use MoonShine\Components\TableBuilder;
use MoonShine\Fields\ID;
use MoonShine\Fields\Text;
use MoonShine\Tests\Fixtures\Models\Item;
use MoonShine\TypeCasts\ModelCast;

uses()->group('table-builder');

beforeEach(function () {
$this->items = Item::factory(10)->create();

$this->builder = TableBuilder::make()
->fields([
ID::make(),
Text::make('Name')
])
->items(Item::query()->get())
->cast(ModelCast::make(Item::class))
;
});

it('without duplicates', function () {
expect((string) $this->builder->render())
->toContain(
$this->items[0]->name,
$this->items[1]->name,
$this->items[2]->name,
$this->items[3]->name,
)
;
});
File renamed without changes.

0 comments on commit 20b04d7

Please sign in to comment.