numberinput.py 4.6 KB

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