*/ protected array $startRenderHooks = []; /** * @var array */ protected array $endRenderHooks = []; protected string | Closure | null $livewireProperty = null; protected bool | Closure $isScrollable = true; protected bool | Closure $isVertical = false; final public function __construct(string | Htmlable | Closure | null $label = null) { $this->label($label); } public static function make(string | Htmlable | Closure | null $label = null): static { $static = app(static::class, ['label' => $label]); $static->configure(); return $static; } protected function setUp(): void { parent::setUp(); $this->key(function (Tabs $component): ?string { $label = $component->getLabel(); if (blank($label)) { return null; } $statePath = $component->getStatePath(); return Str::slug(Str::transliterate($label, strict: true)) . '::' . (filled($statePath) ? "{$statePath}::tabs" : 'tabs'); }, isInheritable: false); } /** * @param array | Closure $tabs */ public function tabs(array | Closure $tabs): static { $this->components($tabs); return $this; } /** * @return array | Schema */ public function getDefaultChildComponents(): array | Schema { $components = parent::getDefaultChildComponents(); if (blank($this->getLivewireProperty()) || (! is_array($components))) { return $components; } // Each tab's key must match the array key written into the Livewire // property, so `$set(...)` can activate it. This is done here rather than // during rendering so the key is settled before any absolute keys (such // as those of nested actions) are computed and cached. foreach ($components as $tabKey => $tab) { if (! $tab instanceof Tab) { continue; } $tab->key(strval($tabKey)); } return $components; } public function activeTab(int | Closure $activeTab): static { $this->activeTab = $activeTab; return $this; } public function persistTabInQueryString(string | Closure | null $key = 'tab'): static { $this->tabQueryStringKey = $key; return $this; } public function getActiveTab(): int { if ($this->isTabPersistedInQueryString()) { $queryStringTab = request()->query($this->getTabQueryStringKey()); foreach ($this->getChildSchema()->getComponents() as $index => $tab) { if ($tab->getId() !== $queryStringTab) { continue; } return $index + 1; } } return $this->evaluate($this->activeTab); } public function getTabQueryStringKey(): ?string { return $this->evaluate($this->tabQueryStringKey); } public function isTabPersistedInQueryString(): bool { return filled($this->getTabQueryStringKey()); } /** * @param array $hooks */ public function startRenderHooks(array $hooks): static { $this->startRenderHooks = $hooks; return $this; } /** * @param array $hooks */ public function endRenderHooks(array $hooks): static { $this->endRenderHooks = $hooks; return $this; } /** * @return array */ public function getStartRenderHooks(): array { return $this->startRenderHooks; } /** * @return array */ public function getEndRenderHooks(): array { return $this->endRenderHooks; } /** * @return array */ public function getRenderHookScopes(): array { $livewire = $this->getLivewire(); if (! ($livewire instanceof HasRenderHookScopes)) { return []; } return $livewire->getRenderHookScopes(); } public function livewireProperty(string | Closure | null $property): static { $this->livewireProperty = $property; return $this; } public function getLivewireProperty(): ?string { return $this->evaluate($this->livewireProperty); } public function scrollable(bool | Closure $condition = true): static { $this->isScrollable = $condition; return $this; } public function isScrollable(): bool { return (bool) $this->evaluate($this->isScrollable); } public function vertical(bool | Closure $condition = true): static { $this->isVertical = $condition; return $this; } public function isVertical(): bool { return (bool) $this->evaluate($this->isVertical); } public function toEmbeddedHtml(): string { if (filled($this->getLivewireProperty())) { return $this->toEmbeddedHtmlForLivewireProperty(); } $activeTab = $this->getActiveTab(); $hasDeferredBadges = $this->hasDeferredBadges(); $id = $this->getId(); $isContained = $this->isContained(); $isScrollable = $this->isScrollable(); $isVertical = $this->isVertical(); $label = $this->getLabel(); $renderHookScopes = $this->getRenderHookScopes(); $tabs = array_values(array_filter( $this->getChildSchema()->getComponents(), static fn ($component): bool => $component instanceof Tab, )); $tabsKey = $this->getKey(); $getTabVisibilityJs = static function (Tab $tab, ?int $index = null, ?string $mode = null) use ($isScrollable): ?string { $hiddenJs = $tab->getHiddenJs(); $visibleJs = $tab->getVisibleJs(); $baseJs = match ([filled($hiddenJs), filled($visibleJs)]) { [true, true] => "(! ({$hiddenJs})) && ({$visibleJs})", [true, false] => "! ({$hiddenJs})", [false, true] => $visibleJs, default => null, }; if ($isScrollable || $index === null || $mode === null) { return $baseJs; } $tabKey = $tab->getKey(isAbsolute: false); $dropdownJs = match ($mode) { 'inline' => "(!withinDropdownMounted || withinDropdownIndex === null || {$index} < withinDropdownIndex)", 'trigger' => "(withinDropdownMounted && withinDropdownIndex !== null && {$index} >= withinDropdownIndex && '{$tabKey}' === tab)", default => null, }; return $baseJs ? "{$baseJs} && {$dropdownJs}" : $dropdownJs; }; $outerAttributes = (new FilamentComponentAttributeBag) ->merge([ 'id' => $id, 'wire:key' => $this->getLivewireKey() . '.container', ], escape: false) ->merge($this->getExtraAttributes(), escape: false) ->merge($this->getExtraAlpineAttributes(), escape: false) ->class([ 'fi-sc-tabs', 'fi-contained' => $isContained, 'fi-vertical' => $isVertical, ]); $navAttributes = (new FilamentComponentAttributeBag) ->merge([ 'aria-label' => $label, 'role' => 'tablist', ]) ->class([ 'fi-tabs', 'fi-contained' => $isContained, 'fi-vertical' => $isVertical, ]); if (! $isScrollable) { $navAttributes = $navAttributes->merge([ 'x-bind:class' => '{ \'fi-invisible\': ! withinDropdownMounted }', ], escape: false); } $navAttributes = $navAttributes->merge(['x-cloak' => true], escape: false); $deferredBadgesXData = ''; if ($hasDeferredBadges) { $deferredBadgesXData = '{ deferredBadges: {}, isLoadingDeferredBadges: true, async init() { try { const badges = await $wire.callSchemaComponentMethod(' . Js::from($tabsKey) . ', \'getDeferredTabBadges\') this.deferredBadges = badges ?? {} } finally { this.isLoadingDeferredBadges = false } }, }'; $navAttributes = $navAttributes->merge([ 'x-data' => $deferredBadgesXData, ], escape: false); } $visibleTabKeysJson = collect($tabs) ->filter(static fn (Tab $tab): bool => $tab->isVisible()) ->map(static fn (Tab $tab) => $tab->getKey(isAbsolute: false)) ->values() ->toJson(); $alpineComponentSrc = FilamentAsset::getAlpineComponentSrc('tabs', 'filament/schemas'); ob_start(); ?>
toHtml() ?> >
toHtml() ?>
toHtml() ?>
getLivewireProperty(); $hasDeferredBadges = $this->hasDeferredBadges(); $id = $this->getId(); $isContained = $this->isContained(); $isVertical = $this->isVertical(); $label = $this->getLabel(); $renderHookScopes = $this->getRenderHookScopes(); $tabsKey = $this->getKey(); // Tab keys are overridden with their array keys in // `getDefaultChildComponents()`. /** @var array $tabs */ $tabs = array_filter( $this->getChildSchema()->getComponents(withOriginalKeys: true), static fn ($component): bool => $component instanceof Tab, ); $activeTab = strval($this->getLivewire()->{$livewireProperty}); $outerAttributes = (new FilamentComponentAttributeBag) ->merge([ 'id' => $id, 'wire:key' => $this->getLivewireKey() . '.container', ], escape: false) ->merge($this->getExtraAttributes(), escape: false) ->class([ 'fi-sc-tabs', 'fi-contained' => $isContained, 'fi-vertical' => $isVertical, ]); if ($hasDeferredBadges) { $outerAttributes = $outerAttributes->merge([ 'x-data' => '{ deferredBadges: {}, isLoadingDeferredBadges: true, async init() { try { const badges = await $wire.callSchemaComponentMethod(' . Js::from($tabsKey) . ', \'getDeferredTabBadges\') this.deferredBadges = badges ?? {} } finally { this.isLoadingDeferredBadges = false } }, }', ], escape: false); } $navAttributes = (new FilamentComponentAttributeBag) ->merge([ 'aria-label' => $label, 'role' => 'tablist', ]) ->class([ 'fi-tabs', 'fi-contained' => $isContained, 'fi-vertical' => $isVertical, ]); ob_start(); ?>
toHtml() ?>> toHtml() ?>
| null $color */ protected function generateTabBadgeHtml( string | int | float $badge, string | array | null $color = null, string | BackedEnum | Htmlable | null $icon = null, IconPosition | string | null $iconPosition = null, string | Htmlable | null $tooltip = null, ): string { if (! $iconPosition instanceof IconPosition) { $iconPosition = filled($iconPosition) ? (IconPosition::tryFrom($iconPosition) ?? $iconPosition) : IconPosition::Before; } $badgeAttributes = (new FilamentComponentAttributeBag) ->class(['fi-badge', 'fi-size-sm']) ->color(BadgeComponent::class, $color ?? 'primary'); ob_start(); ?> x-tooltip="{ content: , theme: $store.theme, allowHTML: , }" toHtml() ?> > toHtml() ?> toHtml() ?> toHtml() ?> */ #[ExposedLivewireMethod] #[Renderless] public function getDeferredTabBadges(): array { $badges = []; foreach ($this->getChildSchema()->getComponents(withOriginalKeys: true) as $tabKey => $tab) { if (! $tab instanceof Tab) { continue; } if (! $tab->isBadgeDeferred()) { continue; } $badge = $tab->getBadge(); $badgeColor = $tab->getBadgeColor($badge); $badgeColorClasses = ''; $badgeColorStyles = ''; if (is_array($badgeColor)) { $badgeColorClasses = 'fi-color'; $badgeColorStyles = implode('; ', FilamentColor::getComponentCustomStyles(BadgeComponent::class, $badgeColor)); } elseif (is_string($badgeColor)) { $badgeColorClasses = implode(' ', FilamentColor::getComponentClasses(BadgeComponent::class, $badgeColor)); } $badgeIcon = $tab->getBadgeIcon($badge); $badgeIconHtml = $badgeIcon ? generate_icon_html($badgeIcon, size: IconSize::Small)?->toHtml() : null; $badgeIconPosition = $tab->getBadgeIconPosition($badge); $badgeTooltip = $tab->getBadgeTooltip($badge); $badges[strval($tabKey)] = [ 'badge' => $badge, 'badgeColorClasses' => $badgeColorClasses, 'badgeColorStyles' => $badgeColorStyles, 'badgeIconHtml' => $badgeIconHtml, 'badgeIconPosition' => $badgeIconPosition instanceof IconPosition ? $badgeIconPosition->value : $badgeIconPosition, 'badgeTooltip' => $badgeTooltip ? strval($badgeTooltip) : null, ]; } return $badges; } public function hasDeferredBadges(): bool { foreach ($this->getChildSchema()->getComponents() as $tab) { if (! $tab instanceof Tab) { continue; } if ($tab->isBadgeDeferred()) { return true; } } return false; } }