1
1
const list = ( {
2
+ enabledSearch = true ,
3
+ otherState,
2
4
callback,
3
- renderItem,
5
+ renderItem
4
6
} ) => {
5
7
return {
6
- // FILTER
7
- search : '' ,
8
- filter : 10 ,
9
- page : 0 ,
8
+ state : {
9
+ // FILTER
10
+ search : '' ,
11
+ filter : 10 ,
12
+ page : 0 ,
10
13
11
- // DISPLAY
12
- total : 0 ,
13
- showStart : 0 ,
14
- showEnd : 0 ,
15
- isLoading : false ,
14
+ // DISPLAY
15
+ total : 0 ,
16
+ showStart : 0 ,
17
+ showEnd : 0 ,
18
+ isLoading : false ,
16
19
17
- items : [ ] ,
20
+ enabledSearch : true ,
21
+
22
+ other : { } ,
23
+
24
+ items : [ ]
25
+ } ,
18
26
async init ( ) {
27
+ this . state . other = otherState
19
28
await this . load ( ) ;
20
29
} ,
21
30
async load ( ) {
22
- this . isLoading = true
23
- this . showStart = 0
24
- this . showEnd = 0
25
- const { data, total} = await callback ( {
26
- filter : this . filter ,
27
- page : this . page ,
28
- search : this . search ,
29
- } )
31
+ this . state . enabledSearch = enabledSearch
32
+ this . state . isLoading = true
33
+ this . state . showStart = 0
34
+ this . state . showEnd = 0
35
+ const { data, total} = await callback ( this . state )
30
36
31
- this . items = data
32
- this . total = total
37
+ this . state . items = data
38
+ this . state . total = total
33
39
34
- this . isLoading = false
40
+ this . state . isLoading = false
35
41
36
- if ( this . items == null || this . items . length == 0 ) return ;
42
+ if ( this . state . items == null || this . state . items . length == 0 ) return ;
37
43
38
- this . showStart = this . items . length > 0 ? ( ( this . page * this . filter ) + 1 ) : 0 ;
39
- this . showEnd = ( this . page * this . filter ) + this . items . length ;
44
+ this . state . showStart = this . state . items . length > 0 ? ( ( this . state . page * this . state . filter ) + 1 ) : 0 ;
45
+ this . state . showEnd = ( this . state . page * this . state . filter ) + this . state . items . length ;
40
46
} ,
41
47
//EVENT HANDLERS
42
48
onChangeFilterHandler ( e ) {
43
- this . page = 0 ,
44
- this . total = 0 ,
45
- this . filter = parseInt ( e . target . value ) ;
49
+ this . state . page = 0 ,
50
+ this . state . total = 0 ,
51
+ this . state . filter = parseInt ( e . target . value ) ;
46
52
this . load ( )
47
53
} ,
48
54
onSearchSubmitHandler ( e ) {
49
- this . page = 0 ,
50
- this . total = 0 ,
51
- this . search = e . target . value ;
55
+ this . state . page = 0 ,
56
+ this . state . total = 0 ,
57
+ this . state . search = e . target . value ;
52
58
this . load ( ) ;
53
59
} ,
54
60
onNextPageHandler ( ) {
55
61
if ( ! this . nextPageEnabled ( ) ) return ;
56
62
57
- this . page = this . page + 1
63
+ this . state . page = this . state . page + 1
58
64
59
65
this . load ( )
60
66
} ,
61
67
onPreviousPageHandler ( ) {
62
68
if ( ! this . previousPageEnabled ( ) ) return ;
63
69
64
- this . page = this . page - 1
70
+ this . state . page = this . state . page - 1
65
71
66
72
this . load ( ) ;
67
73
} ,
68
74
//FUNCTIONS
69
75
nextPageEnabled ( ) {
70
- return this . page < Math . ceil ( this . total / this . filter ) - 1
76
+ return this . state . page < Math . ceil ( this . state . total / this . state . filter ) - 1
71
77
} ,
72
78
previousPageEnabled ( ) {
73
- return this . page > 0
79
+ return this . state . page > 0
74
80
} ,
75
81
checkItemLenght ( ) {
76
- if ( ! this . items ) {
82
+ if ( ! this . state . items ) {
77
83
return false
78
84
}
79
- if ( this . items . length == 0 ) {
85
+ if ( this . state . items . length == 0 ) {
80
86
return false
81
87
}
82
88
return true
@@ -86,29 +92,19 @@ const list = ({
86
92
return renderItem ( item )
87
93
} ,
88
94
template : `<nav class="bg-white flex items-center justify-between" aria-label="header">
89
- <div class="sm:block">
90
- <div>
91
- <label for="filter" class="block text-sm font-medium text-gray-700">Filter</label>
92
- <select @change="onChangeFilterHandler" x-model="filter" id="filter" name="filter" class="mt-1 block w-20 pl-3 pr-10 py-2 text-base text-center border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md">
93
- <option>5</option>
94
- <option>10</option>
95
- <option>20</option>
96
- <option>50</option>
97
- <option>100</option>
98
- </select>
99
- </div>
100
- </div>
101
- <div class="flex justify-between sm:justify-end">
102
- <div class="sm:col-span-3">
103
- <label for="search" class="block text-sm font-medium text-gray-700">Search</label>
104
- <div class="mt.-1">
105
- <input @keyup.enter="onSearchSubmitHandler" type="text" name="search" id="search" class="block w-full focus:ring-indigo-500 focus:border-indigo-500 pl-2 sm:text-sm border-gray-300 rounded-md" x-model="search">
95
+ <template x-if="state.enabledSearch">
96
+ <div class="flex justify-between sm:justify-end">
97
+ <div class="sm:col-span-3">
98
+ <label for="search" class="block text-sm font-medium text-gray-700">Search</label>
99
+ <div class="mt.-1">
100
+ <input @keyup.enter="onSearchSubmitHandler" type="text" name="search" id="search" class="block w-full focus:ring-indigo-500 focus:border-indigo-500 pl-2 sm:text-sm border-gray-300 rounded-md" x-model="state.search">
101
+ </div>
106
102
</div>
107
103
</div>
108
- </div >
104
+ </template >
109
105
</nav>
110
106
111
- <div x-show='isLoading' x-transition>
107
+ <div x-show='state. isLoading' x-transition>
112
108
<svg
113
109
role="status"
114
110
class="w-8 h-8 text-gray-200 animate-spin dark:text-gray-600 fill-[#FF5800] m-auto my-5"
@@ -124,13 +120,13 @@ const list = ({
124
120
</svg>
125
121
</div>
126
122
127
- <div x-show="!isLoading" x-transition>
128
- <template x-if="!items">
123
+ <div x-show="!state. isLoading" x-transition>
124
+ <template x-if="!state. items">
129
125
<p class="text-center my-5">NO RESULT FOUND</p>
130
126
</template>
131
127
<template x-if="checkItemLenght ">
132
128
<ul role="list" class="divide-y divide-gray-300 my-3">
133
- <template x-for="item in items">
129
+ <template x-for="item in state. items">
134
130
<li x-html="render(item)">
135
131
</li>
136
132
</template>
@@ -139,14 +135,26 @@ const list = ({
139
135
</div>
140
136
141
137
<nav class="bg-white py-3 flex items-center justify-between border-t border-gray-200" aria-label="Pagination">
138
+ <div class="sm:block">
139
+ <div class="content-start">
140
+ <label for="filter" class="text-sm font-medium text-gray-700">Filter</label>
141
+ <select @change="onChangeFilterHandler" x-model="state.filter" id="filter" name="filter" class="mt-1 w-20 pl-3 pr-10 py-2 text-base text-center border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md">
142
+ <option>5</option>
143
+ <option>10</option>
144
+ <option>20</option>
145
+ <option>50</option>
146
+ <option>100</option>
147
+ </select>
148
+ </div>
149
+ </div>
142
150
<div class="sm:block">
143
151
<p class="text-sm text-gray-700">
144
152
Showing
145
- <span class="font-medium" x-text="showStart"></span>
153
+ <span class="font-medium" x-text="state. showStart"></span>
146
154
to
147
- <span class="font-medium" x-text="showEnd"></span>
155
+ <span class="font-medium" x-text="state. showEnd"></span>
148
156
of
149
- <span class="font-medium" x-text="total"></span>
157
+ <span class="font-medium" x-text="state. total"></span>
150
158
results
151
159
</p>
152
160
</div>
0 commit comments