Repeater Index

Is it possible to get the index value in a repeater? I want to set a field based on the index. something like:
Repeater::make('addresses')
->relationship()
->minItems(1)
->maxItems(2)
->schema([
Select::make('billing_or_delivery')
->required()
->options([
'Delivery' => 'Delivery',
'Billing' => 'Billing',
])
->default($index ? 'Delivery' : 'Billing')
....
Repeater::make('addresses')
->relationship()
->minItems(1)
->maxItems(2)
->schema([
Select::make('billing_or_delivery')
->required()
->options([
'Delivery' => 'Delivery',
'Billing' => 'Billing',
])
->default($index ? 'Delivery' : 'Billing')
....
2 Replies
Patrick Boivin
Patrick Boivin17mo ago
I'm not sure if there is a simple solution to this... you may need to extend the Repeater component
greatfortress
greatfortress16mo ago
This isn't an exact solution to what you are looking for but something similar could be done. I wanted a repeater that used the index in the label so I overrode the getItemLabel method while extending the Repeater component.
<?php

namespace App\Forms\Components;

use Filament\Forms\Components\Repeater;
use Illuminate\Contracts\Support\Htmlable;

class IndexRepeater extends Repeater
{
public function getItemLabel(string $uuid): string | Htmlable | null
{

return $this->evaluate($this->itemLabel, [
'state' => $this->getChildComponentContainer($uuid)->getRawState(),
'index' => (string) array_search($uuid, array_keys($this->getState())),
'uuid' => $uuid,
]);
}

}
<?php

namespace App\Forms\Components;

use Filament\Forms\Components\Repeater;
use Illuminate\Contracts\Support\Htmlable;

class IndexRepeater extends Repeater
{
public function getItemLabel(string $uuid): string | Htmlable | null
{

return $this->evaluate($this->itemLabel, [
'state' => $this->getChildComponentContainer($uuid)->getRawState(),
'index' => (string) array_search($uuid, array_keys($this->getState())),
'uuid' => $uuid,
]);
}

}
Want results from more Discord servers?
Add your server