Skip to content

Commit

Permalink
Fix ReplaceChildElement
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper committed Sep 5, 2023
1 parent 974ccb9 commit c0c1fad
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/BlazorBindings.Core/NativeComponentAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,23 @@ public void ApplyPendingEdits()
// If we have two consequent edits (Add -> Remove or Remove -> Add) for the same index,
// and non of them are INonPhysicalChild elements,
// we try to replace them instead of adding and removing separately.
if (nextEdit.Index == index)
if (nextEdit.Index == index
&& elementToRemove is not null and not { _targetElement: INonPhysicalChild }
&& nextEdit.ElementToAdd is not null and not { _targetElement: INonPhysicalChild })
{
if (elementToRemove is not null and not { _targetElement: INonPhysicalChild }
&& nextEdit.ElementToAdd is not null and not { _targetElement: INonPhysicalChild })
{
Renderer.ElementManager.ReplaceChildElement(_targetElement, elementToRemove._targetElement, nextEdit.ElementToAdd._targetElement, index);
i++;
continue;
}
else if (elementToAdd is not null and not { _targetElement: INonPhysicalChild }
&& nextEdit.ElementToRemove is not null and not { _targetElement: INonPhysicalChild })
{
Renderer.ElementManager.ReplaceChildElement(_targetElement, nextEdit.ElementToRemove._targetElement, elementToAdd._targetElement, index);
i++;
continue;
}
Renderer.ElementManager.ReplaceChildElement(_targetElement, elementToRemove._targetElement, nextEdit.ElementToAdd._targetElement, index);
i++;
continue;
}
if ((nextEdit.Index == index + 1 || nextEdit.Index == index - 1)
&& elementToAdd is not null and not { _targetElement: INonPhysicalChild }
&& nextEdit.ElementToRemove is not null and not { _targetElement: INonPhysicalChild })
{
// When we add element first, and then delete - index shifts.
var replacedIndex = Math.Min(index, nextEdit.Index);
Renderer.ElementManager.ReplaceChildElement(_targetElement, nextEdit.ElementToRemove._targetElement, elementToAdd._targetElement, replacedIndex);
i++;
continue;
}

if (elementToRemove != null)
Expand Down

0 comments on commit c0c1fad

Please sign in to comment.