tabs_documentation.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo('Tabs', '''
  4. The elements `ui.tabs`, `ui.tab`, `ui.tab_panels`, and `ui.tab_panel` resemble
  5. [Quasar's tabs](https://quasar.dev/vue-components/tabs) and
  6. [tab panels](https://quasar.dev/vue-components/tab-panels>) API.
  7. `ui.tabs` creates a container for the tabs. This could be placed in a `ui.header` for example.
  8. `ui.tab_panels` creates a container for the tab panels with the actual content.
  9. Each `ui.tab_panel` is associated with a `ui.tab` element.
  10. ''')
  11. def main_demo() -> None:
  12. with ui.tabs().classes('w-full') as tabs:
  13. one = ui.tab('One')
  14. two = ui.tab('Two')
  15. with ui.tab_panels(tabs, value=two).classes('w-full'):
  16. with ui.tab_panel(one):
  17. ui.label('First tab')
  18. with ui.tab_panel(two):
  19. ui.label('Second tab')
  20. @doc.demo('Name, label, icon', '''
  21. The `ui.tab` element has a `label` property that can be used to display a different text than the `name`.
  22. The `name` can also be used instead of the `ui.tab` objects to associate a `ui.tab` with a `ui.tab_panel`.
  23. Additionally each tab can have an `icon`.
  24. ''')
  25. def name_and_label():
  26. with ui.tabs() as tabs:
  27. ui.tab('h', label='Home', icon='home')
  28. ui.tab('a', label='About', icon='info')
  29. with ui.tab_panels(tabs, value='h').classes('w-full'):
  30. with ui.tab_panel('h'):
  31. ui.label('Main Content')
  32. with ui.tab_panel('a'):
  33. ui.label('Infos')
  34. @doc.demo('Switch tabs programmatically', '''
  35. The `ui.tabs` and `ui.tab_panels` elements are derived from ValueElement which has a `set_value` method.
  36. That can be used to switch tabs programmatically.
  37. ''')
  38. def switch_tabs():
  39. content = {'Tab 1': 'Content 1', 'Tab 2': 'Content 2', 'Tab 3': 'Content 3'}
  40. with ui.tabs() as tabs:
  41. for title in content:
  42. ui.tab(title)
  43. with ui.tab_panels(tabs).classes('w-full') as panels:
  44. for title, text in content.items():
  45. with ui.tab_panel(title):
  46. ui.label(text)
  47. ui.button('GoTo 1', on_click=lambda: panels.set_value('Tab 1'))
  48. ui.button('GoTo 2', on_click=lambda: tabs.set_value('Tab 2'))
  49. doc.reference(ui.tabs, title='Reference for ui.tabs')
  50. doc.reference(ui.tabs, title='Reference for ui.tab')
  51. doc.reference(ui.tabs, title='Reference for ui.tab_panels')
  52. doc.reference(ui.tabs, title='Reference for ui.tab_panel')