select.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import re
  2. from copy import deepcopy
  3. from pathlib import Path
  4. from typing import Any, Callable, Dict, List, Optional, Union
  5. from nicegui.dependencies import register_vue_component
  6. from .choice_element import ChoiceElement
  7. from .mixins.disableable_element import DisableableElement
  8. register_vue_component('select', Path(__file__).parent / 'select.js')
  9. class Select(ChoiceElement, DisableableElement):
  10. def __init__(self,
  11. options: Union[List, Dict], *,
  12. label: Optional[str] = None,
  13. value: Any = None,
  14. on_change: Optional[Callable[..., Any]] = None,
  15. with_input: bool = False,
  16. multiple: bool = False,
  17. ) -> None:
  18. """Dropdown Selection
  19. The options can be specified as a list of values, or as a dictionary mapping values to labels.
  20. After manipulating the options, call `update()` to update the options in the UI.
  21. :param options: a list ['value1', ...] or dictionary `{'value1':'label1', ...}` specifying the options
  22. :param value: the initial value
  23. :param on_change: callback to execute when selection changes
  24. :param with_input: whether to show an input field to filter the options
  25. :param multiple: whether to allow multiple selections
  26. """
  27. self.multiple = multiple
  28. if multiple:
  29. self.EVENT_ARGS = None
  30. if value is None:
  31. value = []
  32. elif not isinstance(value, list):
  33. value = [value]
  34. super().__init__(tag='select', options=options, value=value, on_change=on_change)
  35. self.use_component('select')
  36. if label is not None:
  37. self._props['label'] = label
  38. if with_input:
  39. self.original_options = deepcopy(options)
  40. self._props['use-input'] = True
  41. self._props['hide-selected'] = not multiple
  42. self._props['fill-input'] = True
  43. self._props['input-debounce'] = 0
  44. self._props['multiple'] = multiple
  45. def on_filter(self, event: Dict) -> None:
  46. self.options = [
  47. option
  48. for option in self.original_options
  49. if not event['args'] or re.search(event['args'], option, re.IGNORECASE)
  50. ]
  51. self.update()
  52. def _msg_to_value(self, msg: Dict) -> Any:
  53. if self.multiple:
  54. return [self._values[arg['value']] for arg in msg['args']]
  55. else:
  56. return self._values[msg['args']['value']]
  57. def _value_to_model_value(self, value: Any) -> Any:
  58. if self.multiple:
  59. result = []
  60. for item in value or []:
  61. try:
  62. index = self._values.index(item)
  63. result.append({'value': index, 'label': self._labels[index]})
  64. except ValueError:
  65. pass
  66. return result
  67. else:
  68. try:
  69. index = self._values.index(value)
  70. return {'value': index, 'label': self._labels[index]}
  71. except ValueError:
  72. return None