-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): add switch component (#31)
* feat(components): initial implementation of the switch component * chore(components): update the size styles of the switch component * chore(components): set the size for the icons of the switch component * chore(components): styles tweaks * feat(components): derive the switch component from the boolean input base * feat(components): derive the checkbox component from the boolean input base * test(components): add tests for the switch component
- Loading branch information
1 parent
b35d174
commit c31e296
Showing
9 changed files
with
537 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
using Microsoft.AspNetCore.Components; | ||
|
||
namespace LumexUI; | ||
|
||
public abstract class LumexBooleanInputBase : LumexInputBase<bool> | ||
{ | ||
/// <summary> | ||
/// Gets or sets content to be rendered inside the input. | ||
/// </summary> | ||
[Parameter] public RenderFragment? ChildContent { get; set; } | ||
|
||
/// <summary> | ||
/// Gets the disabled state of the input. | ||
/// Derived classes can override this to determine the input's disabled state. | ||
/// </summary> | ||
/// <returns>A <see cref="bool"/> value indicating whether the input is disabled.</returns> | ||
protected internal virtual bool GetDisabledState() => Disabled; | ||
|
||
/// <summary> | ||
/// Gets the readonly state of the input. | ||
/// Derived classes can override this to determine the input's readonly state. | ||
/// </summary> | ||
/// <returns>A <see cref="bool"/> value indicating whether the input is readonly.</returns> | ||
protected internal virtual bool GetReadOnlyState() => ReadOnly; | ||
|
||
/// <inheritdoc /> | ||
protected override bool TryParseValueFromString( string? value, [MaybeNullWhen( false )] out bool result ) | ||
{ | ||
throw new NotSupportedException( | ||
$"This component does not parse string inputs. " + | ||
$"Bind to the '{nameof( CurrentValue )}' property, not '{nameof( CurrentValueAsString )}'." ); | ||
} | ||
|
||
/// <summary> | ||
/// Handles the change event asynchronously. | ||
/// Derived classes can override this to specify custom behavior when the input's value changes. | ||
/// </summary> | ||
/// <param name="args">The change event arguments.</param> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> | ||
protected virtual Task OnChangeAsync( ChangeEventArgs args ) | ||
{ | ||
if( GetDisabledState() || GetReadOnlyState() ) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
return SetCurrentValueAsync( (bool)args.Value! ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
@namespace LumexUI | ||
@inherits LumexBooleanInputBase | ||
|
||
<LumexComponent As="label" | ||
Class="@RootClass" | ||
Style="@RootStyle" | ||
data-checked="@CurrentValue" | ||
data-disabled="@GetDisabledState()" | ||
data-readonly="@GetReadOnlyState()" | ||
tabindex="0" | ||
@attributes="@AdditionalAttributes"> | ||
<span class="@Utils.VisuallyHidden"> | ||
<input type="checkbox" | ||
value="@true" | ||
checked="@CurrentValue" | ||
disabled="@GetDisabledState()" | ||
@ref="@Element" | ||
@attributes="@AdditionalAttributes" | ||
@onchange="@OnChangeAsync" /> | ||
</span> | ||
|
||
<span class="@WrapperClass"> | ||
@if( !string.IsNullOrEmpty( StartIcon ) ) | ||
{ | ||
<LumexIcon Icon="@StartIcon" Size="@(new("100%"))" Class="@StartIconClass" /> | ||
} | ||
|
||
<span class="@ThumbClass"> | ||
@if( !string.IsNullOrEmpty( ThumbIcon ) ) | ||
{ | ||
<LumexIcon Icon="@ThumbIcon" Size="@(new("100%"))" Class="@ThumbIconClass" /> | ||
} | ||
</span> | ||
|
||
@if( !string.IsNullOrEmpty( EndIcon ) ) | ||
{ | ||
<LumexIcon Icon="@EndIcon" Size="@(new("100%"))" Class="@EndIconClass" /> | ||
} | ||
</span> | ||
|
||
@if( ChildContent is not null ) | ||
{ | ||
<span class="@LabelClass"> | ||
@ChildContent | ||
</span> | ||
} | ||
</LumexComponent> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) LumexUI 2024 | ||
// LumexUI licenses this file to you under the MIT license | ||
// See the license here https://github.com/LumexUI/lumexui/blob/main/LICENSE | ||
|
||
using LumexUI.Common; | ||
using LumexUI.Styles; | ||
|
||
using Microsoft.AspNetCore.Components; | ||
|
||
namespace LumexUI; | ||
|
||
public partial class LumexSwitch : LumexBooleanInputBase, ISlotComponent<SwitchSlots> | ||
{ | ||
/// <summary> | ||
/// Gets or sets the icon to be used for indicating a checked state of the switch. | ||
/// </summary> | ||
[Parameter] public string? ThumbIcon { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the icon to be rendered before the switch. | ||
/// </summary> | ||
[Parameter] public string? StartIcon { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the icon to be rendered after the switch. | ||
/// </summary> | ||
[Parameter] public string? EndIcon { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the CSS class names for the switch slots. | ||
/// </summary> | ||
[Parameter] public SwitchSlots? Classes { get; set; } | ||
|
||
private protected override string? RootClass => | ||
TwMerge.Merge( Switch.GetStyles( this ) ); | ||
|
||
private string? WrapperClass => | ||
TwMerge.Merge( Switch.GetWrapperStyles( this ) ); | ||
|
||
private string? ThumbClass => | ||
TwMerge.Merge( Switch.GetThumbStyles( this ) ); | ||
|
||
private string? ThumbIconClass => | ||
TwMerge.Merge( Switch.GetThumbIconStyles( this ) ); | ||
|
||
private string? StartIconClass => | ||
TwMerge.Merge( Switch.GetStartIconStyles( this ) ); | ||
|
||
private string? EndIconClass => | ||
TwMerge.Merge( Switch.GetEndIconStyles( this ) ); | ||
|
||
private string? LabelClass => | ||
TwMerge.Merge( Switch.GetLabelStyles( this ) ); | ||
|
||
public LumexSwitch() | ||
{ | ||
Color = ThemeColor.Primary; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
using LumexUI.Common; | ||
|
||
namespace LumexUI; | ||
|
||
[ExcludeFromCodeCoverage] | ||
public class SwitchSlots : ISlot | ||
{ | ||
public string? Root { get; set; } | ||
public string? Wrapper { get; set; } | ||
public string? Thumb { get; set; } | ||
public string? StartIcon { get; set; } | ||
public string? EndIcon { get; set; } | ||
public string? ThumbIcon { get; set; } | ||
public string? Label { get; set; } | ||
} |
Oops, something went wrong.