form.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. form_refs = {}
  18. for ref in self.get_refs():
  19. # when ref start with refs_ it's an array of refs, so we need different method
  20. # to collect data
  21. if ref.startswith("refs_"):
  22. form_refs[ref[5:-3]] = Var.create(
  23. f"getRefValues({ref[:-3]})", is_local=False
  24. )
  25. else:
  26. form_refs[ref[4:]] = Var.create(f"getRefValue({ref})", is_local=False)
  27. return {"on_submit": form_refs}
  28. class FormControl(ChakraComponent):
  29. """Provide context to form components."""
  30. tag = "FormControl"
  31. # If true, the form control will be disabled.
  32. is_disabled: Var[bool]
  33. # If true, the form control will be invalid.
  34. is_invalid: Var[bool]
  35. # If true, the form control will be readonly
  36. is_read_only: Var[bool]
  37. # If true, the form control will be required.
  38. is_required: Var[bool]
  39. # The label text used to inform users as to what information is requested for a text field.
  40. label: Var[str]
  41. @classmethod
  42. def create(
  43. cls,
  44. *children,
  45. label=None,
  46. input=None,
  47. help_text=None,
  48. error_message=None,
  49. **props,
  50. ) -> Component:
  51. """Create a form control component.
  52. Args:
  53. *children: The children of the form control.
  54. label: The label of the form control.
  55. input: The input of the form control.
  56. help_text: The help text of the form control.
  57. error_message: The error message of the form control.
  58. **props: The properties of the form control.
  59. Raises:
  60. AttributeError: raise an error if missing required kwargs.
  61. Returns:
  62. The form control component.
  63. """
  64. if len(children) == 0:
  65. children = []
  66. if label:
  67. children.append(FormLabel.create(*label))
  68. if not input:
  69. raise AttributeError("input keyword argument is required")
  70. children.append(input)
  71. if help_text:
  72. children.append(FormHelperText.create(*help_text))
  73. if error_message:
  74. children.append(FormErrorMessage.create(*error_message))
  75. return super().create(*children, **props)
  76. class FormHelperText(ChakraComponent):
  77. """A form helper text component."""
  78. tag = "FormHelperText"
  79. class FormLabel(ChakraComponent):
  80. """A form label component."""
  81. tag = "FormLabel"
  82. # Link
  83. html_for: Var[str]
  84. class FormErrorMessage(ChakraComponent):
  85. """A form error message component."""
  86. tag = "FormErrorMessage"