numberinput.py 4.0 KB

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