pininput.py 2.6 KB

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