Skip to content

Commit 6d7274a

Browse files
committed
v0.3.0
1 parent 61cf649 commit 6d7274a

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
import Component from "./Components.js";
3+
4+
const d = document;
5+
const app = new Component({
6+
element : "#todo-list",
7+
data : {
8+
todoList: [],
9+
},
10+
template : function(props){
11+
if(props.todoList.length < 1)return `<p>Empty to do List.</p>`;
12+
let todos = props.todoList.map( task => `<li>${task}</li>`).join("");
13+
return todos;
14+
}
15+
});
16+
17+
d.addEventListener('DOMContentLoaded', app.render);
18+
19+
d.addEventListener('submit', e=>{
20+
if(!e.target.matches('#todo-form'))return false;
21+
e.preventDefault();
22+
const $item = d.getElementById('todo-item');
23+
if(!$item)return;
24+
25+
const lastState = app.getState();
26+
// Actualizar el State de forma Reactiva
27+
lastState.todoList.push($item.value);
28+
app.setState({todoList : lastState.todoList});
29+
30+
// Limpiamos el input
31+
$item.value = "";
32+
$item.focus();
33+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
export const Component = (function(){
4+
5+
/**
6+
* Constructor del Componente
7+
*/
8+
const Constructor = function(options){
9+
this.element = options.element;
10+
this.data = options.data;
11+
this.template = options.template;
12+
}
13+
14+
// ------------- Metodos -------------
15+
16+
/**
17+
* Render UI
18+
*/
19+
Constructor.prototype.render = function(){
20+
const $element = document.querySelector(this.element);
21+
if(!$element)return;
22+
$element.innerHTML = this.template(this.data);
23+
}
24+
/**
25+
* Actualizamos el Estado de forma Reactiva
26+
*/
27+
Constructor.prototype.setState = function(object){
28+
for (const key in object) {
29+
if (this.data.hasOwnProperty(key)) {
30+
this.data[key] = object[key] ;
31+
}
32+
}
33+
this.render();
34+
}
35+
36+
/**
37+
* Obtenemos una copia Inmutable del Estado
38+
*/
39+
Constructor.prototype.getState = function(){
40+
return JSON.parse(JSON.stringify(this.data));
41+
}
42+
43+
return Constructor;
44+
})();
45+
46+
export default Component;

JavaScript/Reactividad/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ <h1>Reactividad con Vanilla JS</h1>
1515
<h2>Task List</h2>
1616
<ul id="todo-list">
1717
</ul>
18-
<script src="./05_componentes_con_estado.js"></script>
18+
<script type="module" src="./Components.js"></script>
19+
<script type="module" src="./06_libreria_componentes_con_estado.js"></script>
1920
</body>
2021
</html>

0 commit comments

Comments
 (0)