File tree Expand file tree Collapse file tree 3 files changed +81
-1
lines changed Expand file tree Collapse file tree 3 files changed +81
-1
lines changed Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ <h1>Reactividad con Vanilla JS</h1>
15
15
< h2 > Task List</ h2 >
16
16
< ul id ="todo-list ">
17
17
</ 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 >
19
20
</ body >
20
21
</ html >
You can’t perform that action at this time.
0 commit comments