!2252 feat(#I4O30L): Display support auto show Label when inside ValidateForm

* feat: Display 组件支持嵌套表单内自动显示 Text 特性
This commit is contained in:
Argo 2021-12-25 09:42:30 +00:00
parent 6323954ec4
commit d6226c75f8
2 changed files with 23 additions and 29 deletions

View File

@ -65,6 +65,18 @@ namespace BootstrapBlazor.Components
[Parameter]
public string? DisplayText { get; set; }
/// <summary>
/// 获得 ValidateForm 实例
/// </summary>
[CascadingParameter]
protected ValidateForm? ValidateForm { get; set; }
/// <summary>
/// 获得 IShowLabel 实例
/// </summary>
[CascadingParameter(Name = "EidtorForm")]
protected IShowLabel? EditorForm { get; set; }
/// <summary>
/// SetParametersAsync 方法
/// </summary>
@ -92,7 +104,16 @@ namespace BootstrapBlazor.Components
{
base.OnParametersSet();
IsShowLabel = ShowLabel ?? false;
// 显式设置显示标签时一定显示
var showLabel = ShowLabel;
// 组件自身未设置 ShowLabel 取 EditorForm/VaidateForm 级联值
if (ShowLabel == null && (EditorForm != null || ValidateForm != null))
{
showLabel = EditorForm?.ShowLabel ?? ValidateForm?.ShowLabel ?? true;
}
IsShowLabel = showLabel ?? false;
// 设置显示标签时未提供 DisplayText 通过双向绑定获取 DisplayName
if (IsShowLabel && DisplayText == null && FieldIdentifier.HasValue)

View File

@ -202,18 +202,6 @@ namespace BootstrapBlazor.Components
[CascadingParameter]
protected EditContext? CascadedEditContext { get; set; }
/// <summary>
/// 获得 ValidateForm 实例
/// </summary>
[CascadingParameter]
protected ValidateForm? ValidateForm { get; set; }
/// <summary>
/// 获得 IShowLabel 实例
/// </summary>
[CascadingParameter(Name = "EidtorForm")]
protected IShowLabel? EditorForm { get; set; }
/// <summary>
/// Parses a string to create an instance of <typeparamref name="TValue"/>. Derived classes can override this to change how
/// <see cref="CurrentValueAsString"/> interprets incoming values.
@ -304,22 +292,7 @@ namespace BootstrapBlazor.Components
/// </summary>
protected override void OnParametersSet()
{
// 显式设置显示标签时一定显示
var showLabel = ShowLabel;
// 组件自身未设置 ShowLabel 取 EditorForm/VaidateForm 级联值
if (ShowLabel == null && (EditorForm != null || ValidateForm != null))
{
showLabel = EditorForm?.ShowLabel ?? ValidateForm?.ShowLabel ?? true;
}
IsShowLabel = showLabel ?? false;
// 绑定属性值获取 DisplayName
if (IsShowLabel && DisplayText == null && FieldIdentifier.HasValue)
{
DisplayText = FieldIdentifier.Value.GetDisplayName();
}
base.OnParametersSet();
Required = (IsNeedValidate && !string.IsNullOrEmpty(DisplayText) && (ValidateForm?.ShowRequiredMark ?? false) && IsRequired()) ? "true" : null;
}