number.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from typing import Any, Callable, Dict, Optional
  2. from .mixins.disableable_element import DisableableElement
  3. from .mixins.value_element import ValueElement
  4. class Number(ValueElement, DisableableElement):
  5. LOOPBACK = False
  6. def __init__(self,
  7. label: Optional[str] = None, *,
  8. placeholder: Optional[str] = None,
  9. value: Optional[float] = None,
  10. min: Optional[float] = None,
  11. max: Optional[float] = None,
  12. step: Optional[float] = None,
  13. prefix: Optional[str] = None,
  14. suffix: Optional[str] = None,
  15. format: Optional[str] = None,
  16. on_change: Optional[Callable] = None,
  17. validation: Dict[str, Callable] = {}) -> None:
  18. """Number Input
  19. This element is based on Quasar's `QInput <https://quasar.dev/vue-components/input>`_ component.
  20. You can use the `validation` parameter to define a dictionary of validation rules.
  21. The key of the first rule that fails will be displayed as an error message.
  22. :param label: displayed name for the number input
  23. :param placeholder: text to show if no value is entered
  24. :param value: the initial value of the field
  25. :param min: the minimum value allowed
  26. :param max: the maximum value allowed
  27. :param step: the step size for the stepper buttons
  28. :param prefix: a prefix to prepend to the displayed value
  29. :param suffix: a suffix to append to the displayed value
  30. :param format: a string like "%.2f" to format the displayed value
  31. :param on_change: callback to execute when the input is confirmed by leaving the focus
  32. :param validation: dictionary of validation rules, e.g. ``{'Too small!': lambda value: value < 3}``
  33. """
  34. self.format = format
  35. super().__init__(tag='q-input', value=value, on_value_change=on_change)
  36. self._props['type'] = 'number'
  37. if label is not None:
  38. self._props['label'] = label
  39. if placeholder is not None:
  40. self._props['placeholder'] = placeholder
  41. if min is not None:
  42. self._props['min'] = min
  43. if max is not None:
  44. self._props['max'] = max
  45. if step is not None:
  46. self._props['step'] = step
  47. if prefix is not None:
  48. self._props['prefix'] = prefix
  49. if suffix is not None:
  50. self._props['suffix'] = suffix
  51. self.validation = validation
  52. self.on('blur', self.sanitize)
  53. def sanitize(self) -> None:
  54. value = float(self.value or 0)
  55. value = max(value, self._props.get('min', -float('inf')))
  56. value = min(value, self._props.get('max', float('inf')))
  57. self.set_value(self.format % value if self.format else str(value))
  58. def on_value_change(self, value: Any) -> None:
  59. super().on_value_change(value)
  60. for message, check in self.validation.items():
  61. if not check(value):
  62. self.props(f'error error-message="{message}"')
  63. break
  64. else:
  65. self.props(remove='error')
  66. def _msg_to_value(self, msg: Dict) -> Any:
  67. return float(msg['args']) if msg['args'] else None
  68. def _value_to_model_value(self, value: Any) -> Any:
  69. if value is None:
  70. return None
  71. elif self.format is None:
  72. return str(value)
  73. elif value == '':
  74. return 0
  75. else:
  76. return self.format % float(value)
  77. def _value_to_event_value(self, value: Any) -> Any:
  78. return float(value) if value else 0