form.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """Form components."""
  2. from typing import Dict
  3. from reflex.components.component import Component
  4. from reflex.components.libs.chakra import ChakraComponent
  5. from reflex.vars import Var
  6. class Form(ChakraComponent):
  7. """A form component."""
  8. tag = "Box"
  9. # What the form renders to.
  10. as_: Var[str] = "form" # type: ignore
  11. def get_controlled_triggers(self) -> Dict[str, Dict]:
  12. """Get the event triggers that pass the component's value to the handler.
  13. Returns:
  14. A dict mapping the event trigger to the var that is passed to the handler.
  15. """
  16. # Send all the input refs to the handler.
  17. return {
  18. "on_submit": {
  19. ref[4:]: Var.create(f"getRefValue({ref})", is_local=False)
  20. for ref in self.get_refs()
  21. }
  22. }
  23. class FormControl(ChakraComponent):
  24. """Provide context to form components."""
  25. tag = "FormControl"
  26. # If true, the form control will be disabled.
  27. is_disabled: Var[bool]
  28. # If true, the form control will be invalid.
  29. is_invalid: Var[bool]
  30. # If true, the form control will be readonly
  31. is_read_only: Var[bool]
  32. # If true, the form control will be required.
  33. is_required: Var[bool]
  34. # The label text used to inform users as to what information is requested for a text field.
  35. label: Var[str]
  36. @classmethod
  37. def create(
  38. cls,
  39. *children,
  40. label=None,
  41. input=None,
  42. help_text=None,
  43. error_message=None,
  44. **props,
  45. ) -> Component:
  46. """Create a form control component.
  47. Args:
  48. children: The children of the form control.
  49. label: The label of the form control.
  50. input: The input of the form control.
  51. help_text: The help text of the form control.
  52. error_message: The error message of the form control.
  53. props: The properties of the form control.
  54. Raises:
  55. AttributeError: raise an error if missing required kwargs.
  56. Returns:
  57. The form control component.
  58. """
  59. if len(children) == 0:
  60. children = []
  61. if label:
  62. children.append(FormLabel.create(*label))
  63. if not input:
  64. raise AttributeError("input keyword argument is required")
  65. children.append(input)
  66. if help_text:
  67. children.append(FormHelperText.create(*help_text))
  68. if error_message:
  69. children.append(FormErrorMessage.create(*error_message))
  70. return super().create(*children, **props)
  71. class FormHelperText(ChakraComponent):
  72. """A form helper text component."""
  73. tag = "FormHelperText"
  74. class FormLabel(ChakraComponent):
  75. """A form label component."""
  76. tag = "FormLabel"
  77. # Link
  78. html_for: Var[str]
  79. class FormErrorMessage(ChakraComponent):
  80. """A form error message component."""
  81. tag = "FormErrorMessage"