select.py 680 B

1234567891011121314151617181920212223
  1. import justpy as jp
  2. from typing import Callable, List, Dict, Union
  3. from .choice_element import ChoiceElement
  4. class Select(ChoiceElement):
  5. def __init__(self,
  6. options: Union[List, Dict],
  7. value: any = None,
  8. design: str = '',
  9. on_change: Callable = None):
  10. view = jp.QSelect(options=options, input=self.handle_change)
  11. super().__init__(view, design, value, options, on_change)
  12. def value_to_view(self, value: any):
  13. matches = [o for o in self.view.options if o['value'] == value]
  14. if any(matches):
  15. return matches[0]['label']
  16. else:
  17. return value