PropTypes are at the heart of Mixt. The aim si to specify precisely what kind of data a component is allowed to receive.
It is then "strongly typed" (sort of). And of course largely inspired by React.
It works by defining a class named PropTypes inside your components. (see full
example below).
Then, you must define the type
(using the python typing syntax),
and, if you want, a default value. The format is prop_name: type or
prop_name: type = default.
Special types
¶
Required
¶By default all props are optional. Enclosing a prop type with Required[] make it... required.
Required[] make it... required.Note: A required prop cannot have a default value.
Example
¶
>>> from mixt import Required >>> class Component(Element): ... class PropTypes: ... optional_prop: str ... required_prop: Required[str]
Choices
¶Choices allows to define a list of acceptable value.
Choices allows to define a list of acceptable value.In the PropTypes class, Choices is used as the type. And the value of this prop is the list of choices.
Example
¶
>>> from mixt import Choices >>> class Component(Element): ... class PropTypes: ... size: Choices = ['XS', 'S', 'M', 'L', 'XL', 'XXL']
Special values
¶
Booleans
¶boolean props are handled specifically.
A boolean can be passed:
in python (
<Comp flagged={False} />,<Comp flagged={bool_value} />)as a string (
<Comp flagged=false />,<Comp flagged='false' />)without value (
<Comp flagged />): it is thenTruewith a empty string (
<Comp flagged='' />): it is thenTruewith the name of the prop as value (
<Comp flagged='flagged' />): it is thenTrue
All other case are not valid. Passing without attribute, or with empty string or name of the prop as value is inspired by HTML boolean attributes.
Example
¶
>>> class Component(Element): ... class PropTypes: ... flagged: bool = False >>> <Component />.flagged False >>> <Component flagged />.flagged True >>> <Component flagged=true />.flagged True >>> <Component flagged="true" />.flagged True >>> <Component flagged=TRUE />.flagged True >>> <Component flagged={True} />.flagged True >>> <Component flagged="" />.flagged True >>> <Component flagged="flagged" />.flagged True >>> <Component flagged="other" /> Traceback (most recent call last): ... mixt.exceptions.InvalidPropBoolError: <Component>.flagged: `other` is not a valid choice for this boolean prop (must be in [True, False, 'true', 'false', '', 'flagged']) >>> <Component flagged=false />.flagged False >>> <Component flagged="false" />.flagged False >>> <Component flagged=FALSE />.flagged False >>> <Component flagged={False} />.flagged False
Numbers
¶Numbers (int and float) can be passed as string, and numbers can be passed to strings.
Example
¶
>>> class Component(Element): class PropTypes: num: int string: str >>> <Component num=1 />.num 1 >>> <Component num="1" />.num 1 >>> <Component num={1} />.num 1 >>> <Component num={"1"} /> Traceback (most recent call last): ... mixt.exceptions.InvalidPropValueError: <Component>.num: `1` is not a valid value for this prop (type: <class 'str'>, expected: <class 'int'>) >>> <Component string=1 />.string '1' >>> <Component string={1} />.string '1'
None
¶None will be interpreted as python None if passed directly or as string.
None will be interpreted as python None if passed directly or as string.It is important to note the difference between None and NotProvided.
None is actually a value, and cannot be passed to a prop not having None as possible type.
Example
¶
>>> class Component(Element): ... class PropTypes: ... string: str ... string_or_none: Union[None, str] >>> <Component string_or_none=None />.string_or_none None >>> <Component string_or_none="None" />.string_or_none None >>> <Component string_or_none={"None"} />.string_or_none 'None' >>> <Component string=None />.string Traceback (most recent call last): ... mixt.exceptions.InvalidPropValueError: <Component>.string: `None` is not a valid value for this prop (type: <class 'NoneType'>, expected: <class 'str'>) >>> <Component string="None" />.string Traceback (most recent call last): ... mixt.exceptions.InvalidPropValueError: <Component>.string: `None` is not a valid value for this prop (type: <class 'NoneType'>, expected: <class 'str'>) >>> <Component string="None" />.string 'None'
NotProvided
¶Passing NotProvided (directly, in python or as a string) to a prop is equal to not pass it anything. The prop won't be set.
Example
¶
>>> from mixt import NotProvided >>> class Component(Element): ... class PropTypes: ... string: str >>> <Component string=NotProvided />.string Traceback (most recent call last): ... mixt.exceptions.UnsetPropError: <Component>.string: prop is not set >>> <Component string={NotProvided} />.string Traceback (most recent call last): ... mixt.exceptions.UnsetPropError: <Component>.string: prop is not set >>> <Component string="NotProvided" />.string Traceback (most recent call last): ... mixt.exceptions.UnsetPropError: <Component>.string: prop is not set value = NotProvided >>> <Component string={value} />.string Traceback (most recent call last): ... mixt.exceptions.UnsetPropError: <Component>.string: prop is not set value = "NotProvided" <Component string={value} />.string 'NotProvided'
Comments
¶HTML and python comments are correctly handled but not rendered in the final HTLM.
Example
¶