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