Skip to content

Commit

Permalink
Expose OnFocus event on MatButton. Closes #314
Browse files Browse the repository at this point in the history
- implementation change
- example code update

Thanks!
  • Loading branch information
peterblazejewicz committed Apr 2, 2020
1 parent 9e991da commit fdcd35b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/MatBlazor.Demo/Demo/DemoMatButton.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@using Microsoft.AspNetCore.Components

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

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

public void RunOnFocus(FocusEventArgs e)
{
Console.WriteLine(e.Type);
}

}

</Content>
Expand All @@ -32,7 +37,7 @@
@using Microsoft.AspNetCore.Components
<h4 class=""mat-subtitle1"">Simple use</h4>
<MatButton Icon=""favorite"" OnClick=""@RunOnClick"" Label=""Simple Button""></MatButton>
<MatButton Icon=""favorite"" OnClick=""@RunOnClick"" OnFocus=""@RunOnFocus"" Label=""Simple Button""></MatButton>
<h5 class=""mat-subtitle1"">With Font-Awsome Icons and Link</h5>
<MatButton Link=""https://github.com/BlazorComponents/MatBlazor"">
Expand All @@ -47,6 +52,11 @@
JsRuntime.InvokeAsync<object>(""window.alert"", ""Test"");
}
public void RunOnFocus(FocusEventArgs e)
{
Console.WriteLine(e.Type);
}
}
")></BlazorFiddle>
Expand Down
5 changes: 5 additions & 0 deletions src/MatBlazor.Demo/Doc/DocMatButton.razor
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
<td>EventCallback&lt;MouseEventArgs&gt;</td>
<td>Event occurs when the user clicks on an element.</td>
</tr>
<tr>
<td>OnFocus</td>
<td>EventCallback&lt;FocusEventArgs&gt;</td>
<td>Event occurs when the user focuses on an element.</td>
</tr>
<tr>
<td>OnClickStopPropagation</td>
<td>Boolean</td>
Expand Down
15 changes: 15 additions & 0 deletions src/MatBlazor/Components/MatButton/BaseMatButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public BaseMatButton()
[Parameter]
public bool OnClickStopPropagation { get; set; }

/// <summary>
/// Event occurs when the user focuses on an element
/// </summary>
[Parameter]
public EventCallback<FocusEventArgs> OnFocus { get; set; }

/// <summary>
/// Command executed when the user clicks on an element.
/// </summary>
Expand Down Expand Up @@ -197,6 +203,15 @@ protected void OnClickHandler(MouseEventArgs ev)
}
}

/// <summary>
/// Event handler for focus event
/// </summary>
/// <param name="ev"></param>
protected void OnFocusHandler(FocusEventArgs ev)
{
OnFocus.InvokeAsync(ev);
}

private bool _raised;
private bool _unelevated;
private bool _outlined;
Expand Down
2 changes: 1 addition & 1 deletion src/MatBlazor/Components/MatButton/MatButton.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@namespace MatBlazor
@inherits BaseMatButton

<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">
<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">
<div class="mdc-button__ripple"></div>
@if (Icon != null)
{
Expand Down
11 changes: 11 additions & 0 deletions src/MatBlazor/MatBlazor.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fdcd35b

Please sign in to comment.