diff --git a/src/BootstrapBlazor.Shared/Pages/Samples/Switchs.razor b/src/BootstrapBlazor.Shared/Pages/Samples/Switchs.razor index 4a1a87e71..5cc56a42f 100644 --- a/src/BootstrapBlazor.Shared/Pages/Samples/Switchs.razor +++ b/src/BootstrapBlazor.Shared/Pages/Samples/Switchs.razor @@ -85,6 +85,14 @@ + +
+
+ +
+
+
+ diff --git a/src/BootstrapBlazor.Shared/Pages/Samples/Switchs.razor.cs b/src/BootstrapBlazor.Shared/Pages/Samples/Switchs.razor.cs index debd73fd4..e39f9f03b 100644 --- a/src/BootstrapBlazor.Shared/Pages/Samples/Switchs.razor.cs +++ b/src/BootstrapBlazor.Shared/Pages/Samples/Switchs.razor.cs @@ -6,6 +6,7 @@ using BootstrapBlazor.Shared.Common; using BootstrapBlazor.Shared.Pages.Components; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; namespace BootstrapBlazor.Shared.Pages { @@ -30,9 +31,7 @@ namespace BootstrapBlazor.Shared.Pages /// private bool BindValue { get; set; } = true; - /// - /// - /// + [NotNull] private Logger? Trace { get; set; } /// @@ -42,14 +41,16 @@ namespace BootstrapBlazor.Shared.Pages private void OnValueChanged(bool val) { BindValue = val; - Trace?.Log($"Switch CurrentValue: {val}"); + Trace.Log($"Switch CurrentValue: {val}"); } + private bool? NullValue { get; set; } + /// /// 获得属性方法 /// /// - private IEnumerable GetAttributes() => new AttributeItem[] + private static IEnumerable GetAttributes() => new AttributeItem[] { // TODO: 移动到数据库中 new AttributeItem() { diff --git a/src/BootstrapBlazor.Shared/Pages/Samples/ValidateForms.razor b/src/BootstrapBlazor.Shared/Pages/Samples/ValidateForms.razor index b80cb82fc..771378ec7 100644 --- a/src/BootstrapBlazor.Shared/Pages/Samples/ValidateForms.razor +++ b/src/BootstrapBlazor.Shared/Pages/Samples/ValidateForms.razor @@ -27,23 +27,6 @@
-
- - - - - - - - - -
-
diff --git a/src/BootstrapBlazor.Shared/Pages/Samples/ValidateForms.razor.cs b/src/BootstrapBlazor.Shared/Pages/Samples/ValidateForms.razor.cs index f1abd8bac..b71468ece 100644 --- a/src/BootstrapBlazor.Shared/Pages/Samples/ValidateForms.razor.cs +++ b/src/BootstrapBlazor.Shared/Pages/Samples/ValidateForms.razor.cs @@ -52,8 +52,6 @@ namespace BootstrapBlazor.Shared.Pages [NotNull] private ComplexFoo? ComplexModel { get; set; } - private static IEnumerable PageItemsSource => new int[] { 4, 10, 20 }; - /// /// OnInitializedAsync 方法 /// @@ -62,8 +60,6 @@ namespace BootstrapBlazor.Shared.Pages { await base.OnInitializedAsync(); - Hobbys = Foo.GenerateHobbys(LocalizerFoo); - // 切换线程 模拟异步通过 webapi 加载数据 await Task.Yield(); @@ -76,72 +72,6 @@ namespace BootstrapBlazor.Shared.Pages Dummy = new Dummy1() { Dummy2 = new Dummy2() }, }; } - private Task OnSaveAsync(Foo item) - { - // 增加数据演示代码 - if (item.Id == 0) - { - item.Id = Items.Max(i => i.Id) + 1; - Items.Add(item); - } - else - { - var oldItem = Items.FirstOrDefault(i => i.Id == item.Id); - if (oldItem != null) - { - oldItem.Name = item.Name; - oldItem.Address = item.Address; - oldItem.DateTime = item.DateTime; - oldItem.Count = item.Count; - oldItem.Complete = item.Complete; - oldItem.Education = item.Education; - } - } - return Task.FromResult(true); - } - - private Task OnDeleteAsync(IEnumerable items) - { - items.ToList().ForEach(i => Items.Remove(i)); - return Task.FromResult(true); - } - - private Task> OnQueryAsync(QueryPageOptions options) - { - IEnumerable items = Items; - - // 过滤 - var isFiltered = false; - if (options.Filters.Any()) - { - items = items.Where(options.Filters.GetFilterFunc()); - isFiltered = true; - } - - // 排序 - var isSorted = false; - if (!string.IsNullOrEmpty(options.SortName)) - { - var invoker = SortLambdaCache.GetOrAdd(typeof(Foo), key => LambdaExtensions.GetSortLambda().Compile()); - items = invoker(items, options.SortName, options.SortOrder); - isSorted = true; - } - - // 设置记录总数 - var total = items.Count(); - - // 内存分页 - items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList(); - - return Task.FromResult(new QueryData() - { - Items = items, - TotalCount = total, - IsSorted = isSorted, - IsFiltered = isFiltered, - IsSearch = true - }); - } private Task OnInvalidSubmit1(EditContext context) { diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index c1258239d..a2a15751c 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 5.5.3 + 5.5.4 diff --git a/src/BootstrapBlazor/Components/Switch/NullSwitch.razor b/src/BootstrapBlazor/Components/Switch/NullSwitch.razor new file mode 100644 index 000000000..edd4fe0c5 --- /dev/null +++ b/src/BootstrapBlazor/Components/Switch/NullSwitch.razor @@ -0,0 +1,16 @@ +@namespace BootstrapBlazor.Components +@inherits ToggleBase + +@if (IsShowLabel) +{ + +} +
+ + @if (!string.IsNullOrEmpty(Text)) + { + + @Text + + } +
diff --git a/src/BootstrapBlazor/Components/Switch/NullSwitch.razor.cs b/src/BootstrapBlazor/Components/Switch/NullSwitch.razor.cs new file mode 100644 index 000000000..3e4a67348 --- /dev/null +++ b/src/BootstrapBlazor/Components/Switch/NullSwitch.razor.cs @@ -0,0 +1,150 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Localization; +using System.Diagnostics.CodeAnalysis; +using System.Threading.Tasks; + +namespace BootstrapBlazor.Components +{ + /// + /// + /// + public partial class NullSwitch + { + /// + /// 获得 样式集合 + /// + private string? ClassName => CssBuilder.Default("switch") + .AddClass("is-checked", ComponentValue) + .AddClass("is-disabled", IsDisabled) + .AddClassFromAttributes(AdditionalAttributes) + .Build(); + + private string? CoreClassName => CssBuilder.Default("switch-core") + .AddClass($"border-{OnColor.ToDescriptionString()}", OnColor != Color.None && ComponentValue) + .AddClass($"bg-{OnColor.ToDescriptionString()}", OnColor != Color.None && ComponentValue) + .AddClass($"border-{OffColor.ToDescriptionString()}", OffColor != Color.None && !ComponentValue) + .AddClass($"bg-{OffColor.ToDescriptionString()}", OffColor != Color.None && !ComponentValue) + .Build(); + + private string? GetInnerText() + { + string? ret = null; + if (ShowInnerText) + { + ret = ComponentValue ? OnInnerText : OffInnerText; + } + return ret; + } + + /// + /// 获得 显示文字 + /// + private string? Text => ComponentValue ? OnText : OffText; + + /// + /// 获得 组件最小宽度 + /// + private string? SwitchStyleName => CssBuilder.Default() + .AddClass($"min-width: {Width}px;", Width > 0) + .AddStyleFromAttributes(AdditionalAttributes) + .Build(); + + /// + /// 获得 Style 集合 + /// + protected override string? StyleName => CssBuilder.Default() + .AddClass($"width: {Width}px;", Width > 0) + .AddClass($"height: {Height}px;", Height >= 20) + .Build(); + + /// + /// 获得/设置 开颜色 + /// + [Parameter] + public Color OnColor { get; set; } = Color.Success; + + /// + /// 获得/设置 关颜色 + /// + [Parameter] + public Color OffColor { get; set; } + + /// + /// 获得/设置 组件宽度 默认 40 + /// + [Parameter] + public override int Width { get; set; } = 40; + + /// + /// 获得/设置 控件高度默认 20px + /// + [Parameter] + public int Height { get; set; } = 20; + + /// + /// 获得/设置 组件 On 时内置显示文本 + /// + [Parameter] + [NotNull] + public string? OnInnerText { get; set; } + + /// + /// 获得/设置 组件 Off 时内置显示文本 + /// + [Parameter] + [NotNull] + public string? OffInnerText { get; set; } + + /// + /// 获得/设置 是否显示内置文字 默认 false 显示 + /// + [Parameter] + public bool ShowInnerText { get; set; } + + [Inject] + [NotNull] + private IStringLocalizer? Localizer { get; set; } + + /// + /// 获得/设置 绑定值为空时的默认值 默认为 false + /// + [Parameter] + public bool DefaultValueWhenNull { get; set; } + + /// + /// 获得/设置 组件 Value 值 + /// + protected bool ComponentValue => Value ?? DefaultValueWhenNull; + + /// + /// OnInitialized 方法 + /// + protected override void OnInitialized() + { + base.OnInitialized(); + + OnInnerText ??= Localizer[nameof(OnInnerText)]; + OffInnerText ??= Localizer[nameof(OffInnerText)]; + } + + /// + /// 点击控件时触发此方法 + /// + private async Task OnClick() + { + if (!IsDisabled) + { + Value = !ComponentValue; + if (ValueChanged.HasDelegate) + { + await ValueChanged.InvokeAsync(Value); + } + OnValueChanged?.Invoke(Value); + } + } + } +} diff --git a/src/BootstrapBlazor/Components/Switch/Switch.razor b/src/BootstrapBlazor/Components/Switch/Switch.razor index 9de10021b..dc7975b05 100644 --- a/src/BootstrapBlazor/Components/Switch/Switch.razor +++ b/src/BootstrapBlazor/Components/Switch/Switch.razor @@ -1,5 +1,5 @@ @namespace BootstrapBlazor.Components -@inherits ToggleBase +@inherits ToggleBase @if (IsShowLabel) { diff --git a/src/BootstrapBlazor/Components/Switch/Switch.razor.cs b/src/BootstrapBlazor/Components/Switch/Switch.razor.cs index 520531325..120093495 100644 --- a/src/BootstrapBlazor/Components/Switch/Switch.razor.cs +++ b/src/BootstrapBlazor/Components/Switch/Switch.razor.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Localization; using System.Diagnostics.CodeAnalysis; +using System.Threading.Tasks; namespace BootstrapBlazor.Components { @@ -13,10 +14,7 @@ namespace BootstrapBlazor.Components ///
public partial class Switch { - /// - /// 获得 样式集合 - /// - protected override string? ClassName => CssBuilder.Default("switch") + private string? ClassName => CssBuilder.Default("switch") .AddClass("is-checked", Value) .AddClass("is-disabled", IsDisabled) .AddClassFromAttributes(AdditionalAttributes) @@ -32,7 +30,11 @@ namespace BootstrapBlazor.Components private string? GetInnerText() { string? ret = null; - if (ShowInnerText) ret = Value ? OnInnerText : OffInnerText; + if (ShowInnerText) + { + ret = Value ? OnInnerText : OffInnerText; + } + return ret; } @@ -115,5 +117,22 @@ namespace BootstrapBlazor.Components OnInnerText ??= Localizer[nameof(OnInnerText)]; OffInnerText ??= Localizer[nameof(OffInnerText)]; } + + /// + /// 点击控件时触发此方法 + /// + private async Task OnClick() + { + if (!IsDisabled) + { + Value = !Value; + if (ValueChanged.HasDelegate) + { + await ValueChanged.InvokeAsync(Value); + } + + OnValueChanged?.Invoke(Value); + } + } } } diff --git a/src/BootstrapBlazor/Components/Toggle/Toggle.razor b/src/BootstrapBlazor/Components/Toggle/Toggle.razor index c17130cc4..9f317629d 100644 --- a/src/BootstrapBlazor/Components/Toggle/Toggle.razor +++ b/src/BootstrapBlazor/Components/Toggle/Toggle.razor @@ -1,5 +1,5 @@ @namespace BootstrapBlazor.Components -@inherits ToggleBase +@inherits ToggleBase @if (IsShowLabel) { diff --git a/src/BootstrapBlazor/Components/Toggle/Toggle.razor.cs b/src/BootstrapBlazor/Components/Toggle/Toggle.razor.cs index 466e66d1d..611076538 100644 --- a/src/BootstrapBlazor/Components/Toggle/Toggle.razor.cs +++ b/src/BootstrapBlazor/Components/Toggle/Toggle.razor.cs @@ -5,18 +5,34 @@ using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Localization; using System.Diagnostics.CodeAnalysis; +using System.Threading.Tasks; namespace BootstrapBlazor.Components { /// /// /// - public sealed partial class Toggle + public partial class Toggle { + private string? ClassName => CssBuilder.Default("btn btn-toggle") + .AddClass("btn-default off", !Value) + .AddClass("disabled", IsDisabled) + .Build(); + + private string? ToggleOnClassString => CssBuilder.Default("toggle-on") + .AddClass($"bg-{Color.ToDescriptionString()}", Color != Color.None) + .Build(); + private string? WrapperClassString => CssBuilder.Default("toggle") .AddClassFromAttributes(AdditionalAttributes) .Build(); + /// + /// 获得/设置 组件颜色 默认为 Success 颜色 + /// + [Parameter] + public Color Color { get; set; } = Color.Success; + [Inject] [NotNull] private IStringLocalizer? Localizer { get; set; } @@ -31,5 +47,18 @@ namespace BootstrapBlazor.Components OnText ??= Localizer[nameof(OnText)]; OffText ??= Localizer[nameof(OffText)]; } + + /// + /// 点击控件时触发此方法 + /// + private async Task OnClick() + { + if (!IsDisabled) + { + Value = !Value; + if (ValueChanged.HasDelegate) await ValueChanged.InvokeAsync(Value); + OnValueChanged?.Invoke(Value); + } + } } } diff --git a/src/BootstrapBlazor/Components/Toggle/ToggleBase.cs b/src/BootstrapBlazor/Components/Toggle/ToggleBase.cs index 58e5a03b0..700b1c522 100644 --- a/src/BootstrapBlazor/Components/Toggle/ToggleBase.cs +++ b/src/BootstrapBlazor/Components/Toggle/ToggleBase.cs @@ -3,32 +3,15 @@ // Website: https://www.blazor.zone or https://argozhang.github.io/ using Microsoft.AspNetCore.Components; -using System; using System.Diagnostics.CodeAnalysis; -using System.Threading.Tasks; namespace BootstrapBlazor.Components { /// /// Toggle 开关组件 /// - public class ToggleBase : ValidateBase + public class ToggleBase : ValidateBase { - /// - /// 获得 样式集合 - /// - protected virtual string? ClassName => CssBuilder.Default("btn btn-toggle") - .AddClass("btn-default off", !Value) - .AddClass("disabled", IsDisabled) - .Build(); - - /// - /// 获得 ToggleOn 样式 - /// - protected string? ToggleOnClassString => CssBuilder.Default("toggle-on") - .AddClass($"bg-{Color.ToDescriptionString()}", Color != Color.None) - .Build(); - /// /// 获得 Style 集合 /// @@ -55,24 +38,5 @@ namespace BootstrapBlazor.Components [Parameter] [NotNull] public virtual string? OffText { get; set; } - - /// - /// 获得/设置 组件颜色 默认为 Success 颜色 - /// - [Parameter] - public Color Color { get; set; } = Color.Success; - - /// - /// 点击控件时触发此方法 - /// - protected virtual async Task OnClick() - { - if (!IsDisabled) - { - Value = !Value; - if (ValueChanged.HasDelegate) await ValueChanged.InvokeAsync(Value); - OnValueChanged?.Invoke(Value); - } - } } }