Skip to content

Commit

Permalink
Pass component to field class
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Jan 22, 2024
1 parent 68fdc8f commit 70f70d5
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/BladeDirectives.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
");
}

Expand Down Expand Up @@ -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]);
}
?>";
}
Expand Down
15 changes: 8 additions & 7 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Properties/WithId.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ trait WithId
{
protected function idProperty(): string
{
return "{$this->id}-field-{$this->handle}";
return "{$this->component->getId()}-field-{$this->handle}";
}
}
4 changes: 2 additions & 2 deletions src/Fields/Properties/WithView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/Livewire/Concerns/WithFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 0 additions & 18 deletions src/Livewire/Concerns/WithViewManager.php

This file was deleted.

2 changes: 0 additions & 2 deletions src/Livewire/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Aerni\LivewireForms\Livewire\Concerns\WithMessages;
use Aerni\LivewireForms\Livewire\Concerns\WithTheme;
use Aerni\LivewireForms\Livewire\Concerns\WithView;
use Aerni\LivewireForms\Livewire\Concerns\WithViewManager;
use Illuminate\Contracts\View\View;
use Livewire\Component;

Expand All @@ -22,7 +21,6 @@ class Form extends Component
use WithFields;
use WithForm;
use WithMessages;
use WithViewManager;
use SubmitsForm;

public function render(): View
Expand Down
2 changes: 1 addition & 1 deletion src/Livewire/Synthesizers/FieldSynth.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function hydrate($value, $meta, $hydrateChild)

return $meta['class']::make(
field: new StatamicField($value['handle'], $value['config']),
id: $this->context->component->getId()
component: $this->context->component
)
->properties($value['properties'])
->value($value['value']);
Expand Down
13 changes: 2 additions & 11 deletions src/ViewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 70f70d5

Please sign in to comment.