|
@@ -47,7 +47,7 @@ class Tag(Base):
|
|
|
|
|
|
@staticmethod
|
|
@staticmethod
|
|
def format_prop(
|
|
def format_prop(
|
|
- prop: Union[Var, EventChain, ComponentStyle, PropCond, str],
|
|
|
|
|
|
+ prop: Union[Var, EventChain, ComponentStyle, str],
|
|
) -> Union[int, float, str]:
|
|
) -> Union[int, float, str]:
|
|
"""Format a prop.
|
|
"""Format a prop.
|
|
|
|
|
|
@@ -71,10 +71,6 @@ class Tag(Base):
|
|
events = ",".join([utils.format_event(event) for event in prop.events])
|
|
events = ",".join([utils.format_event(event) for event in prop.events])
|
|
prop = f"({local_args}) => Event([{events}])"
|
|
prop = f"({local_args}) => Event([{events}])"
|
|
|
|
|
|
- # Handle conditional props.
|
|
|
|
- elif isinstance(prop, PropCond):
|
|
|
|
- return str(prop)
|
|
|
|
-
|
|
|
|
# Handle other types.
|
|
# Handle other types.
|
|
elif isinstance(prop, str):
|
|
elif isinstance(prop, str):
|
|
if utils.is_wrapped(prop, "{"):
|
|
if utils.is_wrapped(prop, "{"):
|
|
@@ -89,7 +85,7 @@ class Tag(Base):
|
|
if isinstance(prop, dict):
|
|
if isinstance(prop, dict):
|
|
# Convert any var keys to strings.
|
|
# Convert any var keys to strings.
|
|
prop = {
|
|
prop = {
|
|
- key: str(val) if isinstance(val, (Var, PropCond)) else val
|
|
|
|
|
|
+ key: str(val) if isinstance(val, Var) else val
|
|
for key, val in prop.items()
|
|
for key, val in prop.items()
|
|
}
|
|
}
|
|
|
|
|
|
@@ -188,54 +184,3 @@ class Tag(Base):
|
|
Whether the prop is valid.
|
|
Whether the prop is valid.
|
|
"""
|
|
"""
|
|
return prop is not None and not (isinstance(prop, dict) and len(prop) == 0)
|
|
return prop is not None and not (isinstance(prop, dict) and len(prop) == 0)
|
|
-
|
|
|
|
-
|
|
|
|
-class PropCond(Base):
|
|
|
|
- """A conditional prop."""
|
|
|
|
-
|
|
|
|
- # The condition to determine which prop to render.
|
|
|
|
- cond: Var[Any]
|
|
|
|
-
|
|
|
|
- # The prop to render if the condition is true.
|
|
|
|
- prop1: Any
|
|
|
|
-
|
|
|
|
- # The prop to render if the condition is false.
|
|
|
|
- prop2: Any
|
|
|
|
-
|
|
|
|
- @classmethod
|
|
|
|
- def create(cls, cond: Var, prop1: Any, prop2: Any):
|
|
|
|
- """Create a conditional Prop.
|
|
|
|
-
|
|
|
|
- Args:
|
|
|
|
- cond: The cond to determine which prop to render.
|
|
|
|
- prop1: The prop value to render if the cond is true.
|
|
|
|
- prop2: The prop value to render if the cond is false.
|
|
|
|
-
|
|
|
|
- Returns:
|
|
|
|
- The conditional Prop.
|
|
|
|
-
|
|
|
|
- Raises:
|
|
|
|
- ValueError: If the condition or prop values are not set.
|
|
|
|
- """
|
|
|
|
- if cond is None:
|
|
|
|
- raise ValueError("The condition must be set.")
|
|
|
|
- if prop1 is None or prop2 is None:
|
|
|
|
- raise ValueError("Both prop values must be set.")
|
|
|
|
- return cls(
|
|
|
|
- cond=cond,
|
|
|
|
- prop1=prop1,
|
|
|
|
- prop2=prop2,
|
|
|
|
- )
|
|
|
|
-
|
|
|
|
- def __str__(self) -> str:
|
|
|
|
- """Render the prop as a React string.
|
|
|
|
-
|
|
|
|
- Returns:
|
|
|
|
- The React code to render the prop.
|
|
|
|
- """
|
|
|
|
- return utils.format_cond(
|
|
|
|
- cond=self.cond.full_name,
|
|
|
|
- true_value=self.prop1,
|
|
|
|
- false_value=self.prop2,
|
|
|
|
- is_prop=True,
|
|
|
|
- )
|
|
|