using System.Windows.Input;
using Connected.Annotations;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using static System.String;
namespace Connected.Components;
public abstract class ButtonBase : UIComponent
{
///
/// Potential activation target for this button. This enables RenderFragments with user-defined
/// buttons which will automatically activate the intended functionality.
///
[CascadingParameter] protected IActivatable Activateable { get; set; }
///
/// The HTML element that will be rendered in the root by the component
/// By default, is a button
///
[Parameter]
[Category(CategoryTypes.Button.ClickAction)]
public string HtmlTag { get; set; } = "button";
///
/// The button Type (Button, Submit, Refresh)
///
[Parameter]
[Category(CategoryTypes.Button.ClickAction)]
public ButtonType ButtonType { get; set; }
///
/// If set to a URL, clicking the button will open the referenced document. Use Target to specify where
///
[Parameter]
[Category(CategoryTypes.Button.ClickAction)]
public string Href { get; set; }
///
/// If set to a URL, clicking the button will open the referenced document. Use Target to specify where (Obsolete replaced by Href)
///
[Obsolete("Use Href Instead.", false)]
[Parameter]
[Category(CategoryTypes.Button.ClickAction)]
public string Link
{
get => Href;
set => Href = value;
}
///
/// The target attribute specifies where to open the link, if Link is specified. Possible values: _blank | _self | _parent | _top | framename
///
[Parameter]
[Category(CategoryTypes.Button.ClickAction)]
public string Target { get; set; }
///
/// If true, the button will be disabled.
///
[Parameter]
[Category(CategoryTypes.Button.Behavior)]
public bool Disabled { get; set; }
///
/// If true, no drop-shadow will be used.
///
[Parameter]
[Category(CategoryTypes.Button.Appearance)]
public bool DisableElevation { get; set; }
///
/// If true, disables ripple effect.
///
[Parameter]
[Category(CategoryTypes.Button.Appearance)]
public bool DisableRipple { get; set; }
///
/// Command executed when the user clicks on an element.
///
[Parameter]
[Category(CategoryTypes.Button.ClickAction)]
public ICommand Command { get; set; }
///
/// Command parameter.
///
[Parameter]
[Category(CategoryTypes.Button.ClickAction)]
public object CommandParameter { get; set; }
///
/// Button click event.
///
[Parameter]
public EventCallback OnClick { get; set; }
protected async Task OnClickHandler(MouseEventArgs ev)
{
if (Disabled)
return;
await OnClick.InvokeAsync(ev);
if (Command?.CanExecute(CommandParameter) ?? false)
{
Command.Execute(CommandParameter);
}
Activateable?.Activate(this, ev);
}
protected override void OnInitialized()
{
SetDefaultValues();
}
protected override void OnParametersSet()
{
//if params change, must set default values again
SetDefaultValues();
}
//Set the default value for HtmlTag, Link and Target
private void SetDefaultValues()
{
if (Disabled)
{
HtmlTag = "button";
Href = null;
Target = null;
return;
}
// Render an anchor element if Link property is set and is not disabled
if (!IsNullOrWhiteSpace(Href))
{
HtmlTag = "a";
}
}
protected ElementReference _elementReference;
public ValueTask FocusAsync() => _elementReference.FocusAsync();
}