From 92aaf6131cbd2b87e092a1ada0ba44e106da6239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20M=2E?= Date: Thu, 10 Oct 2024 19:05:15 +0200 Subject: [PATCH] Convert to methods This makes it possible to register only the one you need. --- .../Blade/Concerns/WithBladeMacros.php | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Support/Blade/Concerns/WithBladeMacros.php b/src/Support/Blade/Concerns/WithBladeMacros.php index dfb7497..147d624 100644 --- a/src/Support/Blade/Concerns/WithBladeMacros.php +++ b/src/Support/Blade/Concerns/WithBladeMacros.php @@ -7,7 +7,7 @@ trait WithBladeMacros { - protected function registerBladeMacros(): static + protected function cssClass(): void { ComponentAttributeBag::macro('cssClass', function (array $values = []): mixed { foreach ($values as $key => $value) { @@ -21,7 +21,10 @@ protected function registerBladeMacros(): static return $this; }); + } + protected function classMerge(): void + { ComponentAttributeBag::macro('classMerge', function (?array $values = null): mixed { /** @var ComponentAttributeBag $this */ $classes = Bladeable::classMerged($this, $values) @@ -33,19 +36,28 @@ protected function registerBladeMacros(): static return $this ->withoutClass(); }); + } + protected function withoutClass(): void + { ComponentAttributeBag::macro('withoutClass', function (): mixed { /** @var ComponentAttributeBag $this */ return $this ->whereDoesntStartWith('class:'); }); + } + protected function withoutWireModel(): void + { ComponentAttributeBag::macro('withoutWireModel', function (): mixed { /** @var ComponentAttributeBag $this */ return $this ->whereDoesntStartWith('wire:model'); }); + } + protected function mergeAttributes(): void + { ComponentAttributeBag::macro('mergeAttributes', function (array $values = []): mixed { foreach ($values as $key => $value) { /** @var ComponentAttributeBag $this */ @@ -54,7 +66,10 @@ protected function registerBladeMacros(): static return $this; }); + } + protected function classFor(): void + { ComponentAttributeBag::macro('classFor', function (string $key, string $default = ''): mixed { /** @var ComponentAttributeBag $this */ $class = Bladeable::classKeys($key)->first(); @@ -62,16 +77,21 @@ protected function registerBladeMacros(): static return $this->get($class, $default); }); + } + + protected function wireModel(): void + { ComponentAttributeBag::macro('wireModel', function (): mixed { /** @var ComponentAttributeBag $this */ return $this->whereStartsWith('wire:model')->first(); }); + } + protected function wireKey(): void + { ComponentAttributeBag::macro('wireKey', function (): mixed { /** @var ComponentAttributeBag $this */ return $this->wireModel() ?: $this->first('id') ?: $this->first('name'); }); - - return $this; } }