input_documentation.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.input)
  4. def main_demo() -> None:
  5. ui.input(label='Text', placeholder='start typing',
  6. on_change=lambda e: result.set_text('you typed: ' + e.value),
  7. validation={'Input too long': lambda value: len(value) < 20})
  8. result = ui.label()
  9. @doc.demo('Autocompletion', '''
  10. The `autocomplete` feature provides suggestions as you type, making input easier and faster.
  11. The parameter `options` is a list of strings that contains the available options that will appear.
  12. ''')
  13. def autocomplete_demo():
  14. options = ['AutoComplete', 'NiceGUI', 'Awesome']
  15. ui.input(label='Text', placeholder='start typing', autocomplete=options)
  16. @doc.demo('Clearable', '''
  17. The `clearable` prop from [Quasar](https://quasar.dev/) adds a button to the input that clears the text.
  18. ''')
  19. def clearable():
  20. i = ui.input(value='some text').props('clearable')
  21. ui.label().bind_text_from(i, 'value')
  22. @doc.demo('Styling', '''
  23. Quasar has a lot of [props to change the appearance](https://quasar.dev/vue-components/input).
  24. It is even possible to style the underlying input with `input-style` and `input-class` props
  25. and use the provided slots to add custom elements.
  26. ''')
  27. def styling():
  28. ui.input(placeholder='start typing').props('rounded outlined dense')
  29. ui.input('styling', value='some text') \
  30. .props('input-style="color: blue" input-class="font-mono"')
  31. with ui.input(value='custom clear button').classes('w-64') as i:
  32. ui.button(color='orange-8', on_click=lambda: i.set_value(None), icon='delete') \
  33. .props('flat dense').bind_visibility_from(i, 'value')
  34. @doc.demo('Input validation', '''
  35. You can validate the input in two ways:
  36. - by passing a callable that returns an error message or `None`, or
  37. - by passing a dictionary that maps error messages to callables that return `True` if the input is valid.
  38. ''')
  39. def validation():
  40. ui.input('Name', validation=lambda value: 'Too short' if len(value) < 5 else None)
  41. ui.input('Name', validation={'Too short': lambda value: len(value) >= 5})
  42. doc.reference(ui.input)