浏览代码

Fix format route and code cleanup (#399)

Nikhil Rao 2 年之前
父节点
当前提交
6b97de71a0

+ 1 - 1
pynecone/compiler/compiler.py

@@ -17,7 +17,7 @@ DEFAULT_IMPORTS: ImportDict = {
     "next/router": {"useRouter"},
     f"/{constants.STATE_PATH}": {"connect", "updateState", "E"},
     "": {"focus-visible/dist/focus-visible"},
-    "@chakra-ui/react": {"useColorMode"},
+    "@chakra-ui/react": {constants.USE_COLOR_MODE},
 }
 
 

+ 1 - 1
pynecone/compiler/templates.py

@@ -199,4 +199,4 @@ ROUTER = f"const {constants.ROUTER} = useRouter()"
 SOCKET = "const socket = useRef(null)"
 
 # Color toggle
-COLORTOGGLE = "const { colorMode, toggleColorMode } = useColorMode()"
+COLORTOGGLE = f"const {{ {constants.COLOR_MODE}, {constants.TOGGLE_COLOR_MODE} }} = {constants.USE_COLOR_MODE}()"

+ 2 - 7
pynecone/components/base/document.py

@@ -1,6 +1,7 @@
 """Document components."""
 
 from pynecone.components.component import Component
+from pynecone.components.libs.chakra import ChakraComponent
 
 
 class NextDocumentLib(Component):
@@ -33,13 +34,7 @@ class Script(NextDocumentLib):
     tag = "NextScript"
 
 
-class ChakraUiReactLib(Component):
-    """Chakra UI React document components."""
-
-    library = "@chakra-ui/react"
-
-
-class ColorModeScript(ChakraUiReactLib):
+class ColorModeScript(ChakraComponent):
     """Chakra color mode script."""
 
     tag = "ColorModeScript"

+ 0 - 5
pynecone/components/component.py

@@ -162,11 +162,6 @@ class Component(Base, ABC):
         Raises:
             ValueError: If the value is not a valid event chain.
         """
-        # If it's a JS var, return it.
-        if isinstance(value, Var):
-            if value.is_local is False and value.is_string is False:
-                return value
-
         # If it's an event chain var, return it.
         if isinstance(value, Var):
             if value.type_ is not EventChain:

+ 5 - 0
pynecone/constants.py

@@ -226,3 +226,8 @@ SLUG_404 = "[..._]"
 TITLE_404 = "404 - Not Found"
 FAVICON_404 = "favicon.ico"
 DESCRIPTION_404 = "The page was not found"
+
+# Color mode variables
+USE_COLOR_MODE = "useColorMode"
+COLOR_MODE = "colorMode"
+TOGGLE_COLOR_MODE = "toggleColorMode"

+ 4 - 9
pynecone/style.py

@@ -2,17 +2,12 @@
 
 from typing import Optional
 
-from pynecone import utils
-from pynecone.var import Var
+from pynecone import constants, utils
+from pynecone.event import EventChain
+from pynecone.var import BaseVar, Var
 
 
-def toggle_color_mode():
-    """Toggle the color mode.
-
-    Returns:
-        Toggle color mode JS event as a variable
-    """
-    return Var.create(value="toggleColorMode", is_local=False, is_string=False)
+toggle_color_mode = BaseVar(name=constants.TOGGLE_COLOR_MODE, type_=EventChain)
 
 
 def convert(style_dict):

+ 4 - 2
pynecone/utils.py

@@ -952,12 +952,14 @@ def format_route(route: str) -> str:
     Returns:
         The formatted route.
     """
+    # Strip the route.
+    route = route.strip("/")
+    route = to_snake_case(route).replace("_", "-")
+
     # If the route is empty, return the index route.
     if route == "":
         return constants.INDEX_ROUTE
 
-    route = route.strip("/")
-    route = to_snake_case(route).replace("_", "-")
     return route
 
 

+ 1 - 0
tests/test_utils.py

@@ -210,6 +210,7 @@ def test_is_generic_alias(cls: type, expected: bool):
     "route,expected",
     [
         ("", "index"),
+        ("/", "index"),
         ("custom-route", "custom-route"),
         ("custom-route/", "custom-route"),
         ("/custom-route", "custom-route"),