badge.blade.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. @php
  2. use Filament\Support\Enums\IconPosition;
  3. use Filament\Support\Enums\IconSize;
  4. use Filament\Support\Enums\Size;
  5. use Filament\Support\View\Components\BadgeComponent;
  6. use Illuminate\View\ComponentAttributeBag;
  7. @endphp
  8. @props([
  9. 'color' => 'primary',
  10. 'deleteButton' => null,
  11. 'disabled' => false,
  12. 'form' => null,
  13. 'formId' => null,
  14. 'href' => null,
  15. 'icon' => null,
  16. 'iconAlias' => null,
  17. 'iconPosition' => IconPosition::Before,
  18. 'iconSize' => null,
  19. 'keyBindings' => null,
  20. 'loadingIndicator' => true,
  21. 'size' => Size::Medium,
  22. 'spaMode' => null,
  23. 'tag' => 'span',
  24. 'target' => null,
  25. 'tooltip' => null,
  26. 'type' => 'button',
  27. ])
  28. @php
  29. if (! $iconPosition instanceof IconPosition) {
  30. $iconPosition = filled($iconPosition) ? (IconPosition::tryFrom($iconPosition) ?? $iconPosition) : null;
  31. }
  32. if (! $size instanceof Size) {
  33. $size = filled($size) ? (Size::tryFrom($size) ?? $size) : null;
  34. }
  35. if (filled($iconSize) && (! $iconSize instanceof IconSize)) {
  36. $iconSize = IconSize::tryFrom($iconSize) ?? $iconSize;
  37. }
  38. $isDeletable = count($deleteButton?->attributes->getAttributes() ?? []) > 0;
  39. $wireTarget = $loadingIndicator ? $attributes->whereStartsWith(['wire:target', 'wire:click'])->filter(fn ($value): bool => filled($value))->first() : null;
  40. $hasLoadingIndicator = filled($wireTarget) || ($type === 'submit' && filled($form));
  41. if ($hasLoadingIndicator) {
  42. $loadingIndicatorTarget = html_entity_decode($wireTarget ?: $form, ENT_QUOTES);
  43. }
  44. $hasTooltip = filled($tooltip);
  45. @endphp
  46. <{{ $tag }}
  47. @if (($tag === 'a') && (! ($disabled && $hasTooltip)))
  48. {{ \Filament\Support\generate_href_html($href, $target === '_blank', $spaMode) }}
  49. @endif
  50. @if ($keyBindings)
  51. x-bind:id="$id('key-bindings')"
  52. x-mousetrap.global.{{ collect($keyBindings)->map(fn (string $keyBinding): string => str_replace('+', '-', $keyBinding))->implode('.') }}="document.getElementById($el.id)?.click()"
  53. @endif
  54. @if ($hasTooltip)
  55. x-tooltip="{
  56. content: @js($tooltip),
  57. theme: $store.theme,
  58. allowHTML: @js($tooltip instanceof \Illuminate\Contracts\Support\Htmlable),
  59. }"
  60. @endif
  61. {{
  62. $attributes
  63. ->merge([
  64. 'aria-disabled' => $disabled ? 'true' : null,
  65. 'disabled' => $disabled && blank($tooltip),
  66. 'form' => $tag === 'button' ? $formId : null,
  67. 'type' => $tag === 'button' ? $type : null,
  68. 'wire:loading.attr' => $tag === 'button' ? 'disabled' : null,
  69. 'wire:target' => ($hasLoadingIndicator && $loadingIndicatorTarget) ? $loadingIndicatorTarget : null,
  70. ], escape: false)
  71. ->when(
  72. $disabled && $hasTooltip,
  73. fn (ComponentAttributeBag $attributes) => $attributes->filter(
  74. fn (mixed $value, string $key): bool => ! str($key)->startsWith(['href', 'x-on:', 'wire:click']),
  75. ),
  76. )
  77. ->class([
  78. 'fi-badge',
  79. 'fi-disabled' => $disabled,
  80. ($size instanceof Size) ? "fi-size-{$size->value}" : (is_string($size) ? $size : ''),
  81. ])
  82. ->color(BadgeComponent::class, $color)
  83. }}
  84. >
  85. @if ($iconPosition === IconPosition::Before)
  86. @if ($icon)
  87. {{
  88. \Filament\Support\generate_icon_html($icon, $iconAlias, (new \Illuminate\View\ComponentAttributeBag([
  89. 'wire:loading.remove.delay.' . config('filament.livewire_loading_delay', 'default') => $hasLoadingIndicator,
  90. 'wire:target' => $hasLoadingIndicator ? $loadingIndicatorTarget : false,
  91. ])), size: $iconSize ?? \Filament\Support\Enums\IconSize::Small)
  92. }}
  93. @endif
  94. @if ($hasLoadingIndicator)
  95. {{
  96. \Filament\Support\generate_loading_indicator_html((new \Illuminate\View\ComponentAttributeBag([
  97. 'wire:loading.delay.' . config('filament.livewire_loading_delay', 'default') => '',
  98. 'wire:target' => $loadingIndicatorTarget,
  99. ])), size: $iconSize ?? \Filament\Support\Enums\IconSize::Small)
  100. }}
  101. @endif
  102. @endif
  103. <span class="fi-badge-label-ctn">
  104. <span class="fi-badge-label">
  105. {{ $slot }}
  106. </span>
  107. </span>
  108. @if ($isDeletable)
  109. @php
  110. $deleteButtonWireTarget = $deleteButton->attributes->whereStartsWith(['wire:target', 'wire:click'])->filter(fn ($value): bool => filled($value))->first();
  111. $deleteButtonHasLoadingIndicator = filled($deleteButtonWireTarget);
  112. if ($deleteButtonHasLoadingIndicator) {
  113. $deleteButtonLoadingIndicatorTarget = html_entity_decode($deleteButtonWireTarget, ENT_QUOTES);
  114. }
  115. @endphp
  116. <button
  117. type="button"
  118. {{
  119. $deleteButton->attributes
  120. ->except(['label'])
  121. ->class([
  122. 'fi-badge-delete-btn',
  123. ])
  124. }}
  125. >
  126. {{
  127. \Filament\Support\generate_icon_html(\Filament\Support\Icons\Heroicon::XMark, alias: \Filament\Support\View\SupportIconAlias::BADGE_DELETE_BUTTON, attributes: (new \Illuminate\View\ComponentAttributeBag([
  128. 'wire:loading.remove.delay.' . config('filament.livewire_loading_delay', 'default') => $deleteButtonHasLoadingIndicator,
  129. 'wire:target' => $deleteButtonHasLoadingIndicator ? $deleteButtonLoadingIndicatorTarget : false,
  130. ])), size: \Filament\Support\Enums\IconSize::ExtraSmall)
  131. }}
  132. @if ($deleteButtonHasLoadingIndicator)
  133. {{
  134. \Filament\Support\generate_loading_indicator_html((new \Illuminate\View\ComponentAttributeBag([
  135. 'wire:loading.delay.' . config('filament.livewire_loading_delay', 'default') => '',
  136. 'wire:target' => $deleteButtonLoadingIndicatorTarget,
  137. ])), size: \Filament\Support\Enums\IconSize::ExtraSmall)
  138. }}
  139. @endif
  140. @if (filled($label = $deleteButton->attributes->get('label')))
  141. <span class="fi-sr-only">
  142. {{ $label }}
  143. </span>
  144. @endif
  145. </button>
  146. @elseif ($iconPosition === IconPosition::After)
  147. @if ($icon)
  148. {{
  149. \Filament\Support\generate_icon_html($icon, $iconAlias, (new \Illuminate\View\ComponentAttributeBag([
  150. 'wire:loading.remove.delay.' . config('filament.livewire_loading_delay', 'default') => $hasLoadingIndicator,
  151. 'wire:target' => $hasLoadingIndicator ? $loadingIndicatorTarget : false,
  152. ])), size: $iconSize ?? \Filament\Support\Enums\IconSize::Small)
  153. }}
  154. @endif
  155. @if ($hasLoadingIndicator)
  156. {{
  157. \Filament\Support\generate_loading_indicator_html((new \Illuminate\View\ComponentAttributeBag([
  158. 'wire:loading.delay.' . config('filament.livewire_loading_delay', 'default') => '',
  159. 'wire:target' => $loadingIndicatorTarget,
  160. ])), size: $iconSize ?? \Filament\Support\Enums\IconSize::Small)
  161. }}
  162. @endif
  163. @endif
  164. </{{ $tag }}>