numberinput.py 3.8 KB

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