2020-03-25 00:32:26 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2020-03-25 00:32:26 +08:00
|
|
|
|
{
|
2020-06-29 23:19:25 +08:00
|
|
|
|
public partial class Radio<TValue> : AntDomComponentBase
|
2020-03-25 00:32:26 +08:00
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment ChildContent { get; set; }
|
2020-03-25 00:32:26 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
[Parameter]
|
2020-06-29 23:19:25 +08:00
|
|
|
|
public TValue Value { get; set; }
|
2020-03-25 00:32:26 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool AutoFocus { get; set; } = false;
|
2020-03-25 00:32:26 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool RadioButton { get; set; }
|
2020-03-25 00:32:26 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
[Parameter]
|
2020-06-05 16:06:23 +08:00
|
|
|
|
public bool Checked { get => _checked ?? false; set { _checked = value; } }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<bool> CheckedChanged { get; set; }
|
2020-04-23 17:13:56 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Disabled { get; set; }
|
2020-03-25 00:32:26 +08:00
|
|
|
|
|
2020-06-05 16:06:23 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<bool> CheckedChange { get; set; }
|
|
|
|
|
|
2020-06-29 23:19:25 +08:00
|
|
|
|
[CascadingParameter] public RadioGroup<TValue> RadioGroup { get; set; }
|
2020-03-25 00:32:26 +08:00
|
|
|
|
|
|
|
|
|
protected ClassMapper RadioClassMapper { get; set; } = new ClassMapper();
|
|
|
|
|
|
|
|
|
|
protected ClassMapper InputClassMapper { get; set; } = new ClassMapper();
|
|
|
|
|
|
|
|
|
|
protected ClassMapper InnerClassMapper { get; set; } = new ClassMapper();
|
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
protected ElementReference InputRef { get; set; }
|
2020-03-25 00:32:26 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
protected bool IsChecked => _checked ?? this.Checked;
|
2020-03-25 00:32:26 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
private bool? _checked;
|
2020-03-25 00:32:26 +08:00
|
|
|
|
|
2020-04-23 17:13:56 +08:00
|
|
|
|
internal string _name;
|
2020-03-25 00:32:26 +08:00
|
|
|
|
|
|
|
|
|
protected void SetClass()
|
|
|
|
|
{
|
|
|
|
|
string prefixCls = "ant-radio";
|
|
|
|
|
ClassMapper.Clear()
|
|
|
|
|
.If($"{prefixCls}-wrapper", () => !RadioButton)
|
|
|
|
|
.If($"{prefixCls}-button-wrapper", () => RadioButton)
|
2020-04-23 17:13:56 +08:00
|
|
|
|
.If($"{prefixCls}-wrapper-checked", () => IsChecked && !RadioButton)
|
|
|
|
|
.If($"{prefixCls}-button-wrapper-checked", () => IsChecked && RadioButton)
|
|
|
|
|
.If($"{prefixCls}-wrapper-disabled", () => Disabled && !RadioButton)
|
|
|
|
|
.If($"{prefixCls}-button-wrapper-disabled", () => Disabled && RadioButton);
|
2020-03-25 00:32:26 +08:00
|
|
|
|
|
|
|
|
|
RadioClassMapper.Clear()
|
|
|
|
|
.If(prefixCls, () => !RadioButton)
|
2020-04-23 17:13:56 +08:00
|
|
|
|
.If($"{prefixCls}-checked", () => IsChecked && !RadioButton)
|
|
|
|
|
.If($"{prefixCls}-disabled", () => Disabled && !RadioButton)
|
2020-03-25 00:32:26 +08:00
|
|
|
|
.If($"{prefixCls}-button", () => RadioButton)
|
2020-04-23 17:13:56 +08:00
|
|
|
|
.If($"{prefixCls}-button-checked", () => IsChecked && RadioButton)
|
|
|
|
|
.If($"{prefixCls}-button-disabled", () => Disabled && RadioButton);
|
2020-03-25 00:32:26 +08:00
|
|
|
|
|
|
|
|
|
InputClassMapper.Clear()
|
|
|
|
|
.If($"{prefixCls}-input", () => !RadioButton)
|
|
|
|
|
.If($"{prefixCls}-button-input", () => RadioButton);
|
|
|
|
|
|
|
|
|
|
InnerClassMapper.Clear()
|
|
|
|
|
.If($"{prefixCls}-inner", () => !RadioButton)
|
|
|
|
|
.If($"{prefixCls}-button-inner", () => RadioButton);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
SetClass();
|
2020-06-29 23:19:25 +08:00
|
|
|
|
RadioGroup?.AddRadio(this);
|
2020-03-25 00:32:26 +08:00
|
|
|
|
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task OnFirstAfterRenderAsync()
|
|
|
|
|
{
|
|
|
|
|
if (this.AutoFocus)
|
|
|
|
|
{
|
|
|
|
|
await this.Focus();
|
|
|
|
|
}
|
2020-06-29 23:19:25 +08:00
|
|
|
|
|
2020-03-25 00:32:26 +08:00
|
|
|
|
await base.OnFirstAfterRenderAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal async Task Select()
|
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (!Disabled && !IsChecked)
|
2020-03-25 00:32:26 +08:00
|
|
|
|
{
|
|
|
|
|
this._checked = true;
|
2020-06-05 16:06:23 +08:00
|
|
|
|
await CheckedChange.InvokeAsync(true);
|
|
|
|
|
await CheckedChanged.InvokeAsync(true);
|
2020-03-25 00:32:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal async Task UnSelect()
|
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
if (this.IsChecked)
|
2020-03-25 00:32:26 +08:00
|
|
|
|
{
|
|
|
|
|
this._checked = false;
|
2020-06-05 16:06:23 +08:00
|
|
|
|
await CheckedChange.InvokeAsync(false);
|
|
|
|
|
await CheckedChanged.InvokeAsync(false);
|
2020-03-25 00:32:26 +08:00
|
|
|
|
}
|
|
|
|
|
await Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
public async Task OnClick()
|
2020-03-25 00:32:26 +08:00
|
|
|
|
{
|
2020-06-29 23:19:25 +08:00
|
|
|
|
if (RadioGroup != null)
|
|
|
|
|
{
|
|
|
|
|
await RadioGroup.OnRadioChange(this.Value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await Select();
|
|
|
|
|
}
|
2020-03-25 00:32:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async Task Focus()
|
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
await JsInvokeAsync(JSInteropConstants.focus, this.InputRef);
|
2020-03-25 00:32:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async Task Blur()
|
|
|
|
|
{
|
2020-04-23 17:13:56 +08:00
|
|
|
|
await JsInvokeAsync(JSInteropConstants.blur, this.InputRef);
|
2020-03-25 00:32:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-23 17:13:56 +08:00
|
|
|
|
}
|