number.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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[..., 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 small!': lambda value: value < 3}``
  34. """
  35. self.format = format
  36. super().__init__(tag='q-input', value=value, on_value_change=on_change)
  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.validation = validation
  53. self.on('blur', self.sanitize)
  54. def sanitize(self) -> None:
  55. value = float(self.value or 0)
  56. value = max(value, self._props.get('min', -float('inf')))
  57. value = min(value, self._props.get('max', float('inf')))
  58. self.set_value(float(self.format % value) if self.format else value)
  59. def on_value_change(self, value: Any) -> None:
  60. super().on_value_change(value)
  61. for message, check in self.validation.items():
  62. if not check(value):
  63. self.props(f'error error-message="{message}"')
  64. break
  65. else:
  66. self.props(remove='error')
  67. def _msg_to_value(self, msg: Dict) -> Any:
  68. return float(msg['args']) if msg['args'] else None
  69. def _value_to_model_value(self, value: Any) -> Any:
  70. if value is None:
  71. return None
  72. elif self.format is None:
  73. return str(value)
  74. elif value == '':
  75. return 0
  76. else:
  77. return self.format % float(value)
  78. def _value_to_event_value(self, value: Any) -> Any:
  79. return float(value) if value else 0