select.py 3.0 KB

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