Skip to main content

if_cmp

cast_list

def cast_list(
value: Union[ComponentInstance, List[Union[ComponentInstance, None]]]
) -> List[ComponentInstance]

Cast the value to a list if it is not or return original list if it is.

Arguments:

  • value: the value to cast

If

from dara.components.common.if_cmp import If

class If(ModifierComponent)

The If component allows the subsequent children to be rendered based on a condition that will be evaluated at

runtime in the JS code. Conditions are defined with a Condition object, which can be defined manually or by using a normal comparison operator on a Variable instance, e.g. Variable() == True. The component then accepts one required set of children and one optional set. The required children (passed as a single child or array for multiple) will be rendered if the condition is truthy and the optional second set will be rendered if the condition is falsey.

An If component is created like so, in this example it is comparing the values of two variables:

from dara.core import Variable
from dara.components.common import If, Text

var_1 = Variable(True)
var_2 = Variable(False)

If(
condition=var_1 == var_2,
true_children=Text('Equal'),
false_children=Text('Different')
)

Arguments:

  • condition: a condition object
  • true_children: children to display when the condition is met
  • false_children: children to display when the condition is not met, defaults to nothing rendered if not passed

Attributes

  • condition: Condition
  • true_children: List[ComponentInstance]
  • false_children: List[ComponentInstance]
  • Condition: ClassVar[ConditionType]