using Connected.Annotations; using Microsoft.AspNetCore.Components; namespace Connected.Components; public class BooleanInput : FormComponent { public BooleanInput() : base(new BoolConverter()) { } /// /// If true, the input will be disabled. /// [Parameter] [Category(CategoryTypes.FormComponent.Behavior)] public bool Disabled { get; set; } /// /// If true, the input will be read-only. /// [Parameter] [Category(CategoryTypes.FormComponent.Behavior)] public bool ReadOnly { get; set; } /// /// The state of the component /// [Parameter] [Category(CategoryTypes.FormComponent.Data)] public T Checked { get => _value; set => _value = value; } /// /// If true will prevent the click from bubbling up the event tree. /// [Parameter] [Category(CategoryTypes.FormComponent.Behavior)] public bool StopClickPropagation { get; set; } = true; /// /// Fired when Checked changes. /// [Parameter] public EventCallback CheckedChanged { get; set; } protected bool? BoolValue => Converter.Set(Checked); protected virtual Task OnChange(ChangeEventArgs args) { Touched = true; return SetBoolValueAsync((bool?)args.Value); } protected Task SetBoolValueAsync(bool? value) { return SetCheckedAsync(Converter.Get(value)); } protected async Task SetCheckedAsync(T value) { if (Disabled) return; if (!EqualityComparer.Default.Equals(Checked, value)) { Checked = value; await CheckedChanged.InvokeAsync(value); BeginValidate(); FieldChanged(Checked); } } protected override bool SetConverter(Converter value) { var changed = base.SetConverter(value); if (changed) SetBoolValueAsync(Converter.Set(Checked)).AndForget(); return changed; } /// /// A value is required, so if not checked we return ERROR. /// protected override bool HasValue(T value) { return (BoolValue == true); } }