CommaSeparatedStringModal.razor 5.1 KB

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