-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement.py
More file actions
53 lines (43 loc) · 1.75 KB
/
element.py
File metadata and controls
53 lines (43 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""Lightweight element descriptors for the virtual view tree.
An Element is an immutable description of a UI node — analogous to a React
element. It captures a type (name string **or** component function), a props
dictionary, and an ordered list of children without creating any native
platform objects. The reconciler consumes these trees to determine what
native views must be created, updated, or removed.
"""
from typing import Any, Dict, List, Optional, Union
class Element:
"""Immutable description of a single UI node.
``type_name`` may be a *string* (e.g. ``"Text"``) for built-in native
elements or a *callable* for function components decorated with
:func:`~pythonnative.hooks.component`.
"""
__slots__ = ("type", "props", "children", "key")
def __init__(
self,
type_name: Union[str, Any],
props: Dict[str, Any],
children: List["Element"],
key: Optional[str] = None,
) -> None:
self.type = type_name
self.props = props
self.children = children
self.key = key
def __repr__(self) -> str:
t = self.type if isinstance(self.type, str) else getattr(self.type, "__name__", repr(self.type))
return f"Element({t!r}, props={set(self.props)}, children={len(self.children)})"
def __eq__(self, other: object) -> bool:
if not isinstance(other, Element):
return NotImplemented
return (
self.type == other.type
and self.props == other.props
and self.children == other.children
and self.key == other.key
)
def __ne__(self, other: object) -> bool:
result = self.__eq__(other)
if result is NotImplemented:
return result
return not result