form.py 3.4 KB

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