1
- import React , { PropTypes , Component } from 'react' ;
1
+ import React , { Component } from 'react' ;
2
+ import PropTypes from 'prop-types' ;
2
3
3
4
const simpleGet = key => data => data [ key ] ;
4
5
const keyGetter = keys => data => keys . map ( key => data [ key ] ) ;
5
6
6
7
const isEmpty = value => value == null || value === '' ;
7
8
8
- const getCellValue =
9
- ( { prop , defaultContent, render } , row ) =>
10
- // Return `defaultContent` if the value is empty.
11
- ! isEmpty ( prop ) && isEmpty ( row [ prop ] ) ? defaultContent :
12
- // Use the render function for the value.
13
- render ? render ( row [ prop ] , row ) :
14
- // Otherwise just return the value.
15
- row [ prop ] ;
16
-
17
- const getCellClass =
18
- ( { prop, className} , row ) =>
19
- ! isEmpty ( prop ) && isEmpty ( row [ prop ] ) ? 'empty-cell' :
20
- typeof className == 'function' ? className ( row [ prop ] , row ) :
21
- className ;
9
+ const getCellValue = ( { prop , defaultContent , render } , row ) =>
10
+ // Return ` defaultContent` if the value is empty.
11
+ ! isEmpty ( prop ) && isEmpty ( row [ prop ] )
12
+ ? defaultContent
13
+ : // Use the render function for the value.
14
+ render
15
+ ? render ( row [ prop ] , row )
16
+ : // Otherwise just return the value.
17
+ row [ prop ] ;
18
+
19
+ const getCellClass = ( { prop, className } , row ) =>
20
+ ! isEmpty ( prop ) && isEmpty ( row [ prop ] )
21
+ ? 'empty-cell'
22
+ : typeof className == 'function' ? className ( row [ prop ] , row ) : className ;
22
23
23
24
function buildSortProps ( col , sortBy , onSort ) {
24
25
const order = sortBy && sortBy . prop === col . prop ? sortBy . order : 'none' ;
25
26
const nextOrder = order === 'ascending' ? 'descending' : 'ascending' ;
26
27
const sortEvent = onSort . bind ( null , { prop : col . prop , order : nextOrder } ) ;
27
28
28
29
return {
29
- ' onClick' : sortEvent ,
30
+ onClick : sortEvent ,
30
31
// Fire the sort event on enter.
31
- 'onKeyDown' : e => { if ( e . keyCode === 13 ) sortEvent ( ) ; } ,
32
+ onKeyDown : e => {
33
+ if ( e . keyCode === 13 ) sortEvent ( ) ;
34
+ } ,
32
35
// Prevents selection with mouse.
33
- ' onMouseDown' : e => e . preventDefault ( ) ,
34
- ' tabIndex' : 0 ,
36
+ onMouseDown : e => e . preventDefault ( ) ,
37
+ tabIndex : 0 ,
35
38
'aria-sort' : order ,
36
39
'aria-label' : `${ col . title } : activate to sort column ${ nextOrder } ` ,
37
40
} ;
@@ -46,38 +49,27 @@ export default class Table extends Component {
46
49
PropTypes . string ,
47
50
] ) . isRequired ,
48
51
49
- columns : PropTypes . arrayOf ( PropTypes . shape ( {
50
- title : PropTypes . string . isRequired ,
51
- prop : PropTypes . oneOfType ( [
52
- PropTypes . string ,
53
- PropTypes . number ,
54
- ] ) ,
55
- render : PropTypes . func ,
56
- sortable : PropTypes . bool ,
57
- defaultContent : PropTypes . string ,
58
- width : PropTypes . oneOfType ( [
59
- PropTypes . string ,
60
- PropTypes . number ,
61
- ] ) ,
62
- className : PropTypes . oneOfType ( [
63
- PropTypes . string ,
64
- PropTypes . func ,
65
- ] ) ,
66
- } ) ) . isRequired ,
67
-
68
- dataArray : PropTypes . arrayOf ( PropTypes . oneOfType ( [
69
- PropTypes . array ,
70
- PropTypes . object ,
71
- ] ) ) . isRequired ,
52
+ columns : PropTypes . arrayOf (
53
+ PropTypes . shape ( {
54
+ title : PropTypes . string . isRequired ,
55
+ prop : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
56
+ render : PropTypes . func ,
57
+ sortable : PropTypes . bool ,
58
+ defaultContent : PropTypes . string ,
59
+ width : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
60
+ className : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . func ] ) ,
61
+ } ) ,
62
+ ) . isRequired ,
63
+
64
+ dataArray : PropTypes . arrayOf (
65
+ PropTypes . oneOfType ( [ PropTypes . array , PropTypes . object ] ) ,
66
+ ) . isRequired ,
72
67
73
68
buildRowOptions : PropTypes . func ,
74
69
75
70
sortBy : PropTypes . shape ( {
76
- prop : PropTypes . oneOfType ( [
77
- PropTypes . string ,
78
- PropTypes . number ,
79
- ] ) ,
80
- order : PropTypes . oneOf ( [ 'ascending' , 'descending' ] ) ,
71
+ prop : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
72
+ order : PropTypes . oneOf ( [ 'ascending' , 'descending' ] ) ,
81
73
} ) ,
82
74
83
75
onSort : PropTypes . func ,
@@ -95,8 +87,13 @@ export default class Table extends Component {
95
87
96
88
render ( ) {
97
89
const {
98
- columns, keys, buildRowOptions, sortBy,
99
- onSort, dataArray, ...otherProps ,
90
+ columns,
91
+ keys,
92
+ buildRowOptions,
93
+ sortBy,
94
+ onSort,
95
+ dataArray,
96
+ ...otherProps
100
97
} = this . props ;
101
98
102
99
const headers = columns . map ( ( col , idx ) => {
@@ -109,15 +106,19 @@ export default class Table extends Component {
109
106
110
107
return (
111
108
< th
112
- ref = { c => this . _headers [ idx ] = c }
109
+ ref = { c => ( this . _headers [ idx ] = c ) }
113
110
key = { idx }
114
- style = { { width : col . width } }
111
+ style = { { width : col . width } }
115
112
role = "columnheader"
116
113
scope = "col"
117
- { ...sortProps } >
118
- < span > { col . title } </ span >
119
- { ! order ? null :
120
- < span className = { `sort-icon sort-${ order } ` } aria-hidden = "true" /> }
114
+ { ...sortProps }
115
+ >
116
+ < span >
117
+ { col . title }
118
+ </ span >
119
+ { ! order
120
+ ? null
121
+ : < span className = { `sort-icon sort-${ order } ` } aria-hidden = "true" /> }
121
122
</ th >
122
123
) ;
123
124
} ) ;
@@ -131,31 +132,34 @@ export default class Table extends Component {
131
132
{ columns . map ( ( col , i ) =>
132
133
< td key = { i } className = { getCellClass ( col , row ) } >
133
134
{ getCellValue ( col , row ) }
134
- </ td >
135
+ </ td > ,
135
136
) }
136
137
</ tr >
137
138
) ;
138
139
} ) ;
139
140
140
141
return (
141
142
< table { ...otherProps } >
142
- { ! sortBy ? null :
143
- < caption className = "sr-only" role = "alert" aria-live = "polite" >
144
- { `Sorted by ${ sortBy . prop } : ${ sortBy . order } order` }
145
- </ caption > }
143
+ { ! sortBy
144
+ ? null
145
+ : < caption className = "sr-only" role = "alert" aria-live = "polite" >
146
+ { `Sorted by ${ sortBy . prop } : ${ sortBy . order } order` }
147
+ </ caption > }
146
148
< thead >
147
149
< tr >
148
150
{ headers }
149
151
</ tr >
150
152
</ thead >
151
153
< tbody >
152
- { rows . length ? rows :
153
- < tr >
154
- < td colSpan = { columns . length } className = "text-center" > No data</ td >
155
- </ tr > }
154
+ { rows . length
155
+ ? rows
156
+ : < tr >
157
+ < td colSpan = { columns . length } className = "text-center" >
158
+ No data
159
+ </ td >
160
+ </ tr > }
156
161
</ tbody >
157
162
</ table >
158
163
) ;
159
164
}
160
-
161
165
}
0 commit comments