Skip to content

Commit 2bd1190

Browse files
committed
fix: reordering bug with drag n drop in repeater
1 parent df174de commit 2bd1190

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/Fields/Repeater.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,14 @@ public static function make(string $name, ?Field $field = null): Input
5959
{
6060
$input = self::applyDefaultSettings(Input::make($name), $field);
6161

62+
$isReorderable = $field->config['reorderable'] ?? self::getDefaultConfig()['reorderable'];
63+
$isReorderableWithButtons = $field->config['reorderableWithButtons'] ?? self::getDefaultConfig()['reorderableWithButtons'];
64+
6265
$input = $input->label($field->name ?? self::getDefaultConfig()['label'] ?? null)
6366
->addActionLabel($field->config['addActionLabel'] ?? self::getDefaultConfig()['addActionLabel'])
6467
->addable($field->config['addable'] ?? self::getDefaultConfig()['addable'])
6568
->deletable($field->config['deletable'] ?? self::getDefaultConfig()['deletable'])
66-
->reorderable($field->config['reorderable'] ?? self::getDefaultConfig()['reorderable'])
69+
->reorderable($isReorderable)
6770
->collapsible($field->config['collapsible'] ?? self::getDefaultConfig()['collapsible'])
6871
->cloneable($field->config['cloneable'] ?? self::getDefaultConfig()['cloneable'])
6972
->columns($field->config['columns'] ?? self::getDefaultConfig()['columns']);
@@ -72,10 +75,34 @@ public static function make(string $name, ?Field $field = null): Input
7275
$input = $input->compact();
7376
}
7477

75-
if ($field->config['reorderableWithButtons'] ?? self::getDefaultConfig()['reorderableWithButtons']) {
78+
if ($isReorderableWithButtons) {
7679
$input = $input->reorderableWithButtons();
7780
}
7881

82+
// Fix for Filament Forms v4.2.0 reorder bug
83+
// The default reorder action has a bug where array_flip() creates integer values
84+
// that get merged with the state array, causing type errors
85+
if ($isReorderable || $isReorderableWithButtons) {
86+
$input = $input->reorderAction(function ($action) {
87+
return $action->action(function (array $arguments, Input $component): void {
88+
$currentState = $component->getRawState();
89+
$newOrder = $arguments['items'];
90+
91+
// Reorder the items based on the new order
92+
$reorderedItems = [];
93+
foreach ($newOrder as $oldIndex) {
94+
if (isset($currentState[$oldIndex])) {
95+
$reorderedItems[] = $currentState[$oldIndex];
96+
}
97+
}
98+
99+
$component->rawState($reorderedItems);
100+
$component->callAfterStateUpdated();
101+
$component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null;
102+
});
103+
});
104+
}
105+
79106
if ($field && ! $field->relationLoaded('children')) {
80107
$field->load('children');
81108
}

0 commit comments

Comments
 (0)