1
- import React from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
import { Table } from '../dist/main' ;
3
3
4
4
export function TableExamples ( ) {
5
+ const [ sortState , setSortState ] = useState ( { sortBy : 'a' , sortOrder : 'ASC' } ) ;
6
+
7
+ function changeSort ( attribute ) {
8
+ console . log ( 'sorting by' , attribute ) ;
9
+
10
+ setSortState ( ( prevState ) => {
11
+ const attributeChanged = prevState . sortBy !== attribute ;
12
+ const order = ! attributeChanged && prevState . sortOrder === 'ASC' ? 'DESC' : 'ASC' ;
13
+
14
+ return {
15
+ sortBy : attribute ,
16
+ sortOrder : order ,
17
+ } ;
18
+ } ) ;
19
+ }
20
+
21
+ function buildSortingHeader ( label , attribute ) {
22
+ return (
23
+ < div className = "d-flex justify-content-between" >
24
+ { label }
25
+ < a
26
+ href = "#"
27
+ onClick = { ( e ) => {
28
+ e . preventDefault ( ) ;
29
+ e . stopPropagation ( ) ;
30
+ changeSort ( attribute ) ;
31
+ } }
32
+ >
33
+ { sortState . sortBy === attribute ? (
34
+ sortState . sortOrder === 'ASC' ? (
35
+ < img src = "https://upload.wikimedia.org/wikipedia/commons/5/53/Sort_up_1.18.gif" alt = "V" />
36
+ ) : (
37
+ < img src = "https://upload.wikimedia.org/wikipedia/commons/2/25/Sort_down_1.18.gif" alt = "A" />
38
+ )
39
+ ) : (
40
+ < img src = "https://upload.wikimedia.org/wikipedia/commons/7/73/Sort_both.svg" alt = "V/A" />
41
+ ) }
42
+ </ a >
43
+ </ div >
44
+ ) ;
45
+ }
5
46
return (
6
47
< div >
7
48
< div className = "row" >
8
- < div className = "col" >
49
+ < div className = "col mb-3 " >
9
50
< h1 className = "h4" > Simple Table</ h1 >
10
51
< Table
11
52
columns = { [ 'a' , 'b' , 'c' ] }
@@ -17,7 +58,7 @@ export function TableExamples() {
17
58
/>
18
59
</ div >
19
60
20
- < div className = "col" >
61
+ < div className = "col mb-3 " >
21
62
< h1 className = "h4" > Table with formated colums</ h1 >
22
63
< Table
23
64
columns = { [
@@ -33,7 +74,7 @@ export function TableExamples() {
33
74
/>
34
75
</ div >
35
76
36
- < div className = "col" >
77
+ < div className = "col mb-3 " >
37
78
< h1 className = "h4" > Table with formatted values </ h1 >
38
79
< Table
39
80
columns = { [
@@ -76,7 +117,7 @@ export function TableExamples() {
76
117
</ div >
77
118
78
119
< div className = "row" >
79
- < div className = "col" >
120
+ < div className = "col mb-3 " >
80
121
< h1 className = "h4" > Table with custom styles</ h1 >
81
122
< Table
82
123
columns = { [ 'a' , 'b' , 'c' ] }
@@ -94,7 +135,7 @@ export function TableExamples() {
94
135
/>
95
136
</ div >
96
137
97
- < div className = "col" >
138
+ < div className = "col mb-3 " >
98
139
< h1 className = "h4" > Table with row actions</ h1 >
99
140
< Table
100
141
columns = { [
@@ -126,6 +167,46 @@ export function TableExamples() {
126
167
/>
127
168
</ div >
128
169
</ div >
170
+
171
+ < div className = "row" >
172
+ < div className = "col-6 mb-3" >
173
+ < h1 className = "h4" > Table with custom header</ h1 >
174
+
175
+ < Table
176
+ columns = { [
177
+ {
178
+ attribute : 'a' ,
179
+ label : 'A' ,
180
+ } ,
181
+ {
182
+ attribute : 'b' ,
183
+ label : 'B' ,
184
+ } ,
185
+ {
186
+ attribute : 'c' ,
187
+ label : 'C' ,
188
+ } ,
189
+ ] }
190
+ columnHeaderFormat = { buildSortingHeader }
191
+ docs = { sortData (
192
+ [
193
+ { a : 1 , b : 9 , c : 3 } ,
194
+ { a : 6 , b : 5 , c : 7 } ,
195
+ { a : 8 , b : 2 , c : 4 } ,
196
+ ] ,
197
+ sortState
198
+ ) }
199
+ />
200
+ </ div >
201
+ </ div >
129
202
</ div >
130
203
) ;
131
204
}
205
+
206
+ function sortData ( data , { sortBy, sortOrder } ) {
207
+ return data . sort ( ( a , b ) => {
208
+ const diff = a [ sortBy ] - b [ sortBy ] ;
209
+
210
+ return sortOrder === 'ASC' ? diff : - diff ;
211
+ } ) ;
212
+ }
0 commit comments