浏览代码

fix link issue with href (#1173)

Thomas Brandého 2 年之前
父节点
当前提交
cd065946a9

+ 2 - 2
pynecone/compiler/utils.py

@@ -12,9 +12,9 @@ from pynecone.components.base import (
     Head,
     Head,
     Html,
     Html,
     Image,
     Image,
-    Link,
     Main,
     Main,
     Meta,
     Meta,
+    RawLink,
     Script,
     Script,
     Title,
     Title,
 )
 )
@@ -155,7 +155,7 @@ def create_document_root(stylesheets: List[str]) -> Component:
     Returns:
     Returns:
         The document root.
         The document root.
     """
     """
-    sheets = [Link.create(rel="stylesheet", href=href) for href in stylesheets]
+    sheets = [RawLink.create(rel="stylesheet", href=href) for href in stylesheets]
     return Html.create(
     return Html.create(
         DocumentHead.create(*sheets),
         DocumentHead.create(*sheets),
         Body.create(
         Body.create(

+ 1 - 1
pynecone/components/base/__init__.py

@@ -3,5 +3,5 @@
 from .body import Body
 from .body import Body
 from .document import ColorModeScript, DocumentHead, Html, Main, Script
 from .document import ColorModeScript, DocumentHead, Html, Main, Script
 from .head import Head
 from .head import Head
-from .link import Link, ScriptTag
+from .link import RawLink, ScriptTag
 from .meta import Description, Image, Meta, Title
 from .meta import Description, Image, Meta, Title

+ 1 - 1
pynecone/components/base/link.py

@@ -5,7 +5,7 @@ from pynecone.components.component import Component
 from pynecone.vars import Var
 from pynecone.vars import Var
 
 
 
 
-class Link(Component):
+class RawLink(Component):
     """A component that displays the title of the current page."""
     """A component that displays the title of the current page."""
 
 
     tag = "link"
     tag = "link"

+ 32 - 0
pynecone/components/navigation/link.py

@@ -1,5 +1,8 @@
 """A link component."""
 """A link component."""
 
 
+from typing import Optional
+
+from pynecone.components.component import Component
 from pynecone.components.libs.chakra import ChakraComponent
 from pynecone.components.libs.chakra import ChakraComponent
 from pynecone.components.navigation.nextlink import NextLink
 from pynecone.components.navigation.nextlink import NextLink
 from pynecone.utils import imports
 from pynecone.utils import imports
@@ -28,3 +31,32 @@ class Link(ChakraComponent):
 
 
     def _get_imports(self) -> imports.ImportDict:
     def _get_imports(self) -> imports.ImportDict:
         return {**super()._get_imports(), **NextLink.create()._get_imports()}
         return {**super()._get_imports(), **NextLink.create()._get_imports()}
+
+    @classmethod
+    def create(
+        cls, *children, href: Optional[Var] = None, rel: Optional[Var] = None, **props
+    ) -> Component:
+        """Create a Link component.
+
+        Args:
+            *children: The children of the component.
+            href (Var): The href attribute of the link. Defaults to None.
+            rel (Var): The rel attribute of the link. Defaults to None.
+            **props: The props of the component.
+
+        Raises:
+            ValueError: in case of missing children
+            ValueError: in case of missing href
+
+        Returns:
+            Component: The link component
+        """
+        if href and not len(children):
+            raise ValueError("Link without a child will not display")
+        elif href is None and len(children):
+            raise ValueError("Link without 'href' props will not work.")
+        else:
+            props.update({"href": href})
+        if rel:
+            props.update({"rel": rel})
+        return super().create(*children, **props)