StringValueModal.razor 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. @using System.ComponentModel.DataAnnotations
  2. @if (IsOpen)
  3. {
  4. <div class="fixed inset-0 z-50 flex items-center justify-center">
  5. <!-- Backdrop -->
  6. <div class="absolute inset-0 bg-black/70" @onclick="Cancel"></div>
  7. <!-- Modal -->
  8. <div class="relative bg-zinc-900 border border-zinc-800 rounded w-full max-w-md p-4">
  9. <!-- Header -->
  10. <div class="flex justify-between items-center mb-3">
  11. <div class="text-zinc-100 text-sm font-medium">
  12. @Title
  13. </div>
  14. <button
  15. class="text-zinc-400 hover:text-zinc-200"
  16. @onclick="Cancel">
  17. ✕
  18. </button>
  19. </div>
  20. @if (!string.IsNullOrWhiteSpace(Description))
  21. {
  22. <div class="text-xs text-zinc-400 mb-4">
  23. @Description
  24. </div>
  25. }
  26. <!-- Form -->
  27. <EditForm Model="_model" OnValidSubmit="HandleValidSubmit">
  28. <DataAnnotationsValidator/>
  29. @if (!string.IsNullOrEmpty(_error))
  30. {
  31. <div class="text-xs text-red-400 mb-3">
  32. @_error
  33. </div>
  34. }
  35. <div class="text-sm">
  36. <label class="block text-zinc-400 mb-1">
  37. @Label
  38. </label>
  39. <InputText
  40. class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
  41. @bind-Value="_model.Value"/>
  42. </div>
  43. <!-- Actions -->
  44. <div class="flex justify-end gap-2 mt-5">
  45. <button
  46. type="button"
  47. class="px-3 py-1 rounded border border-zinc-700 text-zinc-300 hover:bg-zinc-800"
  48. @onclick="Cancel">
  49. Cancel
  50. </button>
  51. <button
  52. type="submit"
  53. class="px-3 py-1 rounded bg-emerald-600 text-black hover:bg-emerald-500">
  54. Accept
  55. </button>
  56. </div>
  57. </EditForm>
  58. </div>
  59. </div>
  60. }
  61. @code {
  62. [Parameter] public bool IsOpen { get; set; }
  63. [Parameter] public EventCallback<bool> IsOpenChanged { get; set; }
  64. [Parameter] public string Title { get; set; } = "Edit value";
  65. [Parameter] public string? Description { get; set; }
  66. [Parameter] public string Label { get; set; } = "Value";
  67. [Parameter] public string? Value { get; set; }
  68. /// <summary>
  69. /// Called when Accept is clicked.
  70. /// May throw an exception (e.g. validation error).
  71. /// </summary>
  72. [Parameter]
  73. public EventCallback<string> OnSubmit { get; set; }
  74. private FormModel _model = new();
  75. private string? _error;
  76. protected override void OnParametersSet()
  77. {
  78. if (IsOpen)
  79. {
  80. _error = null;
  81. _model = new FormModel
  82. {
  83. Value = Value
  84. };
  85. }
  86. }
  87. private async Task HandleValidSubmit()
  88. {
  89. _error = null;
  90. try
  91. {
  92. await OnSubmit.InvokeAsync(_model.Value!);
  93. await Close();
  94. }
  95. catch (Exception ex)
  96. {
  97. // Show exception message instead of closing
  98. _error = ex.Message;
  99. }
  100. }
  101. private async Task Cancel()
  102. {
  103. await Close();
  104. }
  105. private async Task Close()
  106. {
  107. _model = new FormModel();
  108. _error = null;
  109. await IsOpenChanged.InvokeAsync(false);
  110. }
  111. private class FormModel
  112. {
  113. [Required] public string? Value { get; set; }
  114. }
  115. }