复选框 (CheckBox)

复选框及开关组件,用于切换两种状态。

概述

  • 支持开关类型

代码演示

1.默认示例

namespace WebSite.Docus.Inputs.CheckBoxs;

class CheckBox1 : BaseComponent
{
    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        builder.Field<CheckBox>("CheckBox1").Set(f => f.Text, "默认").Build();
        builder.Field<CheckBox>("CheckBox2").Value("True").Set(f => f.Text, "启用").Build();
        builder.Field<CheckBox>("CheckBox3").Enabled(false).Set(f => f.Text, "禁用").Build();
    }
}

2.开关示例

namespace WebSite.Docus.Inputs.CheckBoxs;

class CheckBox2 : BaseComponent
{
    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        //默认
        builder.Field<CheckBox>("CheckBox1")
               .Set(f => f.Text, "启用")
               .Set(f => f.Switch, true)
               .Build();
        //开启状态
        builder.Field<CheckBox>("CheckBox2").Value("True")
               .Set(f => f.Text, "启用")
               .Set(f => f.Switch, true)
               .Build();
        //禁用状态
        builder.Field<CheckBox>("CheckBox3").Enabled(false)
               .Set(f => f.Text, "禁用")
               .Set(f => f.Switch, true)
               .Build();
    }
}

3.事件示例

namespace WebSite.Docus.Inputs.CheckBoxs;

class CheckBox3 : BaseComponent
{
    private string? message;

    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        //ValueChanged事件
        builder.Field<CheckBox>("CheckBox1").ValueChanged(OnValueChanged)
               .Set(f => f.Text, "ValueChanged事件")
               .Build();
        builder.Field<CheckBox>("CheckBox2").ValueChanged(OnValueChanged)
               .Set(f => f.Text, "ValueChanged事件")
               .Set(f => f.Switch, true)
               .Build();
        builder.Div("tips", message);
    }

    private void OnValueChanged(string value)
    {
        message = value;
        StateChanged();
    }
}

4.控制示例

namespace WebSite.Docus.Inputs.CheckBoxs;

class CheckBox4 : BaseComponent
{
    private CheckBox? checkBox;

    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        builder.Component<FieldControl>()
               .Set(c => c.OnVisibleChanged, OnVisibleChanged)
               .Set(c => c.OnEnabledChanged, OnEnabledChanged)
               .Set(c => c.OnReadOnlyChanged, OnReadOnlyChanged)
               .Set(c => c.SetValue, SetValue)
               .Set(c => c.GetValue, GetValue)
               .Build();

        builder.Field<CheckBox>("CheckBox")
               .Set(f => f.Text, "启用")
               .Build(value => checkBox = value);
    }

    private void OnVisibleChanged(bool value) => checkBox?.SetVisible(value);
    private void OnEnabledChanged(bool value) => checkBox?.SetEnabled(value);
    private void OnReadOnlyChanged(bool value) => checkBox?.SetReadOnly(value);
    private void SetValue() => checkBox?.SetValue("True");
    private string? GetValue() => checkBox?.Value;
}