KeyValueModal.razor 6.0 KB

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