Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/MyReactBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import myReact from "./myReact";

export default class MyReactBuilder {
constructor(type = null) {
this.type = type;
this.props = {};
this.children = [];
this.styleID = null;
this.myTailwindFSPath = "./myTailwind.json";
}

setType = (type) => {
this.type = type;
return this;
};
setProps = (props) => {
this.props = props;
return this;
};
setChildren = (children) => {
this.children = children;
return this;
};
setStyleID = (styleID) => {
this.styleID = styleID;
return this;
};
build = () => {
return new myReact(
this.type,
this.props,
this.children,
this.styleID,
this.myTailwindFSPath,
);
};
}
206 changes: 117 additions & 89 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,106 +1,134 @@
const dataFilePath = "./data.json";
import myReact from "./myReact";
import MyReactBuilder from "./MyReactBuilder";
fetch(dataFilePath)
.then((response) => response.json())
.then((response) => {
let localData = response.personnes;
let inputSearch = new myReact(
"input",
{
type: "text",
placeholder: "search",
onkeyup: (e) => buildTable(searchData(e.target.value, localData)),
},
[],
9
).render();
document.body.appendChild(inputSearch);
buildTable(localData);
// nextWork();
});
.then((response) => response.json())
.then((response) => {
let localData = response.personnes;

// let inputSearch = new myReact(
// "input",
// {
// type: "text",
// placeholder: "search",
// onkeyup: (e) =>
// buildTable(searchData(e.target.value, localData)),
// },
// [],
// 9,
// ).render();

////////////////////////////////////////

let inputSearch = new MyReactBuilder("input")
.setProps({
// add props
type: "text",
placeholder: "search",
onkeyup: (e) =>
buildTable(searchData(e.target.value, localData)),
})
.setChildren([]) // add children
.setStyleID(9) // add styleId
.build() // return the myReact instance
.render(); // execute render method

document.body.appendChild(inputSearch);
buildTable(localData);
// nextWork();
});
const searchData = (query, data) =>
data.filter((element) =>
Object.values(element).some((value) => value.toString().includes(query))
);
data.filter((element) =>
Object.values(element).some((value) =>
value.toString().includes(query),
),
);
const sortData = (field, data, desc = false) =>
buildTable(
data.sort((a, b) =>
desc ? (a[field] > b[field] ? 1 : -1) : b[field] > a[field] ? 1 : -1
)
);
buildTable(
data.sort((a, b) =>
desc
? a[field] > b[field]
? 1
: -1
: b[field] > a[field]
? 1
: -1,
),
);
const buildTable = (data) => {
// let formSearch = new myReact("form", {}, )
// let formSearch = new myReact("form", {}, )

const exist = document.querySelector("table");
exist && exist.remove();
let tdListdata = Object.keys(data[0]).map(
(key) =>
new myReact(
"th",
{},
[
key,
new myReact(
"button",
{ onclick: () => sortData(key, data, true) },
["^"],
6
),
new myReact(
"button",
{ onclick: () => sortData(key, data) },
["v"],
6
),
],
2
)
);
const exist = document.querySelector("table");
exist && exist.remove();
let tdListdata = Object.keys(data[0]).map(
(key) =>
new myReact(
"th",
{},
[
key,
new myReact(
"button",
{ onclick: () => sortData(key, data, true) },
["^"],
6,
),
new myReact(
"button",
{ onclick: () => sortData(key, data) },
["v"],
6,
),
],
2,
),
);

let tabledata = new myReact(
"table",
{},
[
new myReact("thead", {}, [new myReact("tr", {}, tdListdata, 4)], 3),
new myReact(
"tbody",
let tabledata = new myReact(
"table",
{},
data.map(
(persone) =>
[
new myReact("thead", {}, [new myReact("tr", {}, tdListdata, 4)], 3),
new myReact(
"tr",
{},
Object.keys(persone).map(
(key) => new myReact("td", {}, [persone[key]], 5)
),
4
)
),
10
),
],
1
);
"tbody",
{},
data.map(
(persone) =>
new myReact(
"tr",
{},
Object.keys(persone).map(
(key) =>
new myReact("td", {}, [persone[key]], 5),
),
4,
),
),
10,
),
],
1,
);

document.body.appendChild(tabledata.render());
document.body.appendChild(tabledata.render());
};

function nextWork() {
const helloWord = () => {
alert("hello word");
};
const btn = new myReact("button", { onclick: helloWord }, ["Search"], 6);
document.body.appendChild(btn.render());
const btn2 = new myReact("button", { onclick: helloWord }, ["button 2"], 6);
document.body.appendChild(btn2.render());
const h1 = new myReact("h1", { onclick: helloWord }, ["title"], 8);
document.body.appendChild(h1.render());
const helloWord = () => {
alert("hello word");
};
const btn = new myReact("button", { onclick: helloWord }, ["Search"], 6);
document.body.appendChild(btn.render());
const btn2 = new myReact("button", { onclick: helloWord }, ["button 2"], 6);
document.body.appendChild(btn2.render());
const h1 = new myReact("h1", { onclick: helloWord }, ["title"], 8);
document.body.appendChild(h1.render());

const section = new myReact("section", {}, [
new myReact("h4", { style: "color:green" }, ["Hello World"]),
]);
document.body.appendChild(section.render());
const section = new myReact("section", {}, [
new myReact("h4", { style: "color:green" }, ["Hello World"]),
]);
document.body.appendChild(section.render());

const ul = new myReact("ul", {}, [new myReact("li", {}, ["item1"])], 8);
document.body.appendChild(ul.render());
const ul = new myReact("ul", {}, [new myReact("li", {}, ["item1"])], 8);
document.body.appendChild(ul.render());
}
62 changes: 31 additions & 31 deletions src/myReact.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
export default class myReact {
constructor(type, props, children, styleID = null) {
this.type = type;
this.props = props;
this.children = children;
this.styleID = styleID;
this.myTailwindFSPath="./myTailwind.json"
}
render() {
const element = document.createElement(this.type);

for (const prop in this.props) {
element[prop] = this.props[prop];
constructor(type, props, children, styleID = null) {
this.type = type;
this.props = props;
this.children = children;
this.styleID = styleID;
this.myTailwindFSPath = "./myTailwind.json";
}
render() {
const element = document.createElement(this.type);

if (this.styleID) {
fetch(this.myTailwindFSPath)
.then((response) => response.json())
.then((response) => {
const theStyle = response.allStyles.find(
(style) => style.id == parseInt(this.styleID)
);
theStyle && (element.className += theStyle.class);
});
}
for (const prop in this.props) {
element[prop] = this.props[prop];
}

for (const child of this.children) {
if (!(child instanceof myReact)) {
element.innerHTML += child;
} else {
element.appendChild(child.render());
}
}
if (this.styleID) {
fetch(this.myTailwindFSPath)
.then((response) => response.json())
.then((response) => {
const theStyle = response.allStyles.find(
(style) => style.id == parseInt(this.styleID),
);
theStyle && (element.className += " " + theStyle.class);
});
}

return element;
}
for (const child of this.children) {
if (!(child instanceof myReact)) {
element.innerHTML += child;
} else {
element.appendChild(child.render());
}
}

return element;
}
}