Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions src/BlazorUI/Bit.BlazorUI/Components/Inputs/BitInputBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ protected BitInputBase()
/// </summary>
[Parameter] public string? Name { get; set; }

/// <summary>
/// Disables the validation of the input.
/// </summary>
[Parameter] public bool NoValidate { get; set; }

/// <summary>
/// Callback for when the input value changes.
/// </summary>
Expand Down Expand Up @@ -141,6 +146,11 @@ public override Task SetParametersAsync(ParameterView parameters)
{
switch (parameter.Key)
{
case nameof(NoValidate):
NoValidate = (bool)parameter.Value;
parametersDictionary.Remove(parameter.Key);
break;

case nameof(CascadedEditContext):
CascadedEditContext = (EditContext?)parameter.Value;
parametersDictionary.Remove(parameter.Key);
Expand Down Expand Up @@ -198,26 +208,29 @@ public override Task SetParametersAsync(ParameterView parameters)
}
}

if (_hasInitializedParameters is false)
if (NoValidate is false)
{
// This is the first run
// Could put this logic in OnInit, but its nice to avoid forcing people who override OnInitialized to call base.OnInitialized()
if (_hasInitializedParameters is false)
{
// This is the first run
// Could put this logic in OnInitialized, but its nice to avoid forcing people who override OnInitialized to call base.OnInitialized()

CreateFieldIdentifier();
CreateFieldIdentifier();

_hasInitializedParameters = true;
}
else if (CascadedEditContext != EditContext)
{
// Not the first run
_hasInitializedParameters = true;
}
else if (CascadedEditContext != EditContext)
{
// Not the first run

// We don't support changing EditContext because it's messy to be clearing up state and event
// handlers for the previous one, and there's no strong use case. If a strong use case
// emerges, we can consider changing this.
throw new InvalidOperationException($"{GetType()} does not support changing the {nameof(EditContext)} dynamically.");
}
// We don't support changing EditContext because it's messy to be clearing up state and event
// handlers for the previous one, and there's no strong use case. If a strong use case
// emerges, we can consider changing this.
throw new InvalidOperationException($"{GetType()} does not support changing the {nameof(EditContext)} dynamically.");
}

UpdateValidationAttributes();
UpdateValidationAttributes();
}

// For derived components, retain the usual lifecycle with OnInit/OnParametersSet/etc.
return base.SetParametersAsync(ParameterView.FromDictionary(parametersDictionary!));
Expand Down Expand Up @@ -334,7 +347,7 @@ protected async Task SetCurrentValueAsStringAsync(string? value, bool bypass = f
await SetCurrentValueAsync(parsedValue);
}
}
else
else if (NoValidate is false)
{
_parsingFailed = true;

Expand All @@ -348,6 +361,10 @@ protected async Task SetCurrentValueAsStringAsync(string? value, bool bypass = f
EditContext.NotifyFieldChanged(FieldIdentifier);
}
}
else
{
_parsingFailed = false;
}

// We can skip the validation notification if we were previously valid and still are
if (_parsingFailed || _previousParsingAttemptFailed)
Expand All @@ -365,7 +382,10 @@ protected async Task SetCurrentValueAsync(TValue? value)

await ValueChanged.InvokeAsync(value);

EditContext?.NotifyFieldChanged(FieldIdentifier);
if (EditContext is not null && NoValidate is false)
{
EditContext.NotifyFieldChanged(FieldIdentifier);
}

await OnChange.InvokeAsync(value);
}
Expand All @@ -388,7 +408,7 @@ private void OnValidateStateChanged(object? sender, ValidationStateChangedEventA

private void UpdateValidationAttributes()
{
if (EditContext is null) return;
if (EditContext is null || NoValidate) return;

var hasAriaInvalidAttribute = InputHtmlAttributes is not null && InputHtmlAttributes.ContainsKey("aria-invalid");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ public partial class ComponentDemo
Description = "Gets or sets the name of the element. Allows access by name from the associated form.",
},
new()
{
Name = "NoValidate",
Type = "bool",
DefaultValue = "false",
Description = "Disables the validation of the input.",
},
new()
{
Name = "OnChange",
Type = "EventCallback<TValue?>",
Expand Down