Initial commit
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
@using MudBlazor
|
||||
@using RobotNet10.ScriptEngine.Shared
|
||||
|
||||
<MudDialog>
|
||||
<TitleContent>
|
||||
<MudText Typo="Typo.h6">Edit Variable: @VariableName</MudText>
|
||||
</TitleContent>
|
||||
<DialogContent>
|
||||
<MudText Typo="Typo.body2" Class="mb-3">Type: <strong>@TypeName</strong></MudText>
|
||||
<MudTextField @bind-Value="NewValue"
|
||||
Label="Value"
|
||||
Placeholder="Enter new value"
|
||||
Required="true"
|
||||
RequiredError="Value is required"
|
||||
HelperText="@HelperText"
|
||||
Variant="Variant.Outlined"
|
||||
FullWidth="true"
|
||||
Error="@(!string.IsNullOrEmpty(ErrorMessage))"
|
||||
ErrorText="@ErrorMessage"
|
||||
@onkeydown="HandleKeyDown"
|
||||
@bind-Value:after="ValidateValue" />
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="Cancel">Cancel</MudButton>
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="Submit" Disabled="@(!IsValid)">Save</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
[CascadingParameter] IMudDialogInstance Dialog { get; set; } = null!;
|
||||
[Parameter] public string VariableName { get; set; } = string.Empty;
|
||||
[Parameter] public string TypeName { get; set; } = string.Empty;
|
||||
[Parameter] public string CurrentValue { get; set; } = string.Empty;
|
||||
|
||||
private string NewValue { get; set; } = string.Empty;
|
||||
private string ErrorMessage { get; set; } = string.Empty;
|
||||
private bool IsValid => string.IsNullOrEmpty(ErrorMessage) && !string.IsNullOrWhiteSpace(NewValue);
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
NewValue = CurrentValue;
|
||||
ValidateValue();
|
||||
}
|
||||
|
||||
private string HelperText => GetHelperText();
|
||||
|
||||
private string GetHelperText()
|
||||
{
|
||||
var type = ScriptHelpers.ResolveTypeFromString(TypeName);
|
||||
if (type == null) return "";
|
||||
|
||||
return type switch
|
||||
{
|
||||
_ when type == typeof(bool) => "Enter 'true' or 'false'",
|
||||
_ when type == typeof(int) => "Enter an integer value",
|
||||
_ when type == typeof(long) => "Enter a long integer value",
|
||||
_ when type == typeof(float) => "Enter a float value (e.g., 3.14)",
|
||||
_ when type == typeof(double) => "Enter a double value (e.g., 3.14)",
|
||||
_ when type == typeof(decimal) => "Enter a decimal value (e.g., 3.14)",
|
||||
_ when type == typeof(char) => "Enter a single character",
|
||||
_ when type == typeof(string) => "Enter a string value",
|
||||
_ when type.IsEnum => $"Enter one of: {string.Join(", ", Enum.GetNames(type))}",
|
||||
_ => "Enter a valid value"
|
||||
};
|
||||
}
|
||||
|
||||
private void ValidateValue()
|
||||
{
|
||||
ErrorMessage = string.Empty;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(NewValue))
|
||||
{
|
||||
return; // Will be handled by Required validation
|
||||
}
|
||||
|
||||
var type = ScriptHelpers.ResolveTypeFromString(TypeName);
|
||||
if (type == null)
|
||||
{
|
||||
ErrorMessage = "Unknown type";
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if type is supported
|
||||
if (!ScriptHelpers.SupportedTypes.Values.Contains(type) && !type.IsEnum)
|
||||
{
|
||||
ErrorMessage = "Type not supported for editing";
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate value can be converted to the type
|
||||
try
|
||||
{
|
||||
if (type.IsEnum)
|
||||
{
|
||||
// Handle enum validation separately since ResolveValueFromString doesn't handle enums correctly
|
||||
Enum.Parse(type, NewValue.Trim(), ignoreCase: true);
|
||||
}
|
||||
else if (!ScriptHelpers.ResolveValueFromString(NewValue.Trim(), type, out _))
|
||||
{
|
||||
ErrorMessage = $"Invalid value for type {TypeName}";
|
||||
}
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
ErrorMessage = $"Invalid value for type {TypeName}";
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
ErrorMessage = $"Failed to validate value for type {TypeName}";
|
||||
}
|
||||
}
|
||||
|
||||
private void Cancel() => Dialog.Cancel();
|
||||
|
||||
private void Submit()
|
||||
{
|
||||
if (!IsValid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Dialog.Close(DialogResult.Ok(NewValue.Trim()));
|
||||
}
|
||||
|
||||
private void HandleKeyDown(KeyboardEventArgs e)
|
||||
{
|
||||
if (e.Key == "Enter" && IsValid)
|
||||
{
|
||||
Submit();
|
||||
}
|
||||
else if (e.Key == "Escape")
|
||||
{
|
||||
Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user