Skip to content

Commit fdcd35b

Browse files
Expose OnFocus event on MatButton. Closes #314
- implementation change - example code update Thanks!
1 parent 9e991da commit fdcd35b

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

src/MatBlazor.Demo/Demo/DemoMatButton.razor

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@using Microsoft.AspNetCore.Components
1010

1111
<h4 class="mat-subtitle1">Simple use</h4>
12-
<MatButton Icon="favorite" OnClick="@RunOnClick" Label="Simple Button"></MatButton>
12+
<MatButton Icon="favorite" OnClick="@RunOnClick" Label="Simple Button" OnFocus="@RunOnFocus"></MatButton>
1313

1414
<h5 class="mat-subtitle1">With Font-Awsome Icons and Link</h5>
1515
<MatButton Link="https://github.com/BlazorComponents/MatBlazor">
@@ -24,6 +24,11 @@
2424
JsRuntime.InvokeAsync<object>("window.alert", "Test");
2525
}
2626

27+
public void RunOnFocus(FocusEventArgs e)
28+
{
29+
Console.WriteLine(e.Type);
30+
}
31+
2732
}
2833

2934
</Content>
@@ -32,7 +37,7 @@
3237
@using Microsoft.AspNetCore.Components
3338
3439
<h4 class=""mat-subtitle1"">Simple use</h4>
35-
<MatButton Icon=""favorite"" OnClick=""@RunOnClick"" Label=""Simple Button""></MatButton>
40+
<MatButton Icon=""favorite"" OnClick=""@RunOnClick"" OnFocus=""@RunOnFocus"" Label=""Simple Button""></MatButton>
3641
3742
<h5 class=""mat-subtitle1"">With Font-Awsome Icons and Link</h5>
3843
<MatButton Link=""https://github.com/BlazorComponents/MatBlazor"">
@@ -47,6 +52,11 @@
4752
JsRuntime.InvokeAsync<object>(""window.alert"", ""Test"");
4853
}
4954
55+
public void RunOnFocus(FocusEventArgs e)
56+
{
57+
Console.WriteLine(e.Type);
58+
}
59+
5060
}
5161
5262
")></BlazorFiddle>

src/MatBlazor.Demo/Doc/DocMatButton.razor

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@
7979
<td>EventCallback&lt;MouseEventArgs&gt;</td>
8080
<td>Event occurs when the user clicks on an element.</td>
8181
</tr>
82+
<tr>
83+
<td>OnFocus</td>
84+
<td>EventCallback&lt;FocusEventArgs&gt;</td>
85+
<td>Event occurs when the user focuses on an element.</td>
86+
</tr>
8287
<tr>
8388
<td>OnClickStopPropagation</td>
8489
<td>Boolean</td>

src/MatBlazor/Components/MatButton/BaseMatButton.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public BaseMatButton()
4343
[Parameter]
4444
public bool OnClickStopPropagation { get; set; }
4545

46+
/// <summary>
47+
/// Event occurs when the user focuses on an element
48+
/// </summary>
49+
[Parameter]
50+
public EventCallback<FocusEventArgs> OnFocus { get; set; }
51+
4652
/// <summary>
4753
/// Command executed when the user clicks on an element.
4854
/// </summary>
@@ -197,6 +203,15 @@ protected void OnClickHandler(MouseEventArgs ev)
197203
}
198204
}
199205

206+
/// <summary>
207+
/// Event handler for focus event
208+
/// </summary>
209+
/// <param name="ev"></param>
210+
protected void OnFocusHandler(FocusEventArgs ev)
211+
{
212+
OnFocus.InvokeAsync(ev);
213+
}
214+
200215
private bool _raised;
201216
private bool _unelevated;
202217
private bool _outlined;

src/MatBlazor/Components/MatButton/MatButton.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@namespace MatBlazor
22
@inherits BaseMatButton
33

4-
<button class="@ClassMapper.AsString()" style="@StyleMapper.AsString()" @onclick="OnClickHandler" @onclick:stopPropagation=@OnClickStopPropagation disabled=@Disabled aria-label="@(Label ?? Icon)" @ref="Ref" type="@Type" name="@Name" value="@Value" @attributes="Attributes" Id="@Id">
4+
<button class="@ClassMapper.AsString()" style="@StyleMapper.AsString()" @onclick="OnClickHandler" @onclick:stopPropagation=@OnClickStopPropagation @onfocus="OnFocusHandler" disabled=@Disabled aria-label="@(Label ?? Icon)" @ref="Ref" type="@Type" name="@Name" value="@Value" @attributes="Attributes" Id="@Id">
55
<div class="mdc-button__ripple"></div>
66
@if (Icon != null)
77
{

src/MatBlazor/MatBlazor.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)