tabs.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from __future__ import annotations
  2. from typing import Any, Callable, Optional, Union
  3. from .. import context
  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, TabPanel)) else value # pylint: disable=protected-access
  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 = context.get_slot().parent
  35. class TabPanels(ValueElement):
  36. def __init__(self,
  37. tabs: Optional[Tabs] = None, *,
  38. value: Union[Tab, TabPanel, str, 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: an optional `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. if tabs is not None:
  57. tabs.bind_value(self, 'value')
  58. self._props['animated'] = animated
  59. self._props['keep-alive'] = keep_alive
  60. def _value_to_model_value(self, value: Any) -> Any:
  61. return value._props['name'] if isinstance(value, (Tab, TabPanel)) else value # pylint: disable=protected-access
  62. class TabPanel(DisableableElement):
  63. def __init__(self, name: Union[Tab, str]) -> None:
  64. """Tab Panel
  65. This element represents `Quasar's QTabPanel <https://quasar.dev/vue-components/tab-panels#qtabpanel-api>`_ component.
  66. It is a child of a `TabPanels` element.
  67. :param name: `ui.tab` or the name of a tab element
  68. """
  69. super().__init__(tag='q-tab-panel')
  70. self._props['name'] = name._props['name'] if isinstance(name, Tab) else name
  71. self._classes.append('nicegui-tab-panel')