input_documentation.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. The callable validation function can also be an async coroutine.
  39. In this case, the validation is performed asynchronously in the background.
  40. You can use the `validate` method of the input element to trigger the validation manually.
  41. It returns `True` if the input is valid, and an error message otherwise.
  42. For async validation functions, the return value must be explicitly disabled by setting `return_result=False`.
  43. ''')
  44. def validation():
  45. ui.input('Name', validation=lambda value: 'Too short' if len(value) < 5 else None)
  46. ui.input('Name', validation={'Too short': lambda value: len(value) >= 5})
  47. doc.reference(ui.input)