From 1b118bf6e3c7e386fb72144f18aa3385dfa0db1d Mon Sep 17 00:00:00 2001 From: David Moreira Date: Sat, 17 Feb 2024 15:20:31 +0000 Subject: [PATCH] DropdownList: Disable item with callback (#5317) * DropdownList | Add DisabledItem func * Demo | DropdownListPage | Add example * Docs | DropdownListPage | Add DisabledItem * Release Notes | DisabledItem * Format --------- Co-authored-by: Mladen Macanovic --- .../Pages/Tests/DropdownListPage.razor | 2 ++ .../DropdownList/DropdownListPage.razor | 3 ++ .../News/2024-02-15-release-notes-150.razor | 8 +++++ .../Blazorise.Components/DropdownList.razor | 11 +++---- .../DropdownList.razor.cs | 29 +++++++++++++++++++ 5 files changed, 48 insertions(+), 5 deletions(-) diff --git a/Demos/Blazorise.Demo/Pages/Tests/DropdownListPage.razor b/Demos/Blazorise.Demo/Pages/Tests/DropdownListPage.razor index af31db46a1..e152a36185 100644 --- a/Demos/Blazorise.Demo/Pages/Tests/DropdownListPage.razor +++ b/Demos/Blazorise.Demo/Pages/Tests/DropdownListPage.razor @@ -15,6 +15,7 @@ Data="@Countries" TextField="@(( item ) => item.Name)" ValueField="@((item) => item.Iso)" + DisabledItem="@(item => item.Name == "Armenia")" @bind-SelectedValue="@selectedDropValue" Color="Color.Primary" MaxMenuHeight="200px" @@ -46,6 +47,7 @@ Data="@Countries" TextField="@(( item ) => item.Name)" ValueField="@((item) => item.Iso)" + DisabledItem="@(item => item.Name == "Armenia")" @bind-SelectedValues="@selectedDropValues" SelectionMode="DropdownListSelectionMode.Checkbox" Color="Color.Primary" diff --git a/Documentation/Blazorise.Docs/Pages/Docs/Extensions/DropdownList/DropdownListPage.razor b/Documentation/Blazorise.Docs/Pages/Docs/Extensions/DropdownList/DropdownListPage.razor index e9aab0bad4..2f03e445b8 100644 --- a/Documentation/Blazorise.Docs/Pages/Docs/Extensions/DropdownList/DropdownListPage.razor +++ b/Documentation/Blazorise.Docs/Pages/Docs/Extensions/DropdownList/DropdownListPage.razor @@ -95,4 +95,7 @@ Defines the size of toggle button. + + Method used to get the disabled items from the supplied data source. + \ No newline at end of file diff --git a/Documentation/Blazorise.Docs/Pages/News/2024-02-15-release-notes-150.razor b/Documentation/Blazorise.Docs/Pages/News/2024-02-15-release-notes-150.razor index f22bb3b149..110ec40871 100644 --- a/Documentation/Blazorise.Docs/Pages/News/2024-02-15-release-notes-150.razor +++ b/Documentation/Blazorise.Docs/Pages/News/2024-02-15-release-notes-150.razor @@ -141,4 +141,12 @@ Now it is possible to show an extra information on the Rating component. By defining the GetTooltip callback you can show the tooltip to indicate what is the rating value that it represents. + + DropdownList + + + + Added a new Parameter DisabledItem. By defining this, you can set the disabled items from the supplied data source. + + \ No newline at end of file diff --git a/Source/Extensions/Blazorise.Components/DropdownList.razor b/Source/Extensions/Blazorise.Components/DropdownList.razor index 7ea5263c23..05271fc652 100644 --- a/Source/Extensions/Blazorise.Components/DropdownList.razor +++ b/Source/Extensions/Blazorise.Components/DropdownList.razor @@ -9,14 +9,14 @@ @if ( Virtualize && Data is ICollection collectionableData ) { - @itemFragment(item) + @itemFragment( item ) } else { @foreach ( var item in Data ?? Enumerable.Empty() ) { - @itemFragment(item) + @itemFragment( item ) } } } @@ -25,10 +25,11 @@ @code { protected RenderFragment itemFragment => item => __builder => { - var text = TextField?.Invoke( item ); - var value = ValueField != null ? ValueField.Invoke( item ) : default; + var text = GetItemText( item ); + var value = GetItemValue( item ); + var disabled = GetItemDisabled( item ); - @text diff --git a/Source/Extensions/Blazorise.Components/DropdownList.razor.cs b/Source/Extensions/Blazorise.Components/DropdownList.razor.cs index 6ec618d7b0..7f9d866abf 100644 --- a/Source/Extensions/Blazorise.Components/DropdownList.razor.cs +++ b/Source/Extensions/Blazorise.Components/DropdownList.razor.cs @@ -79,6 +79,30 @@ public Task Focus( bool scrollToElement = true ) return dropdownToggleRef.Focus( scrollToElement ); } + private string GetItemText( TItem item ) + { + if ( TextField is null ) + return string.Empty; + + return TextField.Invoke( item ); + } + + private TValue GetItemValue( TItem item ) + { + if ( ValueField is null ) + return default; + + return ValueField.Invoke( item ); + } + + private bool GetItemDisabled( TItem item ) + { + if ( DisabledItem is null ) + return false; + + return DisabledItem.Invoke( item ); + } + #endregion #region Properties @@ -195,5 +219,10 @@ protected bool IsSelected( TValue value ) /// [Parameter] public EventCallback> SelectedValuesChanged { get; set; } + /// + /// Method used to get the disabled items from the supplied data source. + /// + [Parameter] public Func DisabledItem { get; set; } + #endregion } \ No newline at end of file