1
0

tabs_documentation.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.demo('Vertical tabs with splitter', '''
  50. Like in [Quasar's vertical tabs example](https://quasar.dev/vue-components/tabs#vertical),
  51. we can combine `ui.splitter` and tab elements to create a vertical tabs layout.
  52. ''')
  53. def vertical_tabs():
  54. with ui.splitter(value=30).classes('w-full h-56') as splitter:
  55. with splitter.before:
  56. with ui.tabs().props('vertical').classes('w-full') as tabs:
  57. mail = ui.tab('Mails', icon='mail')
  58. alarm = ui.tab('Alarms', icon='alarm')
  59. movie = ui.tab('Movies', icon='movie')
  60. with splitter.after:
  61. with ui.tab_panels(tabs, value=mail) \
  62. .props('vertical').classes('w-full h-full'):
  63. with ui.tab_panel(mail):
  64. ui.label('Mails').classes('text-h4')
  65. ui.label('Content of mails')
  66. with ui.tab_panel(alarm):
  67. ui.label('Alarms').classes('text-h4')
  68. ui.label('Content of alarms')
  69. with ui.tab_panel(movie):
  70. ui.label('Movies').classes('text-h4')
  71. ui.label('Content of movies')
  72. doc.reference(ui.tabs, title='Reference for ui.tabs')
  73. doc.reference(ui.tabs, title='Reference for ui.tab')
  74. doc.reference(ui.tabs, title='Reference for ui.tab_panels')
  75. doc.reference(ui.tabs, title='Reference for ui.tab_panel')