formcontrol.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """Form components."""
  2. from pynecone.components.component import Component
  3. from pynecone.components.libs.chakra import ChakraComponent
  4. from pynecone.vars import Var
  5. class FormControl(ChakraComponent):
  6. """Provide context to form components."""
  7. tag = "FormControl"
  8. # If true, the form control will be disabled.
  9. is_disabled: Var[bool]
  10. # If true, the form control will be invalid.
  11. is_invalid: Var[bool]
  12. # If true, the form control will be readonly
  13. is_read_only: Var[bool]
  14. # If true, the form control will be required.
  15. is_required: Var[bool]
  16. # The label text used to inform users as to what information is requested for a text field.
  17. label: Var[str]
  18. @classmethod
  19. def create(
  20. cls,
  21. *children,
  22. label=None,
  23. input=None,
  24. help_text=None,
  25. error_message=None,
  26. **props
  27. ) -> Component:
  28. """Create a form control component.
  29. Args:
  30. children: The children of the form control.
  31. label: The label of the form control.
  32. input: The input of the form control.
  33. help_text: The help text of the form control.
  34. error_message: The error message of the form control.
  35. props: The properties of the form control.
  36. Raises:
  37. AttributeError: raise an error if missing required kwargs.
  38. Returns:
  39. The form control component.
  40. """
  41. if len(children) == 0:
  42. children = []
  43. if label:
  44. children.append(FormLabel.create(*label))
  45. if not input:
  46. raise AttributeError("input keyword argument is required")
  47. children.append(input)
  48. if help_text:
  49. children.append(FormHelperText.create(*help_text))
  50. if error_message:
  51. children.append(FormErrorMessage.create(*error_message))
  52. return super().create(*children, **props)
  53. class FormHelperText(ChakraComponent):
  54. """A form helper text component."""
  55. tag = "FormHelperText"
  56. class FormLabel(ChakraComponent):
  57. """A form label component."""
  58. tag = "FormLabel"
  59. # Link
  60. html_for: Var[str]
  61. class FormErrorMessage(ChakraComponent):
  62. """A form error message component."""
  63. tag = "FormErrorMessage"