number.py 4.0 KB

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