Skip to content

tab

django_spire.core.components.tab

__all__ = ['Tab', 'TabsComponent'] module-attribute

Tab dataclass

Declares one tab inside a :class:TabsComponent.

Provide exactly one target: - component: a Glue.Component class or zero-argument factory callable (e.g. partial(EmployeeListComponent, is_active=True)) - view_url: a Django route URL loaded lazily via Glue.view(url) - template: a Django template rendered into the pane (with optional context) - href: a full-page link rendered as an <a> tag instead of swapping panes

id instance-attribute

label instance-attribute

component = None class-attribute instance-attribute

view_url = None class-attribute instance-attribute

template = None class-attribute instance-attribute

context = field(default_factory=dict) class-attribute instance-attribute

href = None class-attribute instance-attribute

icon = None class-attribute instance-attribute

cache = None class-attribute instance-attribute

view_method = 'GET' class-attribute instance-attribute

kind property

is_component property

is_view property

is_template property

__post_init__

Source code in django_spire/core/components/tab/options.py
def __post_init__(self) -> None:
    if not isinstance(self.id, str) or not self.id.strip():
        message = 'Tab.id must be a non-empty string.'
        raise ValueError(message)

    if not isinstance(self.label, str) or not self.label.strip():
        message = 'Tab.label must be a non-empty string.'
        raise ValueError(message)

    targets = [
        name
        for name, value in (
            ('component', self.component),
            ('view_url', self.view_url),
            ('template', self.template),
            ('href', self.href),
        )
        if value is not None
    ]

    if len(targets) != 1:
        message = (
            f'Tab {self.id!r} must set exactly one of component, view_url, template, or href; '
            f'got {targets or "none"}.'
        )
        raise ValueError(message)

    if self.component is not None and not callable(self.component):
        message = f'Tab {self.id!r} component must be a Glue.Component class or factory callable.'
        raise TypeError(message)

    if self.view_method not in ('GET', 'POST'):
        message = f'Tab {self.id!r} view_method must be "GET" or "POST", not {self.view_method!r}.'
        raise ValueError(message)

build_component

Source code in django_spire/core/components/tab/options.py
def build_component(self) -> Glue.Component:
    if self.component is None:
        message = f'Tab {self.id!r} does not have a component.'
        raise ValueError(message)

    instance = self.component()

    if not isinstance(instance, Glue.Component):
        message = (
            f'Tab {self.id!r} component factory must return a Glue.Component instance, '
            f'got {type(instance).__name__}.'
        )
        raise TypeError(message)

    return instance

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},
    )