Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0] API refactor: extension components #5878

Draft
wants to merge 21 commits into
base: next-2.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Convert DropDownList to use Value parameter
  • Loading branch information
stsrki committed Dec 4, 2024
commit 6ac5c1d1888564a57140829195f77e7d5c529e91
2 changes: 1 addition & 1 deletion Source/Extensions/Blazorise.Components/DropdownList.razor
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<DropdownItem @key="@item" Clicked="@HandleDropdownItemClicked" Value="@value" Disabled="@disabled"
ShowCheckbox="@(SelectionMode == DropdownListSelectionMode.Checkbox)"
Checked="IsSelected(value)"
Checked="@IsSelected(value)"
CheckedChanged="@((isChecked) => HandleDropdownItemChecked(isChecked, value))">@text</DropdownItem>
};
}
103 changes: 63 additions & 40 deletions Source/Extensions/Blazorise.Components/DropdownList.razor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#region Using directives
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -29,44 +30,38 @@ public partial class DropdownList<TItem, TValue> : ComponentBase
/// </summary>
protected DropdownToggle dropdownToggleRef;

private List<TValue> selectedValues;

#endregion

#region Methods

protected override void OnInitialized()
{
selectedValues = SelectedValues?.ToList();
base.OnInitialized();
}

public override async Task SetParametersAsync( ParameterView parameters )
protected Task HandleDropdownItemClicked( object value )
{
var selectedValuesChanged = parameters.TryGetValue<IReadOnlyList<TValue>>( nameof( SelectedValues ), out var paramSelectedValues ) && !paramSelectedValues.AreEqual( SelectedValues );

await base.SetParametersAsync( parameters );
if ( Multiple )
return Task.CompletedTask;

if ( selectedValuesChanged )
selectedValues = paramSelectedValues?.ToList();
return ValueChanged.InvokeAsync( Converters.ChangeType<TValue>( value ) );
}

protected Task HandleDropdownItemClicked( object value )
protected Task HandleDropdownItemChecked( bool isChecked, object fieldValue )
{
SelectedValue = Converters.ChangeType<TValue>( value );
return SelectedValueChanged.InvokeAsync( SelectedValue );
}
if ( !Multiple )
return Task.CompletedTask;

protected Task HandleDropdownItemChecked( bool isChecked, TValue value )
{
selectedValues ??= new();
List<object> selectedValues;

if ( Value is IEnumerable<TValue> values )
selectedValues = values.Select( x => (object)x ).ToList();
else if ( Value is IEnumerable objects && Value is not string )
selectedValues = objects.Cast<object>().ToList();
else
selectedValues = new List<object>();

if ( isChecked )
selectedValues.Add( value );
selectedValues.Add( fieldValue );
else
selectedValues.Remove( value );
selectedValues.Remove( fieldValue );

return SelectedValuesChanged.InvokeAsync( selectedValues );
return ValueChanged.InvokeAsync( Converters.ConvertListToReadOnlyList<TValue>( selectedValues ) );
}

/// <summary>
Expand All @@ -87,7 +82,7 @@ private string GetItemText( TItem item )
return TextField.Invoke( item );
}

private TValue GetItemValue( TItem item )
private object GetItemValue( TItem item )
{
if ( ValueField is null )
return default;
Expand All @@ -103,15 +98,53 @@ private bool GetItemDisabled( TItem item )
return DisabledItem.Invoke( item );
}

/// <summary>
/// Check if the internal value is same as the new value.
/// </summary>
/// <param name="value">Value to check against the internal value.</param>
/// <returns>True if the internal value matched the supplied value.</returns>
protected bool IsSameAsInternalValue( TValue value )
{
if ( value is IEnumerable<TValue> values1 && Value is IEnumerable<TValue> values2 )
{
return values1.AreEqual( values2 );
}
else if ( value is IEnumerable objects1 && Value is IEnumerable objects2 )
{
return objects1.AreEqual( objects2 );
}

return value.IsEqual( Value );
}

#endregion

#region Properties

/// <summary>
/// Indicates if the multiple items can be selected.
/// </summary>
protected bool Multiple => SelectionMode == DropdownListSelectionMode.Checkbox;

/// <summary>
/// Whether the value is currently selected.
/// </summary>
protected bool IsSelected( TValue value )
=> selectedValues?.Any( x => x.Equals( value ) ) ?? false;
protected bool IsSelected( object value )
{
if ( Value is null )
return false;

if ( Value is IEnumerable<TValue> values )
{
return values.Any( x => x.IsEqual( value ) );
}
else if ( Value is IEnumerable objects && Value is not string )
{
return objects.Cast<object>().Any( x => x.IsEqual( value ) );
}

return Value.IsEqual( value );
}

/// <summary>
/// Gets or sets the dropdown element id.
Expand Down Expand Up @@ -156,17 +189,17 @@ protected bool IsSelected( TValue value )
/// <summary>
/// Method used to get the value field from the supplied data source.
/// </summary>
[Parameter] public Func<TItem, TValue> ValueField { get; set; }
[Parameter] public Func<TItem, object> ValueField { get; set; }

/// <summary>
/// Currently selected item value.
/// </summary>
[Parameter] public TValue SelectedValue { get; set; }
[Parameter] public TValue Value { get; set; }

/// <summary>
/// Occurs after the selected value has changed.
/// </summary>
[Parameter] public EventCallback<TValue> SelectedValueChanged { get; set; }
[Parameter] public EventCallback<TValue> ValueChanged { get; set; }

/// <summary>
/// Custom classname for dropdown element.
Expand Down Expand Up @@ -209,16 +242,6 @@ protected bool IsSelected( TValue value )
/// </summary>
[Parameter] public DropdownListSelectionMode SelectionMode { get; set; } = DropdownListSelectionMode.Default;

/// <summary>
/// Currently selected item values.
/// </summary>
[Parameter] public IReadOnlyList<TValue> SelectedValues { get; set; }

/// <summary>
/// Occurs after the selected item values have changed.
/// </summary>
[Parameter] public EventCallback<IReadOnlyList<TValue>> SelectedValuesChanged { get; set; }

/// <summary>
/// Method used to get the disabled items from the supplied data source.
/// </summary>
Expand Down