218 lines
8.8 KiB
Plaintext
218 lines
8.8 KiB
Plaintext
@using BLAIzor.Models
|
|
@using Radzen
|
|
|
|
@if (isEditing)
|
|
{
|
|
<EditForm EditContext="@editContext" OnValidSubmit="@HandleValidSubmit">
|
|
<div class="card p-4">
|
|
<h4 class="mb-3">@TitleString</h4>
|
|
|
|
@foreach (var field in Fields)
|
|
{
|
|
if (IgnoreReflection.Contains(field.Label)) continue;
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">@field.Label</label>
|
|
@switch (field.Type?.ToLower())
|
|
{
|
|
case "textarea":
|
|
Console.Write($"{field.Label}, {GetValue(field.Label)}");
|
|
<RadzenTextArea Style="width:100%"
|
|
Value="@GetValue(field.Label)"
|
|
Change="@(args => SetValue(field.Label, args))" />
|
|
break;
|
|
|
|
case "number":
|
|
<RadzenNumeric TValue="int"
|
|
Value=@(Convert.ToInt32(GetValue(field.Label) == "" ? "0" : GetValue(field.Label)))
|
|
Change=@(args => SetValue(field.Label, args.ToString()))
|
|
InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", "enter value" }})" />
|
|
@* <RadzenTextBox Style="width:100%"
|
|
Type="number"
|
|
Value="@GetValue(field.Label)"
|
|
Change="@(args => SetValue(field.Label, args?.ToString()))" /> *@
|
|
break;
|
|
|
|
case "date":
|
|
<RadzenDatePicker TValue="DateTime?"
|
|
Value="@GetDate(field.Label)"
|
|
Change="@(args => SetDate(field.Label, args))"
|
|
Style="width:100%" />
|
|
break;
|
|
|
|
case "checkbox":
|
|
<div class="mb-3">
|
|
<RadzenCheckBox Name="@($"{field.Label}_CheckBox")"
|
|
TValue="bool"
|
|
Value="@GetCheckboxValue(field.Label)"
|
|
Change="@(args => SetCheckboxValue(field.Label, args))" />
|
|
<RadzenLabel Text="@field.Label"
|
|
Component="@($"{field.Label}_CheckBox")"
|
|
class="rz-ms-2" />
|
|
</div>
|
|
break;
|
|
case "radio":
|
|
<div class="mb-3">
|
|
<RadzenRadioButtonList TValue="int"
|
|
Value="@GetRadioValue(field.Label)"
|
|
Change="@(args => SetRadioValue(field.Label, args))">
|
|
|
|
<Items>
|
|
@foreach (var option in field.Options ?? new())
|
|
{
|
|
var split = option.Split(':');
|
|
var text = split[0];
|
|
var val = split.Length > 1 && int.TryParse(split[1], out var parsedVal) ? parsedVal : 0;
|
|
|
|
<RadzenRadioButtonListItem Text="@text" Value="@val" />
|
|
}
|
|
</Items>
|
|
</RadzenRadioButtonList>
|
|
<RadzenLabel Text="@field.Label" class="rz-ms-2" />
|
|
</div>
|
|
break;
|
|
|
|
case "select":
|
|
<RadzenLabel Text="@field.Label" Component="@($"{field.Label}_DropDown")" />
|
|
<RadzenDropDown Name="@($"{field.Label}_DropDown")"
|
|
Data="@field.Options"
|
|
TValue="string"
|
|
Value="@GetDropdownValue(field.Label)"
|
|
Change="@(args => SetDropdownValue(field.Label, args.ToString()))"
|
|
Style="width: 100%; max-width: 400px;" />
|
|
break;
|
|
|
|
default:
|
|
<RadzenTextBox Style="width:100%"
|
|
Value="@GetValue(field.Label)"
|
|
Change="@(args => SetValue(field.Label, args?.ToString()))" />
|
|
break;
|
|
}
|
|
</div>
|
|
}
|
|
|
|
<RadzenButton ButtonStyle="ButtonStyle.Primary" Text="@ButtonTextString" Type="Submit" />
|
|
</div>
|
|
</EditForm>
|
|
}
|
|
else
|
|
{
|
|
<div class="card p-4">
|
|
<h4 class="mb-3">Details</h4>
|
|
@foreach (var field in Fields)
|
|
{
|
|
<p><strong>@field.Label:</strong> @formValues.GetValueOrDefault(field.Label)</p>
|
|
}
|
|
</div>
|
|
}
|
|
|
|
<p class="mt-2">@FormSubmitResult</p>
|
|
|
|
@code {
|
|
[Parameter] public List<FormField> Fields { get; set; } = new();
|
|
[Parameter] public List<string> IgnoreReflection { get; set; } = new();
|
|
[Parameter] public EventCallback<Dictionary<string, string>> OnSubmit { get; set; }
|
|
[Parameter] public bool isEditing { get; set; }
|
|
[Parameter] public string TitleString { get; set; } = "Edit your details";
|
|
[Parameter] public string ButtonTextString { get; set; } = "Submit";
|
|
|
|
private EditContext editContext;
|
|
private Dictionary<string, string> formValues = new();
|
|
private Dictionary<string, DateTime?> formDates = new();
|
|
private string FormSubmitResult = "";
|
|
|
|
protected override void OnParametersSet()
|
|
{
|
|
foreach (var field in Fields)
|
|
{
|
|
// Initialize default value for form fields (text, number, etc.)
|
|
if (!formValues.ContainsKey(field.Label))
|
|
{
|
|
if (field.Type?.ToLower() == "radio" && field.Options?.Count > 0)
|
|
{
|
|
// Try to get the value part after ":" in "Text:Value" format
|
|
var firstOption = field.Options.FirstOrDefault();
|
|
var defaultValue = firstOption?.Split(':').Length > 1
|
|
? firstOption.Split(':')[1]
|
|
: "0";
|
|
|
|
formValues[field.Label] = defaultValue ?? "0";
|
|
}
|
|
else
|
|
{
|
|
formValues[field.Label] = string.Empty;
|
|
}
|
|
}
|
|
|
|
// Initialize default value for date fields
|
|
if (field.Type?.ToLower() == "date" && !formDates.ContainsKey(field.Label))
|
|
{
|
|
formDates[field.Label] = DateTime.Today;
|
|
}
|
|
}
|
|
|
|
editContext = new EditContext(formValues);
|
|
}
|
|
|
|
private string GetValue(string label) => formValues.TryGetValue(label, out var value) ? value : "";
|
|
|
|
private void SetValue(string label, string? value)
|
|
{
|
|
formValues[label] = value ?? "";
|
|
}
|
|
|
|
private DateTime? GetDate(string label) => formDates.TryGetValue(label, out var date) ? date : null;
|
|
|
|
private void SetDate(string label, DateTime? value)
|
|
{
|
|
formDates[label] = value;
|
|
}
|
|
|
|
private string GetDropdownValue(string label) =>
|
|
formValues.TryGetValue(label, out var value) ? value : "";
|
|
|
|
private void SetDropdownValue(string label, string? value)
|
|
{
|
|
formValues[label] = value ?? "";
|
|
}
|
|
|
|
private bool GetCheckboxValue(string label)
|
|
{
|
|
if (formValues.TryGetValue(label, out var value))
|
|
{
|
|
return bool.TryParse(value, out var result) && result;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void SetCheckboxValue(string label, bool? value)
|
|
{
|
|
formValues[label] = (value ?? false).ToString();
|
|
}
|
|
|
|
private int GetRadioValue(string label)
|
|
{
|
|
if (formValues.TryGetValue(label, out var value) && int.TryParse(value, out var intValue))
|
|
return intValue;
|
|
return 0; // or your default
|
|
}
|
|
|
|
private void SetRadioValue(string label, int? value)
|
|
{
|
|
formValues[label] = value?.ToString() ?? "0";
|
|
}
|
|
|
|
private async Task HandleValidSubmit()
|
|
{
|
|
// Merge dates back into string values
|
|
foreach (var dateField in formDates)
|
|
{
|
|
formValues[dateField.Key] = dateField.Value?.ToString("yyyy-MM-dd") ?? "";
|
|
}
|
|
|
|
FormSubmitResult = "Success!";
|
|
await OnSubmit.InvokeAsync(formValues);
|
|
isEditing = false;
|
|
}
|
|
}
|