numberinput.py 3.7 KB

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