zhaolei
2020-11-20 4a2e5b9a21940f11757be37d99f0944e240e908b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace Bootstrap.Admin.Pages.Components
{
    /// <summary>
    /// Select 组件基类
    /// </summary>
    public class SelectBase<TItem> : ValidateInputBase<TItem>
    {
        /// <summary>
        /// 获得/设置 Select 组件 列样式 默认 col-sm-6
        /// </summary>
        [Parameter]
        public string ColumnClass { get; set; } = "col-sm-6";
 
        /// <summary>
        /// 当前选择项实例
        /// </summary>
        public SelectedItem SelectedItem { get; set; } = new SelectedItem();
 
        /// <summary>
        /// 获得/设置 绑定数据集
        /// </summary>
        [Parameter]
        public List<SelectedItem> Items { get; set; } = new List<SelectedItem>();
 
        /// <summary>
        /// 获得/设置 是否禁用
        /// </summary>
        [Parameter]
        public bool Disabled { get; set; }
 
        /// <summary>
        /// OnParametersSet 方法
        /// </summary>
        protected override void OnParametersSet()
        {
            Items.ForEach(t =>
            {
                t.Active = t.Value == Value?.ToString();
                if (t.Active) SelectedItem = t;
            });
        }
 
        /// <summary>
        /// OnInitialized 方法
        /// </summary>
        protected override void OnInitialized()
        {
            base.OnInitialized();
            if (!SelectedItem.Active)
            {
                SelectedItem = Items.FirstOrDefault(item => item.Active) ?? Items.First();
            }
        }
 
        /// <summary>
        /// SelectedItemChanged 方法
        /// </summary>
        [Parameter]
        public Action<SelectedItem>? SelectedItemChanged { get; set; }
 
        /// <summary>
        /// 下拉框项被选中时调用此方法
        /// </summary>
        public void ItemClickCallback(SelectedItem item)
        {
            SelectedItem = item;
            CurrentValueAsString = item.Value;
        }
    }
}