Skip to content

Commit

Permalink
Added ReadOnly parameter for RadzenSwitch component (#1927)
Browse files Browse the repository at this point in the history
* Added ReadOnly parameter for RadzenSwitch component

* Formatting
  • Loading branch information
FrankNWB authored Jan 29, 2025
1 parent d4113e6 commit 3e74681
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Radzen.Blazor.Tests/SwitchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,18 @@ public void Switch_Raises_ValueChangedEvent()
Assert.True(raised);
Assert.True(object.Equals(value, !(bool)newValue));
}

[Fact]
public void Switch_Renders_ReadOnlyParameter()
{
using var ctx = new TestContext();

var component = ctx.RenderComponent<RadzenSwitch>();

component.SetParametersAndRender(parameters => parameters.Add<bool>(p => p.ReadOnly, true));

Assert.Contains(@$"rz-readonly", component.Markup);
}

}
}
2 changes: 1 addition & 1 deletion Radzen.Blazor/RadzenSwitch.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<div class="rz-helper-hidden-accessible">
<input type="checkbox" name="@Name" id="@Name" checked="@Value" value="@ValueAsString" tabindex="-1" aria-checked="@(Value.ToString().ToLowerInvariant())" @attributes=@InputAttributes>
</div>
<span class="rz-switch-circle@(Disabled ? " rz-disabled" : "")"></span>
<span class="rz-switch-circle@(Disabled ? " rz-disabled" : ReadOnly ? " rz-readonly" : "")"></span>
</div>
}
9 changes: 8 additions & 1 deletion Radzen.Blazor/RadzenSwitch.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ namespace Radzen.Blazor
/// </example>
public partial class RadzenSwitch : FormComponent<bool>
{
/// <summary>
/// Gets or sets a value indicating whether is read only.
/// </summary>
/// <value><c>true</c> if is read only; otherwise, <c>false</c>.</value>
[Parameter]
public bool ReadOnly { get; set; }

/// <inheritdoc />
protected override string GetComponentCssClass()
{
Expand All @@ -35,7 +42,7 @@ protected override string GetComponentCssClass()
/// </summary>
public async System.Threading.Tasks.Task Toggle()
{
if (Disabled)
if (Disabled || ReadOnly)
{
return;
}
Expand Down
4 changes: 4 additions & 0 deletions Radzen.Blazor/themes/components/blazor/_switch.scss
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ $switch-focus-outline-offset: var(--rz-outline-offset) !default;
cursor: initial;
}

.rz-switch-circle.rz-readonly {
cursor: initial;
}

.rz-switch .rz-switch-circle {
background: var(--rz-switch-background-color);
transition: background-color 0.2s, color 0.2s, border-color 0.2s, box-shadow 0.2s;
Expand Down

0 comments on commit 3e74681

Please sign in to comment.