diff --git a/src/BladeDirectives.php b/src/BladeDirectives.php index 6e457487..2403411d 100644 --- a/src/BladeDirectives.php +++ b/src/BladeDirectives.php @@ -26,7 +26,7 @@ public static function formView(string $expression): string $arguments = $variables[1] ?? '[]'; return Blade::compileString(" - @include(\Aerni\LivewireForms\Facades\ViewManager::themeViewPath($view), $arguments) + @include(\Aerni\LivewireForms\Facades\ViewManager::themeViewPath(\$this->theme, $view), $arguments) "); } @@ -58,7 +58,7 @@ public static function formField(string $expression): string \$field->\$property(\$value); } - echo view(\Aerni\LivewireForms\Facades\ViewManager::themeViewPath('layouts.field'), ['field' => \$field]); + echo view(\Aerni\LivewireForms\Facades\ViewManager::themeViewPath(\$this->theme, 'layouts.field'), ['field' => \$field]); } ?>"; } diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 9ce49bb0..c2774e83 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -5,7 +5,8 @@ use Aerni\LivewireForms\Fields\Concerns\HandlesProperties; use Aerni\LivewireForms\Fields\Concerns\WithDefaultProperties; use Illuminate\Contracts\Support\Arrayable; -use Statamic\Fields\Field as StatamicField; +use Livewire\Component; +use Statamic\Fields\Field as FormField; use Statamic\Support\Traits\FluentlyGetsAndSets; abstract class Field implements Arrayable @@ -14,17 +15,17 @@ abstract class Field implements Arrayable use HandlesProperties; use WithDefaultProperties; + protected mixed $value = null; + public function __construct( - protected StatamicField $field, - protected string $id, - protected mixed $value = null + protected FormField $field, + protected Component $component, ) { - // } - public static function make(StatamicField $field, string $id): self + public static function make(FormField $field, Component $component): self { - return new static($field, $id); + return new static($field, $component); } public function rules(): array diff --git a/src/Fields/Properties/WithId.php b/src/Fields/Properties/WithId.php index e5805258..de25a99f 100644 --- a/src/Fields/Properties/WithId.php +++ b/src/Fields/Properties/WithId.php @@ -6,6 +6,6 @@ trait WithId { protected function idProperty(): string { - return "{$this->id}-field-{$this->handle}"; + return "{$this->component->getId()}-field-{$this->handle}"; } } diff --git a/src/Fields/Properties/WithView.php b/src/Fields/Properties/WithView.php index 1eee3ac9..e1407253 100644 --- a/src/Fields/Properties/WithView.php +++ b/src/Fields/Properties/WithView.php @@ -11,8 +11,8 @@ protected function viewProperty(): string $configViewName = "fields.{$this->field->get('view')}"; $handleViewName = "fields.{$this->handle}"; - $configView = ViewManager::themeViewPath($configViewName); - $handleView = ViewManager::themeViewPath($handleViewName); + $configView = ViewManager::themeViewPath($this->component->theme, $configViewName); + $handleView = ViewManager::themeViewPath($this->component->theme, $handleViewName); return match (true) { view()->exists($configView) => $configViewName, // Try to load a user-defined view first. diff --git a/src/Livewire/Concerns/WithFields.php b/src/Livewire/Concerns/WithFields.php index 8284ef60..92518ca5 100644 --- a/src/Livewire/Concerns/WithFields.php +++ b/src/Livewire/Concerns/WithFields.php @@ -37,13 +37,13 @@ protected function fields(): Collection ?? $this->models()->get($fieldtype); return $class - ? $class::make(field: $field, id: $this->getId()) + ? $class::make(field: $field, component: $this) : throw new \Exception("The field model binding for fieldtype [{$fieldtype}] cannot be found."); }); $honeypot = Honeypot::make( field: new Field($this->form->honeypot(), []), - id: $this->getId() + component: $this ); return $fields->put($honeypot->handle, $honeypot); diff --git a/src/Livewire/Concerns/WithViewManager.php b/src/Livewire/Concerns/WithViewManager.php deleted file mode 100644 index dc2cb871..00000000 --- a/src/Livewire/Concerns/WithViewManager.php +++ /dev/null @@ -1,18 +0,0 @@ -context->component->getId() + component: $this->context->component ) ->properties($value['properties']) ->value($value['value']); diff --git a/src/ViewManager.php b/src/ViewManager.php index 98c4459b..ede450bc 100644 --- a/src/ViewManager.php +++ b/src/ViewManager.php @@ -2,25 +2,16 @@ namespace Aerni\LivewireForms; -use Livewire\Component; - class ViewManager { - protected Component $component; - - public function boot(Component $component): void - { - $this->component = $component; - } - public function viewPath(string $view): string { return config('livewire-forms.view_path', 'livewire/forms')."/{$view}"; } - public function themeViewPath(string $view): string + public function themeViewPath(string $theme, string $view): string { - $themeView = $this->viewPath("{$this->component->theme}/{$view}"); + $themeView = $this->viewPath("{$theme}/{$view}"); $defaultThemeView = $this->viewPath("{$this->defaultTheme()}/{$view}"); return view()->exists($themeView) ? $themeView : $defaultThemeView;