Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/Fields/Repeater.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ public static function make(string $name, ?Field $field = null): Input
{
$input = self::applyDefaultSettings(Input::make($name), $field);

$isReorderable = $field->config['reorderable'] ?? self::getDefaultConfig()['reorderable'];
$isReorderableWithButtons = $field->config['reorderableWithButtons'] ?? self::getDefaultConfig()['reorderableWithButtons'];

$input = $input->label($field->name ?? self::getDefaultConfig()['label'] ?? null)
->addActionLabel($field->config['addActionLabel'] ?? self::getDefaultConfig()['addActionLabel'])
->addable($field->config['addable'] ?? self::getDefaultConfig()['addable'])
->deletable($field->config['deletable'] ?? self::getDefaultConfig()['deletable'])
->reorderable($field->config['reorderable'] ?? self::getDefaultConfig()['reorderable'])
->reorderable($isReorderable)
->collapsible($field->config['collapsible'] ?? self::getDefaultConfig()['collapsible'])
->cloneable($field->config['cloneable'] ?? self::getDefaultConfig()['cloneable'])
->columns($field->config['columns'] ?? self::getDefaultConfig()['columns']);
Expand All @@ -72,10 +75,34 @@ public static function make(string $name, ?Field $field = null): Input
$input = $input->compact();
}

if ($field->config['reorderableWithButtons'] ?? self::getDefaultConfig()['reorderableWithButtons']) {
if ($isReorderableWithButtons) {
$input = $input->reorderableWithButtons();
}

// Fix for Filament Forms v4.2.0 reorder bug
// The default reorder action has a bug where array_flip() creates integer values
// that get merged with the state array, causing type errors
if ($isReorderable || $isReorderableWithButtons) {
$input = $input->reorderAction(function ($action) {
return $action->action(function (array $arguments, Input $component): void {
$currentState = $component->getRawState();
$newOrder = $arguments['items'];

// Reorder the items based on the new order
$reorderedItems = [];
foreach ($newOrder as $oldIndex) {
if (isset($currentState[$oldIndex])) {
$reorderedItems[] = $currentState[$oldIndex];
}
}

$component->rawState($reorderedItems);
$component->callAfterStateUpdated();
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
});
});
}

if ($field && ! $field->relationLoaded('children')) {
$field->load('children');
}
Expand Down