icon.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """An icon component."""
  2. from pynecone import utils
  3. from pynecone.components.component import Component
  4. class ChakraIconComponent(Component):
  5. """A component that wraps a Chakra icon component."""
  6. library = "@chakra-ui/icons"
  7. class Icon(ChakraIconComponent):
  8. """An image icon."""
  9. tag = "None"
  10. @classmethod
  11. def create(cls, *children, **props):
  12. """Initialize the Icon component.
  13. Run some additional checks on Icon component.
  14. Args:
  15. children: The positional arguments
  16. props: The keyword arguments
  17. Raises:
  18. AttributeError: The errors tied to bad usage of the Icon component.
  19. ValueError: If the icon tag is invalid.
  20. Returns:
  21. The created component.
  22. """
  23. if children:
  24. raise AttributeError(
  25. f"Passing children to Icon component is not allowed: remove positional arguments {children} to fix"
  26. )
  27. if "tag" not in props.keys():
  28. raise AttributeError("Missing 'tag' keyword-argument for Icon")
  29. if type(props["tag"]) != str or props["tag"].lower() not in ICON_LIST:
  30. raise ValueError(
  31. f"Invalid icon tag: {props['tag']}. Please use one of the following: {ICON_LIST}"
  32. )
  33. props["tag"] = utils.to_title_case(props["tag"]) + "Icon"
  34. return super().create(*children, **props)
  35. # List of all icons.
  36. ICON_LIST = [
  37. "add",
  38. "arrow_back",
  39. "arrow_down",
  40. "arrow_forward",
  41. "arrow_left",
  42. "arrow_right",
  43. "arrow_up",
  44. "arrow_up_down",
  45. "at_sign",
  46. "attachment",
  47. "bell",
  48. "calendar",
  49. "check_circle",
  50. "check",
  51. "chevron_down",
  52. "chevron_left",
  53. "chevron_right",
  54. "chevron_up",
  55. "close",
  56. "copy",
  57. "delete",
  58. "download",
  59. "drag_handle",
  60. "edit",
  61. "email",
  62. "external_link",
  63. "hamburger",
  64. "info",
  65. "info_outline",
  66. "link",
  67. "lock",
  68. "minus",
  69. "moon",
  70. "not_allowed",
  71. "phone",
  72. "plus_square",
  73. "question",
  74. "question_outline",
  75. "repeat",
  76. "repeat_clock",
  77. "search",
  78. "search2",
  79. "settings",
  80. "small_add",
  81. "small_close",
  82. "spinner",
  83. "star",
  84. "sun",
  85. "time",
  86. "triangle_down",
  87. "triangle_up",
  88. "unlock",
  89. "up_down",
  90. "view",
  91. "view_off",
  92. "warning",
  93. "warning_two",
  94. ]