Skip to content

Commit b41f22d

Browse files
authored
MudStaticTextField: Adornment Icon Toggle
* Add ability to toggle the adornment icon
1 parent 613f2b4 commit b41f22d

4 files changed

Lines changed: 38 additions & 4 deletions

File tree

demo/StaticSample/StaticSample/Components/Account/Pages/Login.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
<MudStaticTextField @bind-Value="@Input.Email" Adornment="Adornment.End" AdornmentIcon="@Icons.Material.Outlined.Email" autocomplete="username"
4545
Class="pb-2" Margin="Margin.Dense" Variant="Variant.Outlined" Label="Email" For="() => Input.Email" />
4646

47-
<MudStaticTextField @bind-Value="@Input.Password" Adornment="Adornment.End" AdornmentIcon="@Icons.Material.Outlined.VisibilityOff" AdornmentClickFunction="showPassword"
48-
autocomplete="current-password" Class="pb-2" Margin="Margin.Dense" Variant="Variant.Outlined" Label="Password"
49-
InputType="InputType.Password" For="() => Input.Password" />
47+
<MudStaticTextField @bind-Value="@Input.Password" autocomplete="current-password" Class="pb-2" Margin="Margin.Dense" Variant="Variant.Outlined" Label="Password"
48+
Adornment="Adornment.End" AdornmentIcon="@Icons.Material.Outlined.Visibility" AdornmentToggledIcon="@Icons.Material.Outlined.VisibilityOff"
49+
AdornmentClickFunction="showPassword" InputType="InputType.Password" For="() => Input.Password" />
5050

5151
<MudStack Row="true" AlignItems="AlignItems.Center" Justify="Justify.SpaceBetween">
5252
<MudStaticSwitch @bind-Value="Input.RememberMe" Color="Color.Primary" Label="Remember Me" Typo="Typo.body2"/>

src/Components/MudStaticTextField.razor

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
/// </example>
5353
/// </remarks>
5454
[Parameter] public string? AdornmentClickFunction { get; set; }
55+
[Parameter] public string? AdornmentToggledIcon { get; set; }
5556
[Parameter] public Expression<Func<string>>? ValueExpression { get; set; }
5657

5758
private string _name = string.Empty;
@@ -142,9 +143,21 @@
142143
{
143144
const inputControl = inputElement.closest(".mud-input-control");
144145
const buttonElement = inputControl.querySelector('button');
146+
const svgElement = buttonElement.querySelector("svg");
147+
const icon = '@AdornmentIcon';
148+
const toggleIcon = '@AdornmentToggledIcon';
149+
150+
let isToggled = false;
145151
146152
if (buttonElement) {
147153
buttonElement.addEventListener('click', function (event) {
154+
155+
if (svgElement && toggleIcon)
156+
{
157+
isToggled = !isToggled;
158+
svgElement.innerHTML = isToggled ? toggleIcon : icon;
159+
}
160+
148161
if (typeof window[onAdornmentClick] === 'function') {
149162
window[onAdornmentClick](inputElement, event.target);
150163
}

tests/StaticInput.UnitTests.Viewer/Components/Tests/TextField/TextFieldAdornmentTest.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
Adornment="Adornment.End" AdornmentIcon="@Icons.Material.Outlined.Email"
1010
Class="pb-2" Margin="Margin.Dense" Variant="Variant.Outlined" />
1111
<MudStaticTextField @bind-Value="@Model.Password" For="() => Model.Password" Label="Password" Placeholder="Password"
12-
Adornment="Adornment.End" AdornmentIcon="@Icons.Material.Outlined.VisibilityOff"
12+
Adornment="Adornment.End" AdornmentIcon="@Icons.Material.Outlined.Visibility"
13+
AdornmentToggledIcon="@Icons.Material.Outlined.VisibilityOff"
1314
AdornmentClickFunction="handleAdornmentClick" InputType="InputType.Password"
1415
Class="pb-2" Margin="Margin.Dense" Variant="Variant.Outlined" />
1516
</EditForm>

tests/StaticInput.UnitTests/Components/TextFieldTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,5 +226,25 @@ public async Task TextField_AdornmentButton_IsClickable()
226226

227227
message.Should().Be("Adornment clicked!");
228228
}
229+
230+
[Fact]
231+
public async Task TextField_AdornmentButton_CanToggle()
232+
{
233+
var url = typeof(TextFieldAdornmentTest).ToQueryString();
234+
235+
await Page.GotoAsync(url);
236+
237+
var button = await Page.QuerySelectorAsync("input[type=\"password\"] ~ .mud-input-adornment .mud-icon-button");
238+
var innerHtml = await button!.InnerHTMLAsync();
239+
240+
innerHtml.Should().Contain("<path d=\"M0 0h24v24H0V0z\" fill=\"none\"></path>");
241+
242+
await button.ClickAsync();
243+
244+
button = await Page.QuerySelectorAsync("input[type=\"password\"] ~ .mud-input-adornment .mud-icon-button");
245+
innerHtml = await button!.InnerHTMLAsync();
246+
247+
innerHtml.Should().Contain("<path d=\"M0 0h24v24H0V0zm0 0h24v24H0V0zm0 0h24v24H0V0zm0 0h24v24H0V0z\" fill=\"none\"></path>");
248+
}
229249
}
230250
}

0 commit comments

Comments
 (0)