Skip to content

tabs_component

django_spire.core.components.tab.tabs_component

TabsComponent

Bases: Component, ABC

A tabbed container whose panes are loaded lazily and may host Glue components, Django fragment views (via Glue.view), plain templates, or full-page links.

Subclasses implement get_tabs() returning a sequence of :class:Tab items (aliased as self.Tab). Only the initially active tab is rendered on first paint; other tabs stay empty until selected.

  • url_param: query parameter synced with history.replaceState and read from request.GET on first render (default 'tab'; set to None to disable).
  • cache_tabs: whether visited panes stay mounted across switches (default True; overridable per tab via Tab(..., cache=False)).
  • tab_invalidated: Glue event wired to invalidateTab(tab_id) so listeners can mark a cached tab stale or force-refresh the active tab.

Tab = Tab class-attribute instance-attribute

template = 'django_spire/component/tab/tabs.html' class-attribute instance-attribute

view_template = 'django_spire/component/page/full_page.html' class-attribute instance-attribute

url_param = 'tab' class-attribute

default_tab = None class-attribute

cache_tabs = True class-attribute

active_tab = Glue.ComponentParameter(None) class-attribute instance-attribute

tab_invalidated = Glue.event() class-attribute instance-attribute

tabs cached property

active_tab_obj property

active_tab_html cached property

tabs_config_json property

__post_init__

Source code in django_spire/core/components/tab/tabs_component.py
def __post_init__(self, request: HttpRequest) -> None:
    super().__post_init__(request)

    valid_pane_ids = [tab.id for tab in self.tabs if not tab.is_link]

    if not valid_pane_ids:
        self.active_tab = None
        return

    if self.active_tab not in valid_pane_ids:
        requested_tab_id = request.GET.get(self.url_param) if self.url_param else None

        if requested_tab_id in valid_pane_ids:
            self.active_tab = requested_tab_id
        elif self.default_tab in valid_pane_ids:
            self.active_tab = self.default_tab
        else:
            self.active_tab = valid_pane_ids[0]

get_tabs abstractmethod

Return the ordered :class:Tab definitions to display.

Source code in django_spire/core/components/tab/tabs_component.py
@abstractmethod
def get_tabs(self) -> Sequence[Tab]:
    """Return the ordered :class:`Tab` definitions to display."""

get_tab

Source code in django_spire/core/components/tab/tabs_component.py
def get_tab(self, tab_id: str | None) -> Tab | None:
    if tab_id is None:
        return None

    for tab in self.tabs:
        if tab.id == tab_id:
            return tab

    return None

should_cache_tab

Source code in django_spire/core/components/tab/tabs_component.py
def should_cache_tab(self, tab: Tab) -> bool:
    return self.cache_tabs if tab.cache is None else tab.cache

load_tab

Source code in django_spire/core/components/tab/tabs_component.py
@Glue.attr(required_access=Glue.Access.VIEW, skip_rerender=True)
def load_tab(self, request: HttpRequest, tab_id: str) -> GlueResponse:
    tab = self.get_tab(tab_id)

    if tab is None or tab.is_link or tab.is_view:
        message = f'{type(self).__name__} has no loadable tab {tab_id!r}.'
        raise PermissionDenied(message)

    if tab.is_component:
        try:
            return self._render_component_tab(request, tab)
        except GlueAuthorizationError as error:
            raise PermissionDenied from error

    return GlueTemplateResponse(
        request,
        tab.template,
        {**tab.context, 'component': self, 'tab': tab},
    )