formcontrol.py 2.9 KB

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