From 8b5bade3990e959c4e319e56979635e3cbc6339b Mon Sep 17 00:00:00 2001 From: Mike Byrne Date: Mon, 13 Jan 2025 13:56:06 +0000 Subject: [PATCH 1/7] add option to disable sorting of inline repeaters --- frontend/js/components/Repeater.vue | 6 +++++- src/Services/Forms/Fields/Repeater.php | 8 ++++++++ src/Services/Forms/InlineRepeater.php | 9 +++++++++ src/View/Components/Fields/Repeater.php | 1 + views/partials/form/_repeater.blade.php | 1 + 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/frontend/js/components/Repeater.vue b/frontend/js/components/Repeater.vue index 63355f90a..dd44decf0 100755 --- a/frontend/js/components/Repeater.vue +++ b/frontend/js/components/Repeater.vue @@ -7,7 +7,7 @@ ref="blockList" :block="block" :index="index" - :withHandle="draggable" + :withHandle="allowSortable && draggable" :size="blockSize" :opened="opened" > @@ -110,6 +110,10 @@ type: Boolean, default: true }, + allowSortable: { + type: Boolean, + default: true + }, max: { type: [Number, null], required: false, diff --git a/src/Services/Forms/Fields/Repeater.php b/src/Services/Forms/Fields/Repeater.php index 79c8c8373..5db9adf82 100644 --- a/src/Services/Forms/Fields/Repeater.php +++ b/src/Services/Forms/Fields/Repeater.php @@ -13,6 +13,7 @@ class Repeater extends BaseFormField protected ?string $type = null; protected bool $buttonAsLink = false; protected bool $allowCreate = true; + protected bool $allowSortable = true; protected ?string $relation = null; protected ?array $browserModule = null; @@ -63,6 +64,13 @@ public function allowCreate(bool $allowCreate = true): static return $this; } + public function allowSortable(bool $allowSortable = true): static + { + $this->allowSortable = $allowSortable; + + return $this; + } + public function browserModule(?array $browserModule = null): static { $this->browserModule = $browserModule; diff --git a/src/Services/Forms/InlineRepeater.php b/src/Services/Forms/InlineRepeater.php index 13358df89..c8136f56b 100644 --- a/src/Services/Forms/InlineRepeater.php +++ b/src/Services/Forms/InlineRepeater.php @@ -25,6 +25,7 @@ protected function __construct( private ?Collection $fields = null, private ?string $label = null, private bool $allowCreate = true, + private bool $allowSortable = true, private ?string $relation = null, private ?bool $allowBrowse = false, private ?array $browser = null, @@ -95,6 +96,13 @@ public function disableCreate(bool $disableCreate = true): static return $this; } + public function disableSortable(bool $disableSortable = true): static + { + $this->allowSortable = ! $disableSortable; + + return $this; + } + /** * The name of the module to use for selecting existing records. Not for json repeaters. */ @@ -207,6 +215,7 @@ public function render(): View ->name($this->name) ->type($this->getRenderName()) ->allowCreate($this->allowCreate) + ->allowSortable($this->allowSortable) ->relation($this->relation ?? null) ->browserModule($this->allowBrowse ? $this->browser : null); diff --git a/src/View/Components/Fields/Repeater.php b/src/View/Components/Fields/Repeater.php index 431e44498..59e03b152 100644 --- a/src/View/Components/Fields/Repeater.php +++ b/src/View/Components/Fields/Repeater.php @@ -13,6 +13,7 @@ public function __construct( public bool $reorder = true, public ?int $max = null, public bool $allowCreate = true, + public bool $allowSortable = true, public ?string $relation = null, public ?array $browserModule = null, // Generic diff --git a/views/partials/form/_repeater.blade.php b/views/partials/form/_repeater.blade.php index 6f62880e5..fd09204c8 100644 --- a/views/partials/form/_repeater.blade.php +++ b/views/partials/form/_repeater.blade.php @@ -7,6 +7,7 @@ @if ($buttonAsLink) :button-as-link="true" @endif @if ($relation) relation="{{$relation}}" @endif :allow-create="{{$allowCreate ? 'true' : 'false'}}" + :allow-sortable="{{$allowSortable ? 'true' : 'false'}}" > @unless($renderForBlocks) From 44e993e1a2813a006bcefc71158e9038f9accf2a Mon Sep 17 00:00:00 2001 From: Mike Byrne Date: Tue, 14 Jan 2025 18:01:46 +0000 Subject: [PATCH 2/7] hide add button on inline repeater if `allowCreate` is false --- frontend/js/components/Repeater.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/js/components/Repeater.vue b/frontend/js/components/Repeater.vue index dd44decf0..3fa5eaf89 100755 --- a/frontend/js/components/Repeater.vue +++ b/frontend/js/components/Repeater.vue @@ -12,7 +12,7 @@ :opened="opened" > + v-if="hasRemainingBlocks && allowCreate">
From 23a2fe21d03d9de5adb1575b22917b48a7d5a6ee Mon Sep 17 00:00:00 2001 From: Mike Byrne Date: Tue, 14 Jan 2025 18:21:31 +0000 Subject: [PATCH 3/7] add `disableActions` option to repeater --- frontend/js/components/Repeater.vue | 5 +++++ src/Services/Forms/Fields/Repeater.php | 8 ++++++++ src/Services/Forms/InlineRepeater.php | 9 +++++++++ src/View/Components/Fields/Repeater.php | 1 + views/partials/form/_repeater.blade.php | 1 + 5 files changed, 24 insertions(+) diff --git a/frontend/js/components/Repeater.vue b/frontend/js/components/Repeater.vue index 3fa5eaf89..59141f1e5 100755 --- a/frontend/js/components/Repeater.vue +++ b/frontend/js/components/Repeater.vue @@ -8,6 +8,7 @@ :block="block" :index="index" :withHandle="allowSortable && draggable" + :withActions="allowActions" :size="blockSize" :opened="opened" > @@ -114,6 +115,10 @@ type: Boolean, default: true }, + allowActions: { + type: Boolean, + default: true + }, max: { type: [Number, null], required: false, diff --git a/src/Services/Forms/Fields/Repeater.php b/src/Services/Forms/Fields/Repeater.php index 5db9adf82..95ebe98ba 100644 --- a/src/Services/Forms/Fields/Repeater.php +++ b/src/Services/Forms/Fields/Repeater.php @@ -14,6 +14,7 @@ class Repeater extends BaseFormField protected bool $buttonAsLink = false; protected bool $allowCreate = true; protected bool $allowSortable = true; + protected bool $allowActions = true; protected ?string $relation = null; protected ?array $browserModule = null; @@ -71,6 +72,13 @@ public function allowSortable(bool $allowSortable = true): static return $this; } + public function allowActions(bool $allowActions = true): static + { + $this->allowActions = $allowActions; + + return $this; + } + public function browserModule(?array $browserModule = null): static { $this->browserModule = $browserModule; diff --git a/src/Services/Forms/InlineRepeater.php b/src/Services/Forms/InlineRepeater.php index c8136f56b..a6fe546d0 100644 --- a/src/Services/Forms/InlineRepeater.php +++ b/src/Services/Forms/InlineRepeater.php @@ -26,6 +26,7 @@ protected function __construct( 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, @@ -103,6 +104,13 @@ public function disableSortable(bool $disableSortable = true): static 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. */ @@ -216,6 +224,7 @@ public function render(): View ->type($this->getRenderName()) ->allowCreate($this->allowCreate) ->allowSortable($this->allowSortable) + ->allowActions($this->allowActions) ->relation($this->relation ?? null) ->browserModule($this->allowBrowse ? $this->browser : null); diff --git a/src/View/Components/Fields/Repeater.php b/src/View/Components/Fields/Repeater.php index 59e03b152..2e55601c0 100644 --- a/src/View/Components/Fields/Repeater.php +++ b/src/View/Components/Fields/Repeater.php @@ -14,6 +14,7 @@ public function __construct( 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 diff --git a/views/partials/form/_repeater.blade.php b/views/partials/form/_repeater.blade.php index fd09204c8..e7659beee 100644 --- a/views/partials/form/_repeater.blade.php +++ b/views/partials/form/_repeater.blade.php @@ -8,6 +8,7 @@ @if ($relation) relation="{{$relation}}" @endif :allow-create="{{$allowCreate ? 'true' : 'false'}}" :allow-sortable="{{$allowSortable ? 'true' : 'false'}}" + :allow-actions="{{$allowActions ? 'true' : 'false' }}" > @unless($renderForBlocks) From 757f7ff3b87599237c7fc306d9bf2c2d6207961b Mon Sep 17 00:00:00 2001 From: Mike Byrne Date: Tue, 14 Jan 2025 19:01:55 +0000 Subject: [PATCH 4/7] refactor inline repeater options to use traits * `disableSortable()` -> `disableReorder` (existing trait) * `disableActions()` uses new trait --- frontend/js/components/Repeater.vue | 16 ++++++------ src/Services/Forms/Fields/Repeater.php | 18 ++----------- .../Forms/Fields/Traits/DisableActions.php | 18 +++++++++++++ src/Services/Forms/InlineRepeater.php | 25 ++++++------------- src/View/Components/Fields/Repeater.php | 3 +-- views/partials/form/_repeater.blade.php | 3 +-- 6 files changed, 37 insertions(+), 46 deletions(-) create mode 100644 src/Services/Forms/Fields/Traits/DisableActions.php diff --git a/frontend/js/components/Repeater.vue b/frontend/js/components/Repeater.vue index 59141f1e5..0f97a4f70 100755 --- a/frontend/js/components/Repeater.vue +++ b/frontend/js/components/Repeater.vue @@ -7,8 +7,8 @@ ref="blockList" :block="block" :index="index" - :withHandle="allowSortable && draggable" - :withActions="allowActions" + :withHandle="reorder && draggable" + :withActions="displayActions" :size="blockSize" :opened="opened" > @@ -111,11 +111,7 @@ type: Boolean, default: true }, - allowSortable: { - type: Boolean, - default: true - }, - allowActions: { + displayActions: { type: Boolean, default: true }, @@ -123,7 +119,11 @@ type: [Number, null], required: false, default: null - } + }, + reorder: { + type: Boolean, + default: true + }, }, data: function () { return { diff --git a/src/Services/Forms/Fields/Repeater.php b/src/Services/Forms/Fields/Repeater.php index 95ebe98ba..36281184d 100644 --- a/src/Services/Forms/Fields/Repeater.php +++ b/src/Services/Forms/Fields/Repeater.php @@ -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; @@ -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; diff --git a/src/Services/Forms/Fields/Traits/DisableActions.php b/src/Services/Forms/Fields/Traits/DisableActions.php new file mode 100644 index 000000000..83a0d1b09 --- /dev/null +++ b/src/Services/Forms/Fields/Traits/DisableActions.php @@ -0,0 +1,18 @@ +displayActions = !$actions; + + return $this; + } +} diff --git a/src/Services/Forms/InlineRepeater.php b/src/Services/Forms/InlineRepeater.php index a6fe546d0..e75b9724b 100644 --- a/src/Services/Forms/InlineRepeater.php +++ b/src/Services/Forms/InlineRepeater.php @@ -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; @@ -17,6 +19,8 @@ class InlineRepeater implements CanHaveSubfields, CanRenderForBlocks { use RenderForBlocks; use HasSubFields; + use CanReorder; + use DisableActions; protected function __construct( private ?string $name = null, @@ -25,8 +29,6 @@ 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, @@ -34,6 +36,7 @@ protected function __construct( private ?string $titleField = null, private ?bool $hideTitlePrefix = false, private ?bool $buttonAsLink = false, + private ?bool $displayActions = true, protected ?array $connectedTo = null, ) { } @@ -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. */ @@ -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); diff --git a/src/View/Components/Fields/Repeater.php b/src/View/Components/Fields/Repeater.php index 2e55601c0..bdb47a0b4 100644 --- a/src/View/Components/Fields/Repeater.php +++ b/src/View/Components/Fields/Repeater.php @@ -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 diff --git a/views/partials/form/_repeater.blade.php b/views/partials/form/_repeater.blade.php index e7659beee..d3d6dae16 100644 --- a/views/partials/form/_repeater.blade.php +++ b/views/partials/form/_repeater.blade.php @@ -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'}}" > @unless($renderForBlocks) From 9908a997d061bbafc13fda89fce858a495b4b65a Mon Sep 17 00:00:00 2001 From: Mike Byrne Date: Tue, 14 Jan 2025 19:08:07 +0000 Subject: [PATCH 5/7] document repeater `disableCreate`, `disableActions` and `disableReorder` --- docs/content/1_docs/4_form-fields/11_repeater.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/docs/content/1_docs/4_form-fields/11_repeater.md b/docs/content/1_docs/4_form-fields/11_repeater.md index 838f0d829..096550802 100644 --- a/docs/content/1_docs/4_form-fields/11_repeater.md +++ b/docs/content/1_docs/4_form-fields/11_repeater.md @@ -29,13 +29,15 @@ Repeater::make() :::#tab::: :::#tabs::: -| Option | Description | Type | Default value | -|:-------------|:---------------------------------------------|:--------|:-----------------| -| type | Type of repeater items | string | | -| name | Name of the field | string | same as `type` | -| max | Maximum amount that can be created | number | null (unlimited) | -| buttonAsLink | Displays the `Add` button as a centered link | boolean | false | -| reorder | Allow reordering of repeater items | boolean | true | +| Option | Description | Type | Default value | +|:---------------|:---------------------------------------------|:--------|:-----------------| +| type | Type of repeater items | string | | +| name | Name of the field | string | same as `type` | +| max | Maximum amount that can be created | number | null (unlimited) | +| buttonAsLink | Displays the `Add` button as a centered link | boolean | false | +| disableCreate | Disables ability to add new items | boolean | false | +| disableActions | Removes row item actions | boolean | false | +| disableReorder | Disables reordering of repeater items | boolean | false |
From 3fb5d0e1d2cb72684f5af044dd061c4f5e614862 Mon Sep 17 00:00:00 2001 From: Mike Byrne Date: Tue, 14 Jan 2025 19:09:54 +0000 Subject: [PATCH 6/7] update defaults in docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apparently I'm finding boolean too difficult to comprehend 🤦🏻‍♂️ --- docs/content/1_docs/4_form-fields/11_repeater.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/content/1_docs/4_form-fields/11_repeater.md b/docs/content/1_docs/4_form-fields/11_repeater.md index 096550802..1a64cf8d9 100644 --- a/docs/content/1_docs/4_form-fields/11_repeater.md +++ b/docs/content/1_docs/4_form-fields/11_repeater.md @@ -35,9 +35,9 @@ Repeater::make() | name | Name of the field | string | same as `type` | | max | Maximum amount that can be created | number | null (unlimited) | | buttonAsLink | Displays the `Add` button as a centered link | boolean | false | -| disableCreate | Disables ability to add new items | boolean | false | -| disableActions | Removes row item actions | boolean | false | -| disableReorder | Disables reordering of repeater items | boolean | false | +| disableCreate | Disables ability to add new items | boolean | true | +| disableActions | Removes row item actions | boolean | true | +| disableReorder | Disables reordering of repeater items | boolean | true |
From 985f13eaa5fdd4bacd574808d9ee3cb494a5622f Mon Sep 17 00:00:00 2001 From: Mike Byrne Date: Wed, 22 Jan 2025 17:22:47 +0000 Subject: [PATCH 7/7] use $displayActions from trait --- src/Services/Forms/Fields/Traits/DisableActions.php | 6 +++--- src/Services/Forms/InlineRepeater.php | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Services/Forms/Fields/Traits/DisableActions.php b/src/Services/Forms/Fields/Traits/DisableActions.php index 83a0d1b09..98f7ee6e6 100644 --- a/src/Services/Forms/Fields/Traits/DisableActions.php +++ b/src/Services/Forms/Fields/Traits/DisableActions.php @@ -4,14 +4,14 @@ trait DisableActions { - protected bool $actions = true; + protected bool $displayActions = true; /** * Adds a border around the options. */ - public function disableActions(bool $actions = true): static + public function disableActions(bool $displayActions = true): static { - $this->displayActions = !$actions; + $this->displayActions = !$displayActions; return $this; } diff --git a/src/Services/Forms/InlineRepeater.php b/src/Services/Forms/InlineRepeater.php index e75b9724b..ba40578a7 100644 --- a/src/Services/Forms/InlineRepeater.php +++ b/src/Services/Forms/InlineRepeater.php @@ -36,7 +36,6 @@ protected function __construct( private ?string $titleField = null, private ?bool $hideTitlePrefix = false, private ?bool $buttonAsLink = false, - private ?bool $displayActions = true, protected ?array $connectedTo = null, ) { }