// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; namespace AntDesign { public partial class Button : AntDomComponentBase { private string _formSize; public static readonly int RemoveAnimationAfter = 500; [CascadingParameter(Name = "FormSize")] public string FormSize { get { return _formSize; } set { _formSize = value; Size = value; } } /// /// Sets the value of the aria-label attribute /// [Parameter] public string AriaLabel { get; set; } /// /// Set the color of the button. /// [Parameter] public Color Color { get; set; } = Color.None; /// /// Option to fit button width to its parent width /// [Parameter] public bool Block { get; set; } = false; /// /// Content of the button. /// [Parameter] public RenderFragment ChildContent { get; set; } /// /// Set the danger status of button. /// [Parameter] public bool Danger { get; set; } /// /// Whether the `Button` is disabled. /// [Parameter] public bool Disabled { get; set; } /// /// Make background transparent and invert text and border colors /// [Parameter] public bool Ghost { get; set; } = false; /// /// Set the original html type of the button element. /// [Parameter] public string HtmlType { get; set; } = "button"; /// /// Set the icon component of button. /// [Parameter] public string Icon { get; set; } /// /// Show loading indicator. You have to write the loading logic on your own. /// [Parameter] public bool Loading { get; set; } /// /// Callback when `Button` is clicked /// [Parameter] public EventCallback OnClick { get; set; } /// /// Do not propagate events when button is clicked. /// [Parameter] public bool OnClickStopPropagation { get; set; } /// /// Can set button shape: `circle` | `round` or `null` (default, which is rectangle). /// [Parameter] public string Shape { get; set; } = null; /// /// Set the size of button. /// [Parameter] public string Size { get; set; } = AntSizeLDSType.Default; /// /// Type of the button. /// [Parameter] public string Type { get; set; } = ButtonType.Default; /// /// Do not wrap with <span> /// [Parameter] public bool NoSpanWrap { get; set; } public IList Icons { get; set; } = new List(); private bool _animating = false; private string _btnWave = "--antd-wave-shadow-color: rgb(255, 120, 117);"; protected void SetClassMap() { var prefixName = "ant-btn"; ClassMapper.Clear() .Add(prefixName) .GetIf(() => $"{prefixName}-{this.Type}", () => !string.IsNullOrEmpty(Type)) .If($"{prefixName}-dangerous", () => Danger) .GetIf(() => $"{prefixName}-{Shape}", () => !string.IsNullOrEmpty(Shape)) .If($"{prefixName}-lg", () => Size == "large") .If($"{prefixName}-sm", () => Size == "small") .If($"{prefixName}-loading", () => Loading) .If($"{prefixName}-icon-only", () => !string.IsNullOrEmpty(this.Icon) && this.ChildContent == null) .If($"{prefixName}-background-ghost", () => Ghost) .If($"{prefixName}-block", () => this.Block) .If($"{prefixName}-rtl", () => RTL) ; } protected override void OnInitialized() { base.OnInitialized(); SetClassMap(); SetButtonColorStyle(); } private async Task HandleOnClick(MouseEventArgs args) { if (Loading) return; if (OnClick.HasDelegate) { await OnClick.InvokeAsync(args); } } private async Task OnMouseUp(MouseEventArgs args) { if (args.Button != 0 || this.Type == ButtonType.Link) return; //remove animating from Link Button this._animating = true; await Task.Delay(RemoveAnimationAfter); this._animating = false; await InvokeAsync(StateHasChanged); } private void SetButtonColorStyle() { if (Color != Color.None) { Style += ColorHelper.GetBackgroundStyle(Color); } } } }