tabs.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from __future__ import annotations
  2. from typing import Any, Callable, Optional, Union
  3. from .. import globals
  4. from .mixins.disableable_element import DisableableElement
  5. from .mixins.value_element import ValueElement
  6. class Tabs(ValueElement):
  7. def __init__(self, *,
  8. value: Union[Tab, TabPanel, None] = None,
  9. on_change: Optional[Callable[..., Any]] = None,
  10. ) -> None:
  11. """Tabs
  12. This element represents `Quasar's QTabs <https://quasar.dev/vue-components/tabs#qtabs-api>`_ component.
  13. It contains individual tabs.
  14. :param value: `ui.tab`, `ui.tab_panel`, or name of the tab to be initially selected
  15. :param on_change: callback to be executed when the selected tab changes
  16. """
  17. super().__init__(tag='q-tabs', value=value, on_value_change=on_change)
  18. def _value_to_model_value(self, value: Any) -> Any:
  19. return value._props['name'] if isinstance(value, Tab) or isinstance(value, TabPanel) else value
  20. class Tab(DisableableElement):
  21. def __init__(self, name: str, label: Optional[str] = None, icon: Optional[str] = None) -> None:
  22. """Tab
  23. This element represents `Quasar's QTab <https://quasar.dev/vue-components/tabs#qtab-api>`_ component.
  24. It is a child of a `ui.tabs` element.
  25. :param name: name of the tab (will be the value of the `ui.tabs` element)
  26. :param label: label of the tab (default: `None`, meaning the same as `name`)
  27. :param icon: icon of the tab (default: `None`)
  28. """
  29. super().__init__(tag='q-tab')
  30. self._props['name'] = name
  31. self._props['label'] = label if label is not None else name
  32. if icon:
  33. self._props['icon'] = icon
  34. self.tabs = globals.get_slot().parent
  35. class TabPanels(ValueElement):
  36. def __init__(self,
  37. tabs: Tabs, *,
  38. value: Union[Tab, TabPanel, None] = None,
  39. on_change: Optional[Callable[..., Any]] = None,
  40. animated: bool = True,
  41. keep_alive: bool = True,
  42. ) -> None:
  43. """Tab Panels
  44. This element represents `Quasar's QTabPanels <https://quasar.dev/vue-components/tab-panels#qtabpanels-api>`_ component.
  45. It contains individual tab panels.
  46. To avoid issues with dynamic elements when switching tabs,
  47. this element uses Vue's `keep-alive <https://vuejs.org/guide/built-ins/keep-alive.html>`_ component.
  48. If client-side performance is an issue, you can disable this feature.
  49. :param tabs: the `ui.tabs` element that controls this element
  50. :param value: `ui.tab`, `ui.tab_panel`, or name of the tab panel to be initially visible
  51. :param on_change: callback to be executed when the visible tab panel changes
  52. :param animated: whether the tab panels should be animated (default: `True`)
  53. :param keep_alive: whether to use Vue's keep-alive component on the content (default: `True`)
  54. """
  55. super().__init__(tag='q-tab-panels', value=value, on_value_change=on_change)
  56. tabs.bind_value(self, 'value')
  57. self._props['animated'] = animated
  58. self._props['keep-alive'] = keep_alive
  59. def _value_to_model_value(self, value: Any) -> Any:
  60. return value._props['name'] if isinstance(value, Tab) or isinstance(value, TabPanel) else value
  61. class TabPanel(DisableableElement):
  62. def __init__(self, name: Union[Tab, str]) -> None:
  63. """Tab Panel
  64. This element represents `Quasar's QTabPanel <https://quasar.dev/vue-components/tab-panels#qtabpanel-api>`_ component.
  65. It is a child of a `TabPanels` element.
  66. :param name: `ui.tab` or the name of a tab element
  67. """
  68. super().__init__(tag='q-tab-panel')
  69. self._props['name'] = name._props['name'] if isinstance(name, Tab) else name