domelemjs 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +140 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# DOMelemJS
|
|
2
|
+
This lightweight tool is allow you to render HTML lements dynamically form JavaScript!
|
|
3
|
+
|
|
4
|
+
There is two approach and two to use it:
|
|
5
|
+
1. is the Object approach:
|
|
6
|
+
This is a tiny object with name DOMElem!
|
|
7
|
+
|
|
8
|
+
Whit this approach you can create HTML element as follows:
|
|
9
|
+
Elem.Create({
|
|
10
|
+
tag: "div",
|
|
11
|
+
attrs: { class: "app" },
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
that gives you a div with a class: app:
|
|
15
|
+
<div class="app"></div>
|
|
16
|
+
|
|
17
|
+
2 the second way is through a function that named: createDOMElem()
|
|
18
|
+
|
|
19
|
+
With this approach you should use as follows:
|
|
20
|
+
const app = createDOMElem({
|
|
21
|
+
tag: "div",
|
|
22
|
+
attrs: { id: "app" },
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
that gives you a div with a id: app:
|
|
26
|
+
<div id="app"></div>
|
|
27
|
+
|
|
28
|
+
With this renderer you will be able to add eventListeners and styles in the moment as the DOM is created!
|
|
29
|
+
e.g.:
|
|
30
|
+
createDOMElem({
|
|
31
|
+
tag: "h2",
|
|
32
|
+
text: "It's amazing",
|
|
33
|
+
parent: app,
|
|
34
|
+
style: { color: "red", "background-color": "green" },
|
|
35
|
+
attrs: { id: "title" },
|
|
36
|
+
handleEvent: {
|
|
37
|
+
event: "click",
|
|
38
|
+
cb: (e) => console.log(e.target.id),
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
* Handle event is an object or an array of object, that should be conain:
|
|
43
|
+
* event, what will fire the event?
|
|
44
|
+
* and a cb, that is the callback function
|
|
45
|
+
* you can ad as many events as you want easily in an array!
|
|
46
|
+
|
|
47
|
+
You can add the children of the element same time as the element is created:
|
|
48
|
+
const select = createDOMElem({
|
|
49
|
+
tag: "select",
|
|
50
|
+
parent: app,
|
|
51
|
+
attrs: { id: "selector" },
|
|
52
|
+
children: [
|
|
53
|
+
{
|
|
54
|
+
tag: "option",
|
|
55
|
+
content: "foo",
|
|
56
|
+
attr: { value: "foo" },
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
tag: "option",
|
|
60
|
+
content: "bar",
|
|
61
|
+
attr: { value: "bar" },
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
handleEvent: {
|
|
65
|
+
event: "change",
|
|
66
|
+
cb: (e) => console.log(e.target.value),
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
* Adding stye is also possible as:
|
|
71
|
+
* * a string, with a bunch of style properties, it can be:
|
|
72
|
+
* * * CCS style (e.g. background-color) or
|
|
73
|
+
* * * JS/camelCase (e.g. backgroundColor) style formatted version also.
|
|
74
|
+
* * or an object formatted (e.g. style: { backgroundColor: red })
|
|
75
|
+
* * or an array with multiple style strings with CSS or JS vesrion, or mixed
|
|
76
|
+
|
|
77
|
+
And a complex structure semms like this here:
|
|
78
|
+
dateFilterContainer = document.body.appendChild(
|
|
79
|
+
Elem.Create({
|
|
80
|
+
tag: "div",
|
|
81
|
+
attrs: { class: "dateFilter-Container", id: "dateFilter-Container" },
|
|
82
|
+
children: [
|
|
83
|
+
Elem.Create({
|
|
84
|
+
tag: "div",
|
|
85
|
+
attrs: {
|
|
86
|
+
class: "beginDate-container",
|
|
87
|
+
id: "beginDate-container",
|
|
88
|
+
},
|
|
89
|
+
children: [
|
|
90
|
+
Elem.Create({
|
|
91
|
+
tag: "label",
|
|
92
|
+
attrs: {
|
|
93
|
+
class: "beginDate-lable",
|
|
94
|
+
},
|
|
95
|
+
content: "Kezdő dátum: ",
|
|
96
|
+
}),
|
|
97
|
+
Elem.Create({
|
|
98
|
+
tag: "input",
|
|
99
|
+
attrs: {
|
|
100
|
+
type: "date",
|
|
101
|
+
class: "beginDate",
|
|
102
|
+
id: "beginDate",
|
|
103
|
+
},
|
|
104
|
+
eventStarter: "change",
|
|
105
|
+
eventFunction: function (e) {
|
|
106
|
+
e.preventDefault();
|
|
107
|
+
console.log(this.value);
|
|
108
|
+
},
|
|
109
|
+
}),
|
|
110
|
+
],
|
|
111
|
+
}),
|
|
112
|
+
Elem.Create({
|
|
113
|
+
tag: "div",
|
|
114
|
+
attrs: { class: "endDate-container", id: "endDate-container" },
|
|
115
|
+
children: [
|
|
116
|
+
Elem.Create({
|
|
117
|
+
tag: "label",
|
|
118
|
+
attrs: {
|
|
119
|
+
class: "endDate-lable",
|
|
120
|
+
},
|
|
121
|
+
content: "Befejező dátum: ",
|
|
122
|
+
}),
|
|
123
|
+
Elem.Create({
|
|
124
|
+
tag: "input",
|
|
125
|
+
attrs: {
|
|
126
|
+
type: "date",
|
|
127
|
+
class: "endDate",
|
|
128
|
+
id: "endDate",
|
|
129
|
+
},
|
|
130
|
+
eventStarter: "change",
|
|
131
|
+
eventFunction: function (e) {
|
|
132
|
+
e.preventDefault();
|
|
133
|
+
console.log(this.value);
|
|
134
|
+
},
|
|
135
|
+
}),
|
|
136
|
+
],
|
|
137
|
+
}),
|
|
138
|
+
],
|
|
139
|
+
})
|
|
140
|
+
);
|