awcodes / filament-table-repeater

A modified version of the Filament Forms Repeater to display it as a table.
MIT License
188 stars 42 forks source link

defaultItems(0) breaks when no labels are provided #78

Closed niccolofavari closed 5 months ago

niccolofavari commented 7 months ago

Filament Version

v3

Plugin Version

v2.1.0

PHP Version

PHP 8.3.0

Problem description

Using defaultItems(0) will result in a Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization error. A simple repeater will not issue this error and works as expected. The issue is resolved when using a custom label for each fields.

Expected behavior

Expected an empty repeater in the create form.

Steps to reproduce

I'm using this code

Section::make()->schema([
                TableRepeater::make('projectSpNatures')
                    ->relationship()
                    ->columnSpan('full')
                    ->schema([
                        Select::make('spNature_id')
                            ->relationship('spNature', 'name')
                            ->hiddenLabel(true)
                            ->required(),
                        TextInput::make('budget')
                            ->hiddenLabel(true)
                            ->numeric(),
                    ])
                    ->defaultItems(0)
                    ->cloneable()
                    ->orderColumn('sort_order')
                    ->mutateRelationshipDataBeforeSaveUsing(fn($record, $data) => $data['project_id'] = $record->project_id)
            ])
                ->hiddenOn(['view', 'edit']),

Adding a ->label("Custom label") for each field will resolve the issue.

Reproduction repository

No response

Relevant log output

No response

awcodes commented 7 months ago

Can you share a reproduction repo. ->defaultItems(0) is working for me.

Are you using the panels or the stand alone forms package?

monzer15 commented 7 months ago

I just encountered the same issue (error), here is my repeater

 TableRepeater::make('variants')
 ->visible(fn(Forms\Get $get): bool => $get('type') === Product::$TYPE_VARIANTS)
 ->relationship('variants')
 ->hideLabels()
 ->emptyLabel("")
->defaultItems(0)
 ->addable(false)
 ->label(fn(Forms\Get $get) => __('fields.customize_options') . " (" . $get('variants_count') . ")")
->addActionLabel(__('fields.add'))
->live()
->deleteAction(
 fn(Forms\Components\Actions\Action $action) => $action->requiresConfirmation(),
)
 ->columnWidths([
  'name' => '200px',
  'warehouse_id' => '200px',
 'qty' => '90px',
  'unlimited_qty' => '10px',
 'unit_cost' => '135px',
 'price' => '135px',
'discount_price' => '135px',
 ])
awcodes commented 7 months ago

Yea, still trying to think of a solution for this. It has to read the child components to dynamic create the headers and if there are no items then it can generate the headers.

awcodes commented 7 months ago

A reproduction repo would really help me out.

I still can't replicate this and the only difference I'm seeing is that your repeaters are based on relationships and I currently don't have anything setup where I am using relationships that I could test with.

awcodes commented 5 months ago

Closing as stale.