pininput.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. @classmethod
  39. def get_controlled_triggers(cls) -> Dict[str, Var]:
  40. """Get the event triggers that pass the component's value to the handler.
  41. Returns:
  42. A dict mapping the event trigger to the var that is passed to the handler.
  43. """
  44. return {
  45. "on_change": EVENT_ARG,
  46. "on_complete": EVENT_ARG,
  47. }
  48. @classmethod
  49. def create(cls, *children, **props) -> Component:
  50. """Create a pin input component.
  51. If no children are passed in, the component will create a default pin input
  52. based on the length prop.
  53. Args:
  54. *children: The children of the component.
  55. **props: The props of the component.
  56. Returns:
  57. The pin input component.
  58. """
  59. if not children and "length" in props:
  60. children = [PinInputField()] * props["length"]
  61. return super().create(*children, **props)
  62. class PinInputField(ChakraComponent):
  63. """The text field that user types in - must be a direct child of PinInput."""
  64. tag = "PinInputField"