Skip to content

Commit

Permalink
refactor inline repeater options to use traits
Browse files Browse the repository at this point in the history
* `disableSortable()` -> `disableReorder` (existing trait)
* `disableActions()` uses new trait
  • Loading branch information
13twelve committed Jan 14, 2025
1 parent 23a2fe2 commit 757f7ff
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 46 deletions.
16 changes: 8 additions & 8 deletions frontend/js/components/Repeater.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
ref="blockList"
:block="block"
:index="index"
:withHandle="allowSortable && draggable"
:withActions="allowActions"
:withHandle="reorder && draggable"
:withActions="displayActions"
:size="blockSize"
:opened="opened"
>
Expand Down Expand Up @@ -111,19 +111,19 @@
type: Boolean,
default: true
},
allowSortable: {
type: Boolean,
default: true
},
allowActions: {
displayActions: {
type: Boolean,
default: true
},
max: {
type: [Number, null],
required: false,
default: null
}
},
reorder: {
type: Boolean,
default: true
},
},
data: function () {
return {
Expand Down
18 changes: 2 additions & 16 deletions src/Services/Forms/Fields/Repeater.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

use A17\Twill\Services\Forms\Fields\Traits\CanReorder;
use A17\Twill\Services\Forms\Fields\Traits\HasMax;
use A17\Twill\Services\Forms\Fields\Traits\DisableActions;

class Repeater extends BaseFormField
{
use HasMax;
use CanReorder;
use DisableActions;

protected ?string $type = null;
protected bool $buttonAsLink = false;
protected bool $allowCreate = true;
protected bool $allowSortable = true;
protected bool $allowActions = true;
protected ?string $relation = null;
protected ?array $browserModule = null;

Expand Down Expand Up @@ -65,20 +65,6 @@ public function allowCreate(bool $allowCreate = true): static
return $this;
}

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

return $this;
}

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

return $this;
}

public function browserModule(?array $browserModule = null): static
{
$this->browserModule = $browserModule;
Expand Down
18 changes: 18 additions & 0 deletions src/Services/Forms/Fields/Traits/DisableActions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace A17\Twill\Services\Forms\Fields\Traits;

trait DisableActions
{
protected bool $actions = true;

/**
* Adds a border around the options.
*/
public function disableActions(bool $actions = true): static
{
$this->displayActions = !$actions;

return $this;
}
}
25 changes: 7 additions & 18 deletions src/Services/Forms/InlineRepeater.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use A17\Twill\Services\Forms\Fields\Repeater;
use A17\Twill\Services\Forms\Traits\HasSubFields;
use A17\Twill\Services\Forms\Traits\RenderForBlocks;
use A17\Twill\Services\Forms\Fields\Traits\CanReorder;
use A17\Twill\Services\Forms\Fields\Traits\DisableActions;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
Expand All @@ -17,6 +19,8 @@ class InlineRepeater implements CanHaveSubfields, CanRenderForBlocks
{
use RenderForBlocks;
use HasSubFields;
use CanReorder;
use DisableActions;

protected function __construct(
private ?string $name = null,
Expand All @@ -25,15 +29,14 @@ protected function __construct(
private ?Collection $fields = null,
private ?string $label = null,
private bool $allowCreate = true,
private bool $allowSortable = true,
private bool $allowActions = true,
private ?string $relation = null,
private ?bool $allowBrowse = false,
private ?array $browser = null,
private ?int $max = null,
private ?string $titleField = null,
private ?bool $hideTitlePrefix = false,
private ?bool $buttonAsLink = false,
private ?bool $displayActions = true,
protected ?array $connectedTo = null,
) {
}
Expand Down Expand Up @@ -97,20 +100,6 @@ public function disableCreate(bool $disableCreate = true): static
return $this;
}

public function disableSortable(bool $disableSortable = true): static
{
$this->allowSortable = ! $disableSortable;

return $this;
}

public function disableActions(bool $disableActions = true): static
{
$this->allowActions = ! $disableActions;

return $this;
}

/**
* The name of the module to use for selecting existing records. Not for json repeaters.
*/
Expand Down Expand Up @@ -223,8 +212,8 @@ public function render(): View
->name($this->name)
->type($this->getRenderName())
->allowCreate($this->allowCreate)
->allowSortable($this->allowSortable)
->allowActions($this->allowActions)
->disableReorder(!$this->reorder)
->disableActions(!$this->displayActions)
->relation($this->relation ?? null)
->browserModule($this->allowBrowse ? $this->browser : null);

Expand Down
3 changes: 1 addition & 2 deletions src/View/Components/Fields/Repeater.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ public function __construct(
public string $type,
public bool $buttonAsLink = false,
public bool $reorder = true,
public bool $displayActions = true,
public ?int $max = null,
public bool $allowCreate = true,
public bool $allowSortable = true,
public bool $allowActions = true,
public ?string $relation = null,
public ?array $browserModule = null,
// Generic
Expand Down
3 changes: 1 addition & 2 deletions views/partials/form/_repeater.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
@if ($buttonAsLink) :button-as-link="true" @endif
@if ($relation) relation="{{$relation}}" @endif
:allow-create="{{$allowCreate ? 'true' : 'false'}}"
:allow-sortable="{{$allowSortable ? 'true' : 'false'}}"
:allow-actions="{{$allowActions ? 'true' : 'false' }}"
:display-actions="{{$displayActions ? 'true' : 'false'}}"
></a17-repeater>

@unless($renderForBlocks)
Expand Down

0 comments on commit 757f7ff

Please sign in to comment.