2020-03-25 00:32:26 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2020-03-25 00:32:26 +08:00
|
|
|
|
{
|
2020-05-28 16:56:52 +08:00
|
|
|
|
public partial class Radio : 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]
|
|
|
|
|
public string 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]
|
|
|
|
|
public bool Checked { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Disabled { get; set; }
|
2020-03-25 00:32:26 +08:00
|
|
|
|
|
2020-05-28 16:56:52 +08:00
|
|
|
|
[CascadingParameter] public RadioGroup 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();
|
|
|
|
|
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task OnFirstAfterRenderAsync()
|
|
|
|
|
{
|
|
|
|
|
if (this.AutoFocus)
|
|
|
|
|
{
|
|
|
|
|
await this.Focus();
|
|
|
|
|
}
|
2020-05-28 16:56:52 +08:00
|
|
|
|
if (this is Radio radio)
|
2020-03-25 00:32:26 +08:00
|
|
|
|
{
|
|
|
|
|
RadioGroup?.AddRadio(radio);
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (RadioGroup != null)
|
|
|
|
|
{
|
|
|
|
|
await RadioGroup.OnRadioChange(this.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
await Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 18:32:50 +08:00
|
|
|
|
public async Task OnClick()
|
2020-03-25 00:32:26 +08:00
|
|
|
|
{
|
|
|
|
|
await Select();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|