CommaSeparatedStringModal.razor 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. ✕
  25. </button>
  26. </div>
  27. @if (!string.IsNullOrWhiteSpace(Description))
  28. {
  29. <div class="text-xs text-zinc-400 mb-4"
  30. data-testid="@($"{BaseTestId}-description")">
  31. @Description
  32. </div>
  33. }
  34. <!-- Form -->
  35. <EditForm Model="_model"
  36. OnValidSubmit="HandleValidSubmit"
  37. data-testid="@($"{BaseTestId}-form")">
  38. <DataAnnotationsValidator/>
  39. @if (!string.IsNullOrEmpty(_error))
  40. {
  41. <div class="text-xs text-red-400 mb-3"
  42. data-testid="@($"{BaseTestId}-error")">
  43. @_error
  44. </div>
  45. }
  46. <div class="text-sm"
  47. data-testid="@($"{BaseTestId}-field")">
  48. <label class="block text-zinc-400 mb-1">
  49. @Label
  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}-input")"
  53. @bind-Value="_model.Value"/>
  54. </div>
  55. <!-- Actions -->
  56. <div class="flex justify-end gap-2 mt-5"
  57. data-testid="@($"{BaseTestId}-actions")">
  58. <button type="button"
  59. class="px-3 py-1 rounded border border-zinc-700 text-zinc-300 hover:bg-zinc-800"
  60. data-testid="@($"{BaseTestId}-cancel")"
  61. @onclick="Cancel">
  62. Cancel
  63. </button>
  64. <button type="submit"
  65. class="px-3 py-1 rounded bg-emerald-600 text-black hover:bg-emerald-500"
  66. data-testid="@($"{BaseTestId}-submit")">
  67. Accept
  68. </button>
  69. </div>
  70. </EditForm>
  71. </div>
  72. </div>
  73. }
  74. @code {
  75. [Parameter] public bool IsOpen { get; set; }
  76. [Parameter] public EventCallback<bool> IsOpenChanged { get; set; }
  77. [Parameter] public string Title { get; set; } = "Add values";
  78. [Parameter] public string? Description { get; set; }
  79. [Parameter] public string Label { get; set; } = "Values";
  80. [Parameter] public string? Value { get; set; }
  81. [Parameter] public EventCallback<IReadOnlyList<string>> OnSubmit { get; set; }
  82. [Parameter] public string? TestIdPrefix { get; set; }
  83. private string BaseTestId =>
  84. string.IsNullOrWhiteSpace(TestIdPrefix)
  85. ? "comma-separated-string-modal"
  86. : $"{TestIdPrefix}-comma-separated-string-modal";
  87. private FormModel _model = new();
  88. private string? _error;
  89. protected override void OnParametersSet()
  90. {
  91. if (IsOpen)
  92. {
  93. _error = null;
  94. _model = new FormModel
  95. {
  96. Value = Value
  97. };
  98. }
  99. }
  100. private async Task HandleValidSubmit()
  101. {
  102. _error = null;
  103. try
  104. {
  105. var values = (_model.Value ?? string.Empty)
  106. .Split(',', StringSplitOptions.RemoveEmptyEntries)
  107. .Select(x => x.Trim())
  108. .Where(x => !string.IsNullOrWhiteSpace(x))
  109. .Distinct(StringComparer.OrdinalIgnoreCase)
  110. .ToList();
  111. if (!values.Any())
  112. {
  113. _error = "Please enter at least one value.";
  114. return;
  115. }
  116. await OnSubmit.InvokeAsync(values);
  117. await Close();
  118. }
  119. catch (Exception ex)
  120. {
  121. _error = ex.Message;
  122. }
  123. }
  124. private async Task Cancel()
  125. {
  126. await Close();
  127. }
  128. private async Task Close()
  129. {
  130. _model = new FormModel();
  131. _error = null;
  132. await IsOpenChanged.InvokeAsync(false);
  133. }
  134. private class FormModel
  135. {
  136. [Required] public string? Value { get; set; }
  137. }
  138. }