Skip to content

Commit

Permalink
🆕 feat(Input): supports for built-in required rule (#1442)
Browse files Browse the repository at this point in the history
* 🆕 feat(Input): supports for built-in required rule

* 📝 docs: Update api and comments
  • Loading branch information
capdiem authored Jul 24, 2023
1 parent b1cb837 commit 0e3dd2f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
6 changes: 4 additions & 2 deletions docs/Masa.Blazor.Docs/wwwroot/data/apis/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"scrollTarget": "The amount of scroll distance down before `HideOnScroll` activates.",
"scroll-threshold": "The amount of scroll distance down before `hideOnScroll` activates.",
"src": "Image source. See [MImage](/blazor/components/images) for details.",
"required": "Required",
"extended": "Use this prop to increase the height of the toolbar without using the `Extension` slot for adding content. May be used in conjunction with the `ExtensionHeight` prop, and any of the other props that affect the height of the toolbar, e.g. `Prominent`, `Dense`, etc., WITH THE EXCEPTION of `Height`.",
"collapse": "Puts the toolbar into a collapsed state reducing its maximum width.",
"extensionHeight": "Specify an explicit height for the `ExtensionContent` slot.",
Expand Down Expand Up @@ -104,6 +103,8 @@
"prependInnerIcon": "Prepends an icon inside the component's input, uses the same syntax as [MIcon](/blazor/components/icons)",
"readonly": "Puts input in readonly state",
"reverse": "Reverses the input orientation",
"required": "The required rule built-in",
"requiredMessage": "The error message when the required rule is not satisfied.",
"rules": "Accepts an array of functions that take an input value as an argument and return either true / false or a string with an error message",
"searchInput": "Search value.",
"shaped": "Round if `Outlined` and increase **border-radius** if `Filled`. Must be used with either `Outlined` or `Filled`",
Expand Down Expand Up @@ -207,7 +208,6 @@
"scrollTarget": "在 `HideOnScroll` 激活之前的滚动距离。",
"scroll-threshold": "在 `HideOnScroll` 激活之前的滚动距离。",
"src": "图像源。详情请参阅 [MImage](/blazor/components/images)",
"required": "必须的",
"extended": "使用此道具可以增加工具栏的高度,而 不需要 使用 `ExtensionContent` 插槽来添加内容。可以与 `ExtensionHeight` 属性,以及其他会影响工具栏高度的其他属性,例如 `Prominent`, `Dense` 等配合使用,`Height` 除外。",
"collapse": "将工具栏置于折叠状态,以减小其最大宽度。",
"extensionHeight": "为 `ExtensionContent` 插槽指定一个明确的高度。",
Expand Down Expand Up @@ -273,6 +273,8 @@
"prependInnerIcon": "在组件的输入中添加一个图标,使用与 [MIcon](/blazor/components/icons) 相同的语法",
"readonly": "将输入设置为只读状态",
"reverse": "反转输入方向",
"required": "内置的必填规则",
"requiredMessage": "当必填规则不满足时的错误消息",
"rules": "接受一个委托数组,该数组接受一个输入值作为参数,并返回 true / false 或 a string 和一个错误消息",
"searchInput": "搜索值",
"shaped": "如果 `Outlined` 则为圆形,如果 `Filled` 则增加 **border-radius**。必须与 `Outlined` 或 `Filled` 一起使用",
Expand Down
45 changes: 45 additions & 0 deletions src/Masa.Blazor/Components/Input/MInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,40 @@ public partial class MInput<TValue> : BInput<TValue>, IThemeable
[Parameter]
public EventCallback<TValue> OnChange { get; set; }

/// <summary>
/// The required rule built-in.
/// </summary>
[Parameter]
public bool Required { get; set; }

/// <summary>
/// The error message when the required rule is not satisfied.
/// </summary>
[Parameter, ApiDefaultValue(DEFAULT_REQUIRED_MESSAGE)]
public string RequiredMessage
{
get => _requiredMessage ?? DEFAULT_REQUIRED_MESSAGE;
set => _requiredMessage = value;
}

#region built-in Required rule

private const string DEFAULT_REQUIRED_MESSAGE = "Required";

private static readonly Func<TValue, bool> s_defaultRequiredRule = v =>
{
if (v is string str)
{
return !string.IsNullOrWhiteSpace(str);
}
return !EqualityComparer<TValue>.Default.Equals(v, default);
};

private string? _requiredMessage;

#endregion

public virtual string ComputedColor => IsDisabled ? "" : Color ?? (IsDark ? "white" : "primary");

public virtual bool HasColor => false;
Expand Down Expand Up @@ -59,6 +93,17 @@ public virtual string ValidationState

public virtual bool IsLabelActive => IsDirty;

protected override void OnParametersSet()
{
base.OnParametersSet();

if (Required)
{
var rules = new List<Func<TValue, StringBoolean>>() { v => s_defaultRequiredRule(v) ? true : RequiredMessage };
Rules = Rules is null ? rules : rules.Concat(Rules);
}
}

protected override void RegisterWatchers(PropertyWatcher watcher)
{
base.RegisterWatchers(watcher);
Expand Down

0 comments on commit 0e3dd2f

Please sign in to comment.