Skip to content

Commit c7fd809

Browse files
authored
Gallery Fix Same Item Loop Cycle (#524)
1 parent 102fcd0 commit c7fd809

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

CodeBeam.MudBlazor.Extensions.Docs/Pages/Components/Gallery/Examples/GalleryExample1.razor

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<MudGrid>
55
<MudItem xs="12" sm="8" Class="d-flex align-center flex-wrap">
66
<MudGallery @ref="_gallery" ImageSource="_source" ItemPerLine="_itemPerLine" EnableBackdropClick="_enableBackdropClick"
7-
ShowToolboxCloseButton="_showToolboxCloseButton" ShowToolboxNavigationButtons="_showToolboxNavigationButtons"
8-
MaxWidth="_maxWidth" StyleSelectedImage="@ImageStyle" EnableAnimation="_enableAnimation">
7+
ShowToolboxCloseButton="_showToolboxCloseButton" ShowToolboxNavigationButtons="_showToolboxNavigationButtons"
8+
MaxWidth="_maxWidth" StyleSelectedImage="@ImageStyle" EnableAnimation="_enableAnimation">
99
<ToolboxTopContent>
1010
<MudText Class="white-text pa-4">Image @(_gallery?.GetSelectedImageIndex() + 1) - Description</MudText>
1111
</ToolboxTopContent>
@@ -56,6 +56,7 @@
5656
"https://mudblazor.com/images/castle.jpg",
5757
"https://cdn.pixabay.com/photo/2022/08/01/13/20/lily-of-the-valley-7358144__340.jpg",
5858
"https://cdn.pixabay.com/photo/2022/03/31/01/05/bird-7102006__340.jpg",
59+
"https://mudblazor.com/images/castle.jpg",
5960
"https://cdn.pixabay.com/photo/2019/06/05/08/37/dog-4253238__340.jpg",
6061
"https://cdn.pixabay.com/photo/2022/07/27/03/23/deer-7347041_960_720.jpg",
6162
"https://cdn.pixabay.com/photo/2022/03/31/11/28/snakes-head-fritillary-7102810_960_720.jpg",

CodeBeam.MudBlazor.Extensions/Components/Gallery/MudGallery.razor.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public partial class MudGallery : MudComponentBase
1515
bool _visible = false;
1616
DialogOptions _dialogOptions = new() { NoHeader = true, FullWidth = true, MaxWidth = MaxWidth.Large, CloseOnEscapeKey = true };
1717
string? _selectedSrc;
18+
int _selectedIndex = 0;
1819

1920
/// <summary>
2021
///
@@ -104,6 +105,7 @@ public partial class MudGallery : MudComponentBase
104105
protected void ImageClick(string src)
105106
{
106107
_selectedSrc = src;
108+
_selectedIndex = ImageSource.IndexOf(src);
107109
_visible = true;
108110
StateHasChanged();
109111
}
@@ -128,19 +130,29 @@ protected async Task SetAdjacentImage(int count)
128130
{
129131
return;
130132
}
131-
int index = ImageSource.IndexOf(_selectedSrc);
132133

133-
if (ImageSource.Count <= index + count || index + count < 0)
134+
if (EnableAnimation)
135+
{
136+
await _animate.Refresh();
137+
}
138+
139+
if (ImageSource.Count <= _selectedIndex + count)
134140
{
141+
_selectedSrc = ImageSource[0];
142+
_selectedIndex = 0;
135143
return;
136144
}
137145

138-
if (EnableAnimation)
146+
if (_selectedIndex + count < 0)
139147
{
140-
await _animate.Refresh();
148+
_selectedSrc = ImageSource[^1];
149+
_selectedIndex = ImageSource.Count - 1;
150+
return;
141151
}
142-
_selectedSrc = ImageSource[index + count];
143-
152+
153+
_selectedSrc = ImageSource[_selectedIndex + count];
154+
_selectedIndex += count;
155+
144156
}
145157

146158
/// <summary>
@@ -149,7 +161,7 @@ protected async Task SetAdjacentImage(int count)
149161
/// <returns></returns>
150162
public int GetSelectedImageIndex()
151163
{
152-
return ImageSource.IndexOf(_selectedSrc ?? "");
164+
return _selectedIndex;
153165
}
154166

155167
}

0 commit comments

Comments
 (0)