diff --git a/src/Connected.Components/ComponentsN/Select.razor b/src/Connected.Components/ComponentsN/Select.razor index c4c3e62..eddab26 100644 --- a/src/Connected.Components/ComponentsN/Select.razor +++ b/src/Connected.Components/ComponentsN/Select.razor @@ -2,32 +2,48 @@ -
- @if (SearchFieldEnabled) +
+ @if (SearchFieldEnabled && SearchFieldVisibility) { //search field + } - @foreach (SelectItem item in Items) - { - - } +
+ @if(ItemsCount>0) + { + @foreach (SelectItem item in _itemsToShow) + { + + } + } else + { +
Ni elementov
+ } +
- @if (_pagination()>1) + @if (numberOfPages>1) { - //pagination +
+ @if (numberOfPages < 6) + { + @for (int i = 1; i <= numberOfPages; i++) + { + var index = i; + @index.ToString() + } + } else + { + //pagination of more than 6 pages + } +
+ //pagination }
diff --git a/src/Connected.Components/ComponentsN/Select.razor.cs b/src/Connected.Components/ComponentsN/Select.razor.cs index 7480c9c..45db35b 100644 --- a/src/Connected.Components/ComponentsN/Select.razor.cs +++ b/src/Connected.Components/ComponentsN/Select.razor.cs @@ -1,5 +1,7 @@ using Connected.Utilities; using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Web; + namespace Connected.ComponentsN; public partial class Select { @@ -8,43 +10,113 @@ public partial class Select public bool SearchFieldEnabled { get; set; } = false; [Parameter] - public int MaxVisibleItems { get; set; } = 7; + public int MaxVisibleItems { get; set; } = 5; [Parameter] public List> Items { - get{ - if (FilterText.Length > 0) - { - return _items.Where(i => i.Value.ToString().Contains(FilterText) || i.Text.ToString().Contains(FilterText)).ToList(); - } - return _items; - } set { - _items= value; + _originalList = value; + _itemsWithFilter= value; + _itemsToShow = _itemsWithFilter.Skip(Offset).Take(MaxVisibleItems).ToList(); } } - private List> _items = new List>(); + private void SetPage(int page) + { + _selectedPage = page; + SetLists(); + StateHasChanged(); + } - private int ItemsCount => Items.Count(); + private void SetLists() + { + if (FilterText.Length > 0) + { + _itemsWithFilter = _originalList.Where(i => i.Value.ToString().Contains(FilterText) || i.Text.ToString().Contains(FilterText)).ToList(); + } + else + { + _itemsWithFilter = _originalList; + } + _itemsToShow = _itemsWithFilter.Skip(Offset).Take(MaxVisibleItems).ToList(); + } + + private int Offset + { + get + { + return (_selectedPage - 1) * MaxVisibleItems; + } + } + private List> _itemsWithFilter; + private List> _itemsToShow; + private List> _originalList; - [Parameter] - public int SelectedIndex { get; set; } = 0; + public EventCallback ValueChanged { get; set; } + private async Task Search(ChangeEventArgs args) + { + FilterText = args.Value.ToString(); + SetLists(); + _pagination(); + await ValueChanged.InvokeAsync(args.Value.ToString()); + } + + private int ItemsCount => _itemsWithFilter.Count(); + + private int Height = 20; + + private bool SearchFieldVisibility = false; + + private void HeightOnFull() + { + if (Height < 400) + { + SearchFieldVisibility = true; + Height = 400; + StateHasChanged(); + } + } + + private void HeightToDefault() + { + if (Height > 20) + { + SearchFieldVisibility = false; + Height = 20; + StateHasChanged(); + } + } + + private string GetHeight() + { + return Height.ToString() + "px;"; + } + [Parameter] public string FilterText { get; set; } = string.Empty; - private int _pagination() + private int numberOfPages = 1; + private void _pagination() { - int result = 0; - if (MaxVisibleItems 0) result++; } - return result; + if (!numberOfPages.Equals(result)) + { + numberOfPages = result; + StateHasChanged(); + } + } + private int _selectedPage = 1; [Parameter] public string ClassList { get; set; } = string.Empty; @@ -60,4 +132,11 @@ public partial class Select .Build(); } } + + protected override Task OnParametersSetAsync() + { + _pagination(); + SetLists(); + return base.OnParametersSetAsync(); + } }