number.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. from typing import Any, Callable, Dict, Optional, Union
  2. from ..events import GenericEventArguments
  3. from .mixins.disableable_element import DisableableElement
  4. from .mixins.validation_element import ValidationElement
  5. class Number(ValidationElement, DisableableElement):
  6. LOOPBACK = False
  7. def __init__(self,
  8. label: Optional[str] = None, *,
  9. placeholder: Optional[str] = None,
  10. value: Optional[float] = None,
  11. min: Optional[float] = None, # pylint: disable=redefined-builtin
  12. max: Optional[float] = None, # pylint: disable=redefined-builtin
  13. precision: Optional[int] = None,
  14. step: Optional[float] = None,
  15. prefix: Optional[str] = None,
  16. suffix: Optional[str] = None,
  17. format: Optional[str] = None, # pylint: disable=redefined-builtin
  18. on_change: Optional[Callable[..., Any]] = None,
  19. validation: Optional[Union[Callable[..., Optional[str]], Dict[str, Callable[..., bool]]]] = None,
  20. ) -> None:
  21. """Number Input
  22. This element is based on Quasar's `QInput <https://quasar.dev/vue-components/input>`_ component.
  23. You can use the `validation` parameter to define a dictionary of validation rules,
  24. e.g. ``{'Too small!': lambda value: value < 3}``.
  25. The key of the first rule that fails will be displayed as an error message.
  26. Alternatively, you can pass a callable that returns an optional error message.
  27. To disable the automatic validation on every value change, you can use the `without_auto_validation` method.
  28. :param label: displayed name for the number input
  29. :param placeholder: text to show if no value is entered
  30. :param value: the initial value of the field
  31. :param min: the minimum value allowed
  32. :param max: the maximum value allowed
  33. :param precision: the number of decimal places allowed (default: no limit, negative: decimal places before the dot)
  34. :param step: the step size for the stepper buttons
  35. :param prefix: a prefix to prepend to the displayed value
  36. :param suffix: a suffix to append to the displayed value
  37. :param format: a string like "%.2f" to format the displayed value
  38. :param on_change: callback to execute when the value changes
  39. :param validation: dictionary of validation rules or a callable that returns an optional error message
  40. """
  41. self.format = format
  42. super().__init__(tag='q-input', value=value, on_value_change=on_change, validation=validation)
  43. self._props['type'] = 'number'
  44. if label is not None:
  45. self._props['label'] = label
  46. if placeholder is not None:
  47. self._props['placeholder'] = placeholder
  48. if min is not None:
  49. self._props['min'] = min
  50. if max is not None:
  51. self._props['max'] = max
  52. self._precision = precision
  53. if step is not None:
  54. self._props['step'] = step
  55. if prefix is not None:
  56. self._props['prefix'] = prefix
  57. if suffix is not None:
  58. self._props['suffix'] = suffix
  59. self.on('blur', self.sanitize, [])
  60. @property
  61. def min(self) -> float:
  62. """The minimum value allowed."""
  63. return self._props.get('min', -float('inf'))
  64. @min.setter
  65. def min(self, value: float) -> None:
  66. if self._props.get('min') == value:
  67. return
  68. self._props['min'] = value
  69. self.sanitize()
  70. self.update()
  71. @property
  72. def max(self) -> float:
  73. """The maximum value allowed."""
  74. return self._props.get('max', float('inf'))
  75. @max.setter
  76. def max(self, value: float) -> None:
  77. if self._props.get('max') == value:
  78. return
  79. self._props['max'] = value
  80. self.sanitize()
  81. self.update()
  82. @property
  83. def precision(self) -> Optional[int]:
  84. """The number of decimal places allowed (default: no limit, negative: decimal places before the dot)."""
  85. return self._precision
  86. @precision.setter
  87. def precision(self, value: Optional[int]) -> None:
  88. self._precision = value
  89. self.sanitize()
  90. @property
  91. def out_of_limits(self) -> bool:
  92. """Whether the current value is out of the allowed limits."""
  93. return not self.min <= self.value <= self.max
  94. def sanitize(self) -> None:
  95. """Sanitize the current value to be within the allowed limits."""
  96. if self.value is None:
  97. return
  98. value = float(self.value)
  99. value = max(value, self.min)
  100. value = min(value, self.max)
  101. if self.precision is not None:
  102. value = float(round(value, self.precision))
  103. self.set_value(float(self.format % value) if self.format else value)
  104. def _event_args_to_value(self, e: GenericEventArguments) -> Any:
  105. if not e.args:
  106. return None
  107. return float(e.args)
  108. def _value_to_model_value(self, value: Any) -> Any:
  109. if value is None:
  110. return None
  111. if self.format is None:
  112. old_value = float(self._props.get(self.VALUE_PROP) or 0)
  113. if old_value == int(old_value) and value == int(value):
  114. return str(int(value)) # preserve integer representation
  115. return str(value)
  116. if value == '':
  117. return 0
  118. return self.format % float(value)
  119. def _value_to_event_value(self, value: Any) -> Any:
  120. return float(value) if value else 0