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] public EventCallback<string> OnSubmit { get; set; }
  72. private FormModel _model = new();
  73. private string? _error;
  74. protected override void OnParametersSet()
  75. {
  76. if (IsOpen)
  77. {
  78. _error = null;
  79. _model = new FormModel
  80. {
  81. Value = Value
  82. };
  83. }
  84. }
  85. private async Task HandleValidSubmit()
  86. {
  87. _error = null;
  88. try
  89. {
  90. await OnSubmit.InvokeAsync(_model.Value!);
  91. await Close();
  92. }
  93. catch (Exception ex)
  94. {
  95. // Show exception message instead of closing
  96. _error = ex.Message;
  97. }
  98. }
  99. private async Task Cancel()
  100. {
  101. await Close();
  102. }
  103. private async Task Close()
  104. {
  105. _model = new();
  106. _error = null;
  107. await IsOpenChanged.InvokeAsync(false);
  108. }
  109. private class FormModel
  110. {
  111. [Required]
  112. public string? Value { get; set; }
  113. }
  114. }