|
@@ -3,10 +3,10 @@
|
|
|
import dataclasses
|
|
|
from typing import List, Optional
|
|
|
|
|
|
-from reflex.components.component import Component, NoSSRComponent
|
|
|
+from reflex.components.component import NoSSRComponent
|
|
|
from reflex.event import EventHandler, identity_event
|
|
|
from reflex.utils.imports import ImportDict
|
|
|
-from reflex.vars.base import Var
|
|
|
+from reflex.vars.base import LiteralVar, Var
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
@@ -92,6 +92,9 @@ class Moment(NoSSRComponent):
|
|
|
# Display the date in the given timezone.
|
|
|
tz: Var[str]
|
|
|
|
|
|
+ # The locale to use when rendering.
|
|
|
+ locale: Var[str]
|
|
|
+
|
|
|
# Fires when the date changes.
|
|
|
on_change: EventHandler[identity_event(str)]
|
|
|
|
|
@@ -101,22 +104,15 @@ class Moment(NoSSRComponent):
|
|
|
Returns:
|
|
|
The import dict for the component.
|
|
|
"""
|
|
|
+ imports = {}
|
|
|
+
|
|
|
+ if isinstance(self.locale, LiteralVar):
|
|
|
+ imports[""] = f"moment/locale/{self.locale._var_value}"
|
|
|
+ elif self.locale is not None:
|
|
|
+ # If the user is using a variable for the locale, we can't know the
|
|
|
+ # value at compile time so import all locales available.
|
|
|
+ imports[""] = "moment/min/locales"
|
|
|
if self.tz is not None:
|
|
|
- return {"moment-timezone": ""}
|
|
|
- return {}
|
|
|
-
|
|
|
- @classmethod
|
|
|
- def create(cls, *children, **props) -> Component:
|
|
|
- """Create a Moment component.
|
|
|
-
|
|
|
- Args:
|
|
|
- *children: The children of the component.
|
|
|
- **props: The properties of the component.
|
|
|
+ imports["moment-timezone"] = ""
|
|
|
|
|
|
- Returns:
|
|
|
- The Moment Component.
|
|
|
- """
|
|
|
- comp = super().create(*children, **props)
|
|
|
- if "tz" in props:
|
|
|
- comp.lib_dependencies.append("moment-timezone")
|
|
|
- return comp
|
|
|
+ return imports
|