Exemplo n.º 1
0
        public ReactNode Render(ReactElement element, ReactNode nodeOld, IReactRendererDOM dom)
        {
            dom.Start();
            var node = RenderElement(element, nodeOld, dom, 0, 0);

            dom.End();
            return(node);
        }
Exemplo n.º 2
0
        protected ReactNode RenderElement(ReactElement element, ReactNode nodeOld, IReactRendererDOM dom,
                                          long parentDomId, long beforeDomId)
        {
            ReactNode node;
            long      domId = 0;

            if (nodeOld != null &&
                ((!(nodeOld.Data is ReactElement)) || ((ReactElement)nodeOld.Data).Type != element.Type))
            {
                RemoveNode(nodeOld, dom);
                nodeOld = null;
            }

            var isDomElement = (element.Type is string);

            if (isDomElement)
            {
                var propsNew = element.Props.Where(i => !string.Equals(i.Key, "Children"))
                               .ToDictionary(i => i.Key, i => i.Value);

                if (nodeOld == null)
                {
                    domId = dom.CreateElement(element.Type.ToString(), propsNew, parentDomId, beforeDomId);
                }
                else
                {
                    domId = nodeOld.DomId;
                    var propsChanges = DiffProps(propsNew, nodeOld.Props);

                    if (propsChanges.Any())
                    {
                        dom.SetProps(domId, propsChanges);
                    }
                }

                node       = ReactNode.Create(_runtime);
                node.DomId = domId;
                node.Data  = element;
                node.Props = propsNew;
            }
            else if (element.Type is Type componentType)
            {
                var propsNew = element.Props;
                if (nodeOld == null)
                {
                    node                = ReactNode.Create(_runtime);
                    node.Data           = element;
                    node.Component      = (ReactComponent)Activator.CreateInstance(componentType);
                    node.Component.Node = node;
                    node.Props          = propsNew;
                    node.Component.SetProps(propsNew);
                    node.Component.ComponentWillMount();
                }
                else
                {
                    node                = ReactNode.Create(_runtime);
                    node.Data           = element;
                    node.Component      = nodeOld.Component;
                    node.Props          = element.Props;
                    node.Component.Node = node;

                    var propsChanhes = DiffProps(propsNew, nodeOld.Props);
                    if (propsChanhes.Any())
                    {
                        node.Component.SetProps(propsNew);
                    }
                }
            }
            else
            {
                throw new Exception();
            }

            if (isDomElement)
            {
                if (element.Props.TryGetValue("Children", out var childrenObj))
                {
                    long firstChildrenDomId;
                    node.Children = RenderChildren(childrenObj, nodeOld?.Children ?? new List <ReactNode>(), dom, domId,
                                                   0, out firstChildrenDomId);
                }
            }
            else
            {
                long firstChildrenDomId;
                node.Children = RenderChildren(node.Component.Render(), nodeOld?.Children ?? new List <ReactNode>(), dom,
                                               parentDomId, beforeDomId, out firstChildrenDomId);
                node.DomId = firstChildrenDomId;
            }

            return(node);
        }