pininput.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """A pin 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 PinInput(ChakraComponent):
  7. """The component that provides context to all the pin-input fields."""
  8. tag = "PinInput"
  9. # State var to bind the the input.
  10. value: Var[str]
  11. # If true, the pin input receives focus on mount
  12. auto_focus: Var[bool]
  13. # The default value of the pin input
  14. default_value: Var[str]
  15. # The border color when the input is invalid.
  16. error_border_color: Var[str]
  17. # The border color when the input is focused.
  18. focus_border_color: Var[str]
  19. # The top-level id string that will be applied to the input fields. The index of the input will be appended to this top-level id.
  20. id_: Var[str]
  21. # The length of the number input.
  22. length: Var[int]
  23. # If true, the pin input component is put in the disabled state
  24. is_disabled: Var[bool]
  25. # If true, the pin input component is put in the invalid state
  26. is_invalid: Var[bool]
  27. # If true, focus will move automatically to the next input once filled
  28. manage_focus: Var[bool]
  29. # If true, the input's value will be masked just like `type=password`
  30. mask: Var[bool]
  31. # The placeholder for the pin input
  32. placeholder: Var[str]
  33. # The type of values the pin-input should allow ("number" | "alphanumeric").
  34. type_: Var[str]
  35. # "outline" | "flushed" | "filled" | "unstyled"
  36. variant: Var[str]
  37. @classmethod
  38. def get_controlled_triggers(cls) -> Set[str]:
  39. """Get the event triggers that pass the component's value to the handler.
  40. Returns:
  41. The controlled event triggers.
  42. """
  43. return {"on_change", "on_complete"}
  44. @classmethod
  45. def create(cls, *children, **props) -> Component:
  46. """Create a pin input component.
  47. If no children are passed in, the component will create a default pin input
  48. based on the length prop.
  49. Args:
  50. *children: The children of the component.
  51. **props: The props of the component.
  52. Returns:
  53. The pin input component.
  54. """
  55. if not children and "length" in props:
  56. children = [PinInputField()] * props["length"]
  57. return super().create(*children, **props)
  58. class PinInputField(ChakraComponent):
  59. """The text field that user types in - must be a direct child of PinInput."""
  60. tag = "PinInputField"