number.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from typing import Any, Callable, Dict, Optional
  2. from .mixins.disableable_element import DisableableElement
  3. from .mixins.validation_element import ValidationElement
  4. class Number(ValidationElement, 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[..., Any]] = None,
  17. validation: Dict[str, Callable[..., bool]] = {},
  18. ) -> None:
  19. """Number Input
  20. This element is based on Quasar's `QInput <https://quasar.dev/vue-components/input>`_ component.
  21. You can use the `validation` parameter to define a dictionary of validation rules.
  22. The key of the first rule that fails will be displayed as an error message.
  23. :param label: displayed name for the number input
  24. :param placeholder: text to show if no value is entered
  25. :param value: the initial value of the field
  26. :param min: the minimum value allowed
  27. :param max: the maximum value allowed
  28. :param step: the step size for the stepper buttons
  29. :param prefix: a prefix to prepend to the displayed value
  30. :param suffix: a suffix to append to the displayed value
  31. :param format: a string like "%.2f" to format the displayed value
  32. :param on_change: callback to execute when the value changes
  33. :param validation: dictionary of validation rules, e.g. ``{'Too large!': lambda value: value < 3}``
  34. """
  35. self.format = format
  36. super().__init__(tag='q-input', value=value, on_value_change=on_change, validation=validation)
  37. self._props['type'] = 'number'
  38. if label is not None:
  39. self._props['label'] = label
  40. if placeholder is not None:
  41. self._props['placeholder'] = placeholder
  42. if min is not None:
  43. self._props['min'] = min
  44. if max is not None:
  45. self._props['max'] = max
  46. if step is not None:
  47. self._props['step'] = step
  48. if prefix is not None:
  49. self._props['prefix'] = prefix
  50. if suffix is not None:
  51. self._props['suffix'] = suffix
  52. self.on('blur', self.sanitize)
  53. @property
  54. def min(self) -> float:
  55. """The minimum value allowed."""
  56. return self._props.get('min', -float('inf'))
  57. @min.setter
  58. def min(self, value: float) -> None:
  59. self._props['min'] = value
  60. self.sanitize()
  61. @property
  62. def max(self) -> float:
  63. """The maximum value allowed."""
  64. return self._props.get('max', float('inf'))
  65. @max.setter
  66. def max(self, value: float) -> None:
  67. self._props['max'] = value
  68. self.sanitize()
  69. @property
  70. def out_of_limits(self) -> bool:
  71. """Whether the current value is out of the allowed limits."""
  72. return not self.min <= self.value <= self.max
  73. def sanitize(self) -> None:
  74. value = float(self.value or 0)
  75. value = max(value, self.min)
  76. value = min(value, self.max)
  77. self.set_value(float(self.format % value) if self.format else value)
  78. def _msg_to_value(self, msg: Dict) -> Any:
  79. return float(msg['args']) if msg['args'] else None
  80. def _value_to_model_value(self, value: Any) -> Any:
  81. if value is None:
  82. return None
  83. elif self.format is None:
  84. return str(value)
  85. elif value == '':
  86. return 0
  87. else:
  88. return self.format % float(value)
  89. def _value_to_event_value(self, value: Any) -> Any:
  90. return float(value) if value else 0