Browse Source

Change Strategy Prop to Literal (#3575)

* Better type strategy prop

* Add beter description

* Pre commit

* fix pyi generation

* Default prop assignment must be Var to pass type checking

* fix script.pyi

---------

Co-authored-by: Alek Petuskey <alekpetuskey@Aleks-MacBook-Pro.local>
Co-authored-by: Masen Furer <m_github@0x26.net>
Alek Petuskey 10 tháng trước cách đây
mục cha
commit
1c3d65dd52

+ 6 - 2
reflex/components/base/script.py

@@ -5,6 +5,8 @@ https://nextjs.org/docs/app/api-reference/components/script
 
 
 from __future__ import annotations
 from __future__ import annotations
 
 
+from typing import Literal
+
 from reflex.components.component import Component
 from reflex.components.component import Component
 from reflex.event import EventHandler
 from reflex.event import EventHandler
 from reflex.vars import Var
 from reflex.vars import Var
@@ -27,8 +29,10 @@ class Script(Component):
     # Required unless inline script is used
     # Required unless inline script is used
     src: Var[str]
     src: Var[str]
 
 
-    # When the script will execute: afterInteractive | beforeInteractive | lazyOnload
-    strategy: Var[str] = "afterInteractive"  # type: ignore
+    # When the script will execute: afterInteractive (defer-like behavior) | beforeInteractive | lazyOnload (async-like behavior)
+    strategy: Var[Literal["afterInteractive", "beforeInteractive", "lazyOnload"]] = (
+        Var.create_safe("afterInteractive", _var_is_string=True)
+    )
 
 
     # Triggered when the script is loading
     # Triggered when the script is loading
     on_load: EventHandler[lambda: []]
     on_load: EventHandler[lambda: []]

+ 10 - 2
reflex/components/base/script.pyi

@@ -8,6 +8,7 @@ import reflex
 from reflex.vars import Var, BaseVar, ComputedVar
 from reflex.vars import Var, BaseVar, ComputedVar
 from reflex.event import EventChain, EventHandler, EventSpec
 from reflex.event import EventChain, EventHandler, EventSpec
 from reflex.style import Style
 from reflex.style import Style
+from typing import Literal
 from reflex.components.component import Component
 from reflex.components.component import Component
 from reflex.event import EventHandler
 from reflex.event import EventHandler
 from reflex.vars import Var
 from reflex.vars import Var
@@ -19,7 +20,14 @@ class Script(Component):
         cls,
         cls,
         *children,
         *children,
         src: Optional[Union[reflex.vars.Var[str], str]] = None,
         src: Optional[Union[reflex.vars.Var[str], str]] = None,
-        strategy: Optional[Union[reflex.vars.Var[str], str]] = None,
+        strategy: Optional[
+            Union[
+                reflex.vars.Var[
+                    Literal["afterInteractive", "beforeInteractive", "lazyOnload"]
+                ],
+                Literal["afterInteractive", "beforeInteractive", "lazyOnload"],
+            ]
+        ] = None,
         style: Optional[Style] = None,
         style: Optional[Style] = None,
         key: Optional[Any] = None,
         key: Optional[Any] = None,
         id: Optional[Any] = None,
         id: Optional[Any] = None,
@@ -97,7 +105,7 @@ class Script(Component):
         Args:
         Args:
             *children: The children of the component.
             *children: The children of the component.
             src: Required unless inline script is used
             src: Required unless inline script is used
-            strategy: When the script will execute: afterInteractive | beforeInteractive | lazyOnload
+            strategy: When the script will execute: afterInteractive (defer-like behavior) | beforeInteractive | lazyOnload (async-like behavior)
             style: The style of the component.
             style: The style of the component.
             key: A unique key for the component.
             key: A unique key for the component.
             id: The id for the component.
             id: The id for the component.