Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not implement a Filament Repeater as attribute field #2071

Open
tdwesten opened this issue Jan 21, 2025 · 0 comments
Open

Can not implement a Filament Repeater as attribute field #2071

tdwesten opened this issue Jan 21, 2025 · 0 comments
Labels
bug Something isn't working unconfirmed

Comments

@tdwesten
Copy link

tdwesten commented Jan 21, 2025

  • Lunar version: v1.0.0-beta.4
  • Laravel Version: v11
  • PHP Version: 8.3.15
  • Database Driver & Version: Mysql

Expected Behaviour:

I can use the Filament Repeater field to create more complex data structures based on a array of objects.

Actual Behaviour:

When I create a custom attribute field using the Filament Repeater field, which extends from the Lunar List field, it doesn't render existing data and throws this error:

Filament\Forms\ComponentContainer::getRawState(): Return value must be of type Illuminate\Contracts\Support\Arrayable|array, stdClass returned

Steps To Reproduce:

  1. Create a new field:
<?php

namespace App\Domain\Lunar\Fields;

use Lunar\FieldTypes\ListField;

class ProductDimensionsField extends ListField {}
  1. Create a field type:
<?php

namespace App\Domain\Lunar\FieldTypes;

use App\Domain\Lunar\Synthesizers\ProductDimensionsFieldSynth;
use Filament\Forms\Components\Component;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\TextInput;
use Lunar\Admin\Support\FieldTypes\ListField;
use Lunar\Models\Attribute;

class ProductDimensionsFieldType extends ListField
{
    /**
     * Specify the Synthesizer for this field type
     */
    protected static string $synthesizer = ProductDimensionsFieldSynth::class;

    /**
     * Define the Filament component for rendering
     */
    public static function getFilamentComponent(Attribute $attribute): Component
    {
        return Repeater::make($attribute->handle)
            ->collapsed()
            ->itemLabel(fn (array $state): ?string => self::getLabel($state))
            ->columns(3)
            ->schema([
                TextInput::make('length')
                    ->numeric()
                    ->label('Length')
                    ->required(),
                TextInput::make('depth')
                    ->numeric()
                    ->label('Depth')
                    ->required(),
                TextInput::make('height')
                    ->numeric()
                    ->label('Height')
                    ->required(),
                TextInput::make('label')
                    ->label('Label')
                    ->required(),
            ])
            ->addable()
            ->deletable()
            ->reorderable();
    }

    /**
     * Get the label for the repeater item
     */
    protected static function getLabel(array $state): ?string
    {
        return sprintf(
            '%s | Price: %s - Updated: %s',
            $state['label'],
            $state['price_formatted'] ?? 'N/A',
            $state['elfsquad_update_timestamp'] ?? 'N/A'
        );
    }
}
  1. Create a Livewire Synthesizer
<?php

namespace App\Domain\Lunar\Synthesizers;

use App\Domain\Lunar\Fields\ProductDimensionsField;
use Lunar\Admin\Support\Synthesizers\ListSynth;

class ProductDimensionsFieldSynth extends ListSynth
{
    /**
     * Unique key for the synthesizer
     */
    public static $key = 'lunar_dimensions_field';

    protected static $targetClass = ProductDimensionsField::class;
}

And register it field in your service provider.

        AttributeData::registerFieldType(ProductDimensionsField::class, ProductDimensionsFieldType::class);
@tdwesten tdwesten added bug Something isn't working unconfirmed labels Jan 21, 2025
@tdwesten tdwesten changed the title How to implement a Filament Repeater as attribute field Can not implement a Filament Repeater as attribute field Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unconfirmed
Projects
None yet
Development

No branches or pull requests

1 participant