numberinput.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """A number input component."""
  2. from numbers import Number
  3. from reflex.components.chakra import (
  4. ChakraComponent,
  5. LiteralButtonSize,
  6. LiteralInputVariant,
  7. )
  8. from reflex.components.component import Component
  9. from reflex.event import EventHandler
  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. # "lg" | "md" | "sm" | "xs"
  49. size: Var[LiteralButtonSize]
  50. # The name of the form field
  51. name: Var[str]
  52. # Fired when the input value changes.
  53. on_change: EventHandler[lambda e0: [e0]]
  54. @classmethod
  55. def create(cls, *children, **props) -> Component:
  56. """Create a number input component.
  57. If no children are provided, a default stepper will be used.
  58. Args:
  59. *children: The children of the component.
  60. **props: The props of the component.
  61. Returns:
  62. The component.
  63. """
  64. if len(children) == 0:
  65. _id = props.pop("id", None)
  66. children = [
  67. NumberInputField.create(id=_id)
  68. if _id is not None
  69. else NumberInputField.create(),
  70. NumberInputStepper.create(
  71. NumberIncrementStepper.create(),
  72. NumberDecrementStepper.create(),
  73. ),
  74. ]
  75. return super().create(*children, **props)
  76. class NumberInputField(ChakraComponent):
  77. """The input field itself."""
  78. tag = "NumberInputField"
  79. class NumberInputStepper(ChakraComponent):
  80. """The wrapper for the input's stepper buttons."""
  81. tag = "NumberInputStepper"
  82. class NumberIncrementStepper(ChakraComponent):
  83. """The button to increment the value of the input."""
  84. tag = "NumberIncrementStepper"
  85. class NumberDecrementStepper(ChakraComponent):
  86. """The button to decrement the value of the input."""
  87. tag = "NumberDecrementStepper"