njxqlus / filament-relation-manager-component

Use Filament Relation Manager Everywhere!
MIT License
18 stars 2 forks source link

[Bug]: cannot use RelationManagerConfiguration #16

Open galliroberto opened 1 month ago

galliroberto commented 1 month ago

What happened?

Cannot use RelationManagerConfiguration

\Njxqlus\Filament\Components\Forms\RelationManager::make()->manager(XYZRelationManager::make([]))->lazy(false);

How to reproduce the bug

Please add RelationManagerConfiguration

<?php

namespace Njxqlus\Filament\Components\Forms;

use Closure;
use Filament\Forms\Components\Component;
use Filament\Resources\RelationManagers\RelationManagerConfiguration;

class RelationManager extends Component
{
    protected string $view = 'filament-relation-manager-component::forms.relation-manager';

    protected string | Closure | RelationManagerConfiguration $relationManager;

    protected bool | Closure $isLazy = false;

    public static function make(): static
    {
        return app(static::class);
    }

    public function manager(string | Closure | RelationManagerConfiguration $relationManager): static
    {
        $this->relationManager = $relationManager;

        return $this;
    }

    public function getRelationManager(): string | RelationManagerConfiguration
    {
        return $this->evaluate($this->relationManager);
    }

    public function lazy(bool | Closure $condition = true): static
    {
        $this->isLazy = $condition;

        return $this;
    }

    public function isLazy(): bool
    {
        return (bool) $this->evaluate($this->isLazy);
    }
}

inside both view use getProperties() instead protected properties

<div>
    @livewire($normalizedManagerClass, [...$managerLivewireProperties, ...$manager instanceof \Filament\Resources\RelationManagers\RelationManagerConfiguration ? $manager->getProperties() : []], key($normalizedManagerClass))
</div>

Package Version

1.0.2

PHP Version

8.3

Laravel Version

11

Filament Version

3.2.92

Notes

No response

njxqlus commented 1 month ago

Not really sure what is going on here and what do you want to achieve, but you are using package wrong. You should pass a class to ->manager() method, not XYZRelationManager::make([])

galliroberto commented 1 month ago

Are you sure?

I see your code, you put RelationManagerConfiguration in view

 $normalizeRelationManagerClass = function (string | Filament\Resources\RelationManagers\RelationManagerConfiguration $manager): string {
        if ($manager instanceof \Filament\Resources\RelationManagers\RelationManagerConfiguration) {
            return $manager->relationManager;
        }

        return $manager;
    };

However in this way you can pass property to RelationManager

njxqlus commented 1 month ago

Yes. This part is inherit from filament source code.

Method manager() accept string or Closure which must return string that must be equal relation manager class.

https://github.com/njxqlus/filament-relation-manager-component/blob/main/src/Forms/RelationManager.php#L21

public function manager(string | Closure $relationManager): static
    {
        $this->relationManager = $relationManager;

        return $this;
    }