input.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """An input component."""
  2. from typing import Dict
  3. from reflex.components.component import EVENT_ARG
  4. from reflex.components.libs.chakra import ChakraComponent
  5. from reflex.utils import imports
  6. from reflex.vars import ImportVar, Var
  7. class Input(ChakraComponent):
  8. """The Input component is a component that is used to get user input in a text field."""
  9. tag = "Input"
  10. # State var to bind the input.
  11. value: Var[str]
  12. # The default value of the input.
  13. default_value: Var[str]
  14. # The placeholder text.
  15. placeholder: Var[str]
  16. # The type of input.
  17. type_: Var[str] = "text" # type: ignore
  18. # The border color when the input is invalid.
  19. error_border_color: Var[str]
  20. # The border color when the input is focused.
  21. focus_border_color: Var[str]
  22. # If true, the form control will be disabled. This has 2 side effects - The FormLabel will have `data-disabled` attribute - The form element (e.g, Input) will be disabled
  23. is_disabled: Var[bool]
  24. # If true, the form control will be invalid. This has 2 side effects - The FormLabel and FormErrorIcon will have `data-invalid` set to true - The form element (e.g, Input) will have `aria-invalid` set to true
  25. is_invalid: Var[bool]
  26. # If true, the form control will be readonly.
  27. is_read_only: Var[bool]
  28. # If true, the form control will be required. This has 2 side effects - The FormLabel will show a required indicator - The form element (e.g, Input) will have `aria-required` set to true
  29. is_required: Var[bool]
  30. # "outline" | "filled" | "flushed" | "unstyled"
  31. variant: Var[str]
  32. # "lg" | "md" | "sm" | "xs"
  33. size: Var[str]
  34. def _get_imports(self) -> imports.ImportDict:
  35. return imports.merge_imports(
  36. super()._get_imports(),
  37. {"/utils/state": {ImportVar(tag="set_val")}},
  38. )
  39. def get_controlled_triggers(self) -> 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.target.value,
  46. "on_focus": EVENT_ARG.target.value,
  47. "on_blur": EVENT_ARG.target.value,
  48. "on_key_down": EVENT_ARG.key,
  49. "on_key_up": EVENT_ARG.key,
  50. }
  51. class InputGroup(ChakraComponent):
  52. """The InputGroup component is a component that is used to group a set of inputs."""
  53. tag = "InputGroup"
  54. class InputLeftAddon(ChakraComponent):
  55. """The InputLeftAddon component is a component that is used to add an addon to the left of an input."""
  56. tag = "InputLeftAddon"
  57. class InputRightAddon(ChakraComponent):
  58. """The InputRightAddon component is a component that is used to add an addon to the right of an input."""
  59. tag = "InputRightAddon"