KeyValueModal.razor 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. @using System.ComponentModel.DataAnnotations
  2. @if (IsOpen)
  3. {
  4. <div class="fixed inset-0 z-50 flex items-center justify-center"
  5. data-testid="@BaseTestId">
  6. <!-- Backdrop -->
  7. <div class="absolute inset-0 bg-black/70"
  8. data-testid="@($"{BaseTestId}-backdrop")"
  9. @onclick="Cancel">
  10. </div>
  11. <!-- Modal -->
  12. <div class="relative bg-zinc-900 border border-zinc-800 rounded w-full max-w-md p-4"
  13. data-testid="@($"{BaseTestId}-container")">
  14. <!-- Header -->
  15. <div class="flex justify-between items-center mb-3"
  16. data-testid="@($"{BaseTestId}-header")">
  17. <div class="text-zinc-100 text-sm font-medium"
  18. data-testid="@($"{BaseTestId}-title")">
  19. @Title
  20. </div>
  21. <button class="text-zinc-400 hover:text-zinc-200"
  22. data-testid="@($"{BaseTestId}-close")"
  23. @onclick="Cancel">
  24. </button>
  25. </div>
  26. @if (!string.IsNullOrWhiteSpace(Description))
  27. {
  28. <div class="text-xs text-zinc-400 mb-4"
  29. data-testid="@($"{BaseTestId}-description")">
  30. @Description
  31. </div>
  32. }
  33. <!-- Form -->
  34. <EditForm Model="_model"
  35. OnValidSubmit="HandleValidSubmit"
  36. data-testid="@($"{BaseTestId}-form")">
  37. <DataAnnotationsValidator />
  38. @if (!string.IsNullOrEmpty(_error))
  39. {
  40. <div class="text-xs text-red-400 mb-3"
  41. data-testid="@($"{BaseTestId}-error")">
  42. @_error
  43. </div>
  44. }
  45. <div class="text-sm space-y-3"
  46. data-testid="@($"{BaseTestId}-fields")">
  47. <div>
  48. <label class="block text-zinc-400 mb-1">
  49. @KeyLabel
  50. </label>
  51. <InputText class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
  52. data-testid="@($"{BaseTestId}-key-input")"
  53. @bind-Value="_model.Key"
  54. placeholder="@KeyPlaceholder" />
  55. </div>
  56. <div>
  57. <label class="block text-zinc-400 mb-1">
  58. @ValueLabel
  59. </label>
  60. <InputText class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
  61. data-testid="@($"{BaseTestId}-value-input")"
  62. @bind-Value="_model.Value"
  63. placeholder="@ValuePlaceholder" />
  64. </div>
  65. </div>
  66. <!-- Actions -->
  67. <div class="flex justify-end gap-2 mt-5"
  68. data-testid="@($"{BaseTestId}-actions")">
  69. <button type="button"
  70. class="px-3 py-1 rounded border border-zinc-700 text-zinc-300 hover:bg-zinc-800"
  71. data-testid="@($"{BaseTestId}-cancel")"
  72. @onclick="Cancel">
  73. Cancel
  74. </button>
  75. <button type="submit"
  76. class="px-3 py-1 rounded bg-emerald-600 text-black hover:bg-emerald-500"
  77. data-testid="@($"{BaseTestId}-submit")">
  78. Accept
  79. </button>
  80. </div>
  81. </EditForm>
  82. </div>
  83. </div>
  84. }
  85. @code {
  86. [Parameter] public bool IsOpen { get; set; }
  87. [Parameter] public EventCallback<bool> IsOpenChanged { get; set; }
  88. [Parameter] public string Title { get; set; } = "Add label";
  89. [Parameter] public string? Description { get; set; }
  90. [Parameter] public string KeyLabel { get; set; } = "Key";
  91. [Parameter] public string ValueLabel { get; set; } = "Value";
  92. [Parameter] public string? KeyPlaceholder { get; set; }
  93. [Parameter] public string? ValuePlaceholder { get; set; }
  94. [Parameter] public string? Key { get; set; }
  95. [Parameter] public string? Value { get; set; }
  96. [Parameter] public EventCallback<(string Key, string Value)> OnSubmit { get; set; }
  97. [Parameter] public string? TestIdPrefix { get; set; }
  98. private string BaseTestId =>
  99. string.IsNullOrWhiteSpace(TestIdPrefix)
  100. ? "key-value-modal"
  101. : $"{TestIdPrefix}-key-value-modal";
  102. private FormModel _model = new();
  103. private string? _error;
  104. protected override void OnParametersSet()
  105. {
  106. if (IsOpen)
  107. {
  108. _error = null;
  109. _model = new FormModel
  110. {
  111. Key = Key ?? string.Empty,
  112. Value = Value ?? string.Empty
  113. };
  114. }
  115. }
  116. private async Task HandleValidSubmit()
  117. {
  118. _error = null;
  119. try
  120. {
  121. var key = (_model.Key ?? string.Empty).Trim();
  122. var value = (_model.Value ?? string.Empty).Trim();
  123. if (string.IsNullOrWhiteSpace(key))
  124. {
  125. _error = "Key is required.";
  126. return;
  127. }
  128. if (string.IsNullOrWhiteSpace(value))
  129. {
  130. _error = "Value is required.";
  131. return;
  132. }
  133. await OnSubmit.InvokeAsync((key, value));
  134. await Close();
  135. }
  136. catch (Exception ex)
  137. {
  138. _error = ex.Message;
  139. }
  140. }
  141. private async Task Cancel()
  142. {
  143. await Close();
  144. }
  145. private async Task Close()
  146. {
  147. _model = new FormModel();
  148. _error = null;
  149. await IsOpenChanged.InvokeAsync(false);
  150. }
  151. private class FormModel
  152. {
  153. public string? Key { get; set; }
  154. public string? Value { get; set; }
  155. }
  156. }