numberinput.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """A number input component."""
  2. from numbers import Number
  3. from typing import Any, Dict
  4. from reflex.components.component import Component
  5. from reflex.components.libs.chakra import (
  6. ChakraComponent,
  7. LiteralInputVariant,
  8. )
  9. from reflex.constants import EventTriggers
  10. from reflex.vars import Var
  11. class NumberInput(ChakraComponent):
  12. """The wrapper that provides context and logic to the components."""
  13. tag = "NumberInput"
  14. # State var to bind the input.
  15. value: Var[Number]
  16. # If true, the input's value will change based on mouse wheel.
  17. allow_mouse_wheel: Var[bool]
  18. # This controls the value update when you blur out of the input. - If true and the value is greater than max, the value will be reset to max - Else, the value remains the same.
  19. clamped_value_on_blur: Var[bool]
  20. # The initial value of the counter. Should be less than max and greater than min
  21. default_value: Var[Number]
  22. # The border color when the input is invalid.
  23. error_border_color: Var[str]
  24. # The border color when the input is focused.
  25. focus_border_color: Var[str]
  26. # If true, the input will be focused as you increment or decrement the value with the stepper
  27. focus_input_on_change: Var[bool]
  28. # Hints at the type of data that might be entered by the user. It also determines the type of keyboard shown to the user on mobile devices ("text" | "search" | "none" | "tel" | "url" | "email" | "numeric" | "decimal")
  29. # input_mode: Var[LiteralInputNumberMode]
  30. # Whether the input should be disabled.
  31. is_disabled: Var[bool]
  32. # If true, the input will have `aria-invalid` set to true
  33. is_invalid: Var[bool]
  34. # If true, the input will be in readonly mode
  35. is_read_only: Var[bool]
  36. # Whether the input is required
  37. is_required: Var[bool]
  38. # Whether the pressed key should be allowed in the input. The default behavior is to allow DOM floating point characters defined by /^[Ee0-9+\-.]$/
  39. is_valid_character: Var[str]
  40. # This controls the value update behavior in general. - If true and you use the stepper or up/down arrow keys, the value will not exceed the max or go lower than min - If false, the value will be allowed to go out of range.
  41. keep_within_range: Var[bool]
  42. # The maximum value of the counter
  43. max_: Var[Number]
  44. # The minimum value of the counter
  45. min_: Var[Number]
  46. # "outline" | "filled" | "flushed" | "unstyled"
  47. variant: Var[LiteralInputVariant]
  48. def get_event_triggers(self) -> Dict[str, Any]:
  49. """Get the event triggers that pass the component's value to the handler.
  50. Returns:
  51. A dict mapping the event trigger to the var that is passed to the handler.
  52. """
  53. return {
  54. **super().get_event_triggers(),
  55. EventTriggers.ON_CHANGE: lambda e0: [e0],
  56. }
  57. @classmethod
  58. def create(cls, *children, **props) -> Component:
  59. """Create a number input component.
  60. If no children are provided, a default stepper will be used.
  61. Args:
  62. *children: The children of the component.
  63. **props: The props of the component.
  64. Returns:
  65. The component.
  66. """
  67. if len(children) == 0:
  68. _id = props.pop("id", None)
  69. children = [
  70. NumberInputField.create(id=_id)
  71. if _id is not None
  72. else NumberInputField.create(),
  73. NumberInputStepper.create(
  74. NumberIncrementStepper.create(),
  75. NumberDecrementStepper.create(),
  76. ),
  77. ]
  78. return super().create(*children, **props)
  79. class NumberInputField(ChakraComponent):
  80. """The input field itself."""
  81. tag = "NumberInputField"
  82. class NumberInputStepper(ChakraComponent):
  83. """The wrapper for the input's stepper buttons."""
  84. tag = "NumberInputStepper"
  85. class NumberIncrementStepper(ChakraComponent):
  86. """The button to increment the value of the input."""
  87. tag = "NumberIncrementStepper"
  88. class NumberDecrementStepper(ChakraComponent):
  89. """The button to decrement the value of the input."""
  90. tag = "NumberDecrementStepper"