Skip to content

Commit

Permalink
* Modify createElement function
Browse files Browse the repository at this point in the history
  • Loading branch information
nparashar150 committed Oct 5, 2022
1 parent a36928f commit 9d738bc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 50 deletions.
4 changes: 3 additions & 1 deletion babel/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
var App = function App() {
return _index["default"].createElement("div", {
id: "root2"
}, _index["default"].createElement("h1", null, "My First Own React App"), _index["default"].createElement("p", null, "Lets see how it works"));
}, _index["default"].createElement("h1", null, "My First Own React App"), _index["default"].createElement("p", null, _index["default"].createElement("div", {
className: "testing"
}, _index["default"].createElement("h1", null, "My First Own React App"), _index["default"].createElement("p", null, "This is another nesting level"))));
};

_index["default"].render(App(), "root");
Expand Down
67 changes: 20 additions & 47 deletions lib/createElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,41 @@ import { styleToString } from "./utils/styleToString";
* @param {object} state
* @returns {HTMLElement}
*/
export const createElement = (
element,
props = {},
state = {},
children = [],
parentElement = null,
) => {
export const createElement = (element, props = {}, ...children) => {
// create a new element
const _htmlElement = document?.createElement(element);

// add the props to the element
if (typeof props === "object" && Object.keys(props).length > 0) {
if (props && typeof props === "object" && Object.keys(props).length > 0) {
Object.keys(props)?.map((propName) => {
if (propName.includes("on")) {
// if prop is of event type then add event listener
_htmlElement.addEventListener(propName.slice(2), props[propName]);
}
if (propName.toLowerCase() === "classname" || propName === "class") {
} else if (propName.toLowerCase() === "classname") {
// if prop is className
_htmlElement.setAttribute("class", props[propName]);
}
if (propName.toLowerCase() === "style") {
} else if (propName.toLowerCase() === "style") {
// if prop is style
_htmlElement.setAttribute("style", styleToString(props[propName]));
} else {
// rest other propNames
_htmlElement?.setAttribute(propName, props[propName]);
}
// rest other propNames
_htmlElement?.setAttribute(propName, props[propName]);
});
}

// store the state in the element
if (typeof state === "object" && Object.keys(state).length > 0) {
_htmlElement.state = {};
_htmlElement.state = {
...state,
};
}
// append child to parent element if parent element is passed as _htmlElement
if (parentElement && parentElement instanceof HTMLElement) {
parentElement.appendChild(_htmlElement);
}

// add the children to the element
if (
(typeof children === "string") |
(typeof children === "number") |
(typeof children === "boolean") |
(typeof children === "undefined") |
(children === null)
) {
_htmlElement.innerHTML = children;
} else {
children?.map((child) => {
createElement({
element: child?.element,
props: child?.props,
state: child?.state,
children: child?.children,
parentElement: _htmlElement,
});
});
}
children?.map((child) => {
if (
(typeof child === "string") |
(typeof child === "number") |
(typeof child === "boolean") |
(typeof child === "undefined") |
(child === null)
) {
_htmlElement.innerHTML = child;
} else {
_htmlElement.appendChild(child);
}
});

// return the most top-level element
return _htmlElement;
Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import OwnReact from "../lib/index";
const App = () => {
return (
<div id="root2">
{/* <Button /> */}
<h1>My First Own React App</h1>
<p>Lets see how it works</p>
<p>
<div className="testing">
<h1>My First Own React App</h1>
<p>This is another nesting level</p>
</div>
</p>
</div>
);
};
Expand Down

0 comments on commit 9d738bc

Please sign in to comment.