1
1
import React from 'react' ;
2
2
3
- import { ButtonGroup , Well } from 'react-bootstrap' ;
3
+ import { ButtonGroup , Collapse , Well } from 'react-bootstrap' ;
4
4
import shortid from 'shortid' ;
5
5
6
6
import CopyToClipboard from '../../components/CopyToClipboard' ;
@@ -10,18 +10,21 @@ import ModalTrigger from '../../components/ModalTrigger';
10
10
const propTypes = {
11
11
table : React . PropTypes . object ,
12
12
actions : React . PropTypes . object ,
13
+ timeout : React . PropTypes . integer , // used for tests
13
14
} ;
14
15
15
16
const defaultProps = {
16
- table : null ,
17
17
actions : { } ,
18
+ table : null ,
19
+ timeout : 500 ,
18
20
} ;
19
21
20
22
class TableElement extends React . PureComponent {
21
23
constructor ( props ) {
22
24
super ( props ) ;
23
25
this . state = {
24
26
sortColumns : false ,
27
+ expanded : true ,
25
28
} ;
26
29
}
27
30
@@ -36,18 +39,17 @@ class TableElement extends React.PureComponent {
36
39
this . props . actions . addQueryEditor ( qe ) ;
37
40
}
38
41
39
- collapseTable ( e ) {
40
- e . preventDefault ( ) ;
41
- this . props . actions . collapseTable ( this . props . table ) ;
42
- }
43
-
44
- expandTable ( e ) {
42
+ toggleTable ( e ) {
45
43
e . preventDefault ( ) ;
46
- this . props . actions . expandTable ( this . props . table ) ;
44
+ if ( this . props . table . expanded ) {
45
+ this . props . actions . collapseTable ( this . props . table ) ;
46
+ } else {
47
+ this . props . actions . expandTable ( this . props . table ) ;
48
+ }
47
49
}
48
50
49
51
removeTable ( ) {
50
- this . props . actions . removeTable ( this . props . table ) ;
52
+ this . setState ( { expanded : false } ) ;
51
53
}
52
54
dataPreviewModal ( ) {
53
55
const query = {
@@ -65,11 +67,8 @@ class TableElement extends React.PureComponent {
65
67
this . setState ( { sortColumns : ! this . state . sortColumns } ) ;
66
68
}
67
69
68
- render ( ) {
70
+ renderHeader ( ) {
69
71
const table = this . props . table ;
70
- let metadata = null ;
71
- let buttonToggle ;
72
-
73
72
let header ;
74
73
if ( table . partitions ) {
75
74
let partitionQuery ;
@@ -101,25 +100,26 @@ class TableElement extends React.PureComponent {
101
100
</ Well >
102
101
) ;
103
102
}
104
- if ( table . expanded ) {
105
- buttonToggle = (
106
- < a
107
- href = "#"
108
- onClick = { ( e ) => { this . collapseTable ( e ) ; } }
109
- >
110
- < strong > { table . name } </ strong >
111
- < small className = "m-l-5" > < i className = "fa fa-minus" /> </ small >
112
- </ a >
113
- ) ;
114
- const cols = table . columns . slice ( ) ;
103
+ return header ;
104
+ }
105
+ renderMetadata ( ) {
106
+ const table = this . props . table ;
107
+ let cols ;
108
+ if ( table . columns ) {
109
+ cols = table . columns . slice ( ) ;
115
110
if ( this . state . sortColumns ) {
116
111
cols . sort ( ( a , b ) => a . name . toUpperCase ( ) > b . name . toUpperCase ( ) ) ;
117
112
}
118
- metadata = (
113
+ }
114
+ const metadata = (
115
+ < Collapse
116
+ in = { table . expanded }
117
+ timeout = { this . props . timeout }
118
+ >
119
119
< div >
120
- { header }
120
+ { this . renderHeader ( ) }
121
121
< div className = "table-columns" >
122
- { cols . map ( ( col ) => {
122
+ { cols && cols . map ( ( col ) => {
123
123
let name = col . name ;
124
124
if ( col . indexed ) {
125
125
name = < strong > { col . name } </ strong > ;
@@ -137,18 +137,17 @@ class TableElement extends React.PureComponent {
137
137
< hr />
138
138
</ div >
139
139
</ div >
140
- ) ;
141
- } else {
142
- buttonToggle = (
143
- < a
144
- href = "#"
145
- onClick = { ( e ) => { this . expandTable ( e ) ; } }
146
- >
147
- { table . name }
148
- < small className = "m-l-5" > < i className = "fa fa-plus" /> </ small >
149
- </ a >
150
- ) ;
151
- }
140
+ </ Collapse >
141
+ ) ;
142
+ return metadata ;
143
+ }
144
+ removeFromStore ( ) {
145
+ this . props . actions . removeTable ( this . props . table ) ;
146
+ }
147
+
148
+ render ( ) {
149
+ const table = this . props . table ;
150
+
152
151
let keyLink ;
153
152
if ( table . indexes && table . indexes . length > 0 ) {
154
153
keyLink = (
@@ -171,52 +170,70 @@ class TableElement extends React.PureComponent {
171
170
) ;
172
171
}
173
172
return (
174
- < div className = "TableElement" >
175
- < div className = "clearfix" >
176
- < div className = "pull-left" >
177
- { buttonToggle }
178
- </ div >
179
- < div className = "pull-right" >
180
- < ButtonGroup className = "ws-el-controls pull-right" >
181
- { keyLink }
182
- < Link
183
- className = {
184
- `fa fa-sort-${ ! this . state . sortColumns ? 'alpha' : 'numeric' } -asc ` +
185
- 'pull-left sort-cols m-l-2' }
186
- onClick = { this . toggleSortColumns . bind ( this ) }
187
- tooltip = {
188
- ! this . state . sortColumns ?
189
- 'Sort columns alphabetically' :
190
- 'Original table column order' }
173
+ < Collapse
174
+ in = { this . state . expanded }
175
+ timeout = { this . props . timeout }
176
+ transitionAppear
177
+ onExited = { this . removeFromStore . bind ( this ) }
178
+ >
179
+ < div className = "TableElement" >
180
+ < div className = "clearfix" >
181
+ < div className = "pull-left" >
182
+ < a
191
183
href = "#"
192
- />
193
- < Link
194
- className = "fa fa-search-plus pull-left m-l-2"
195
- onClick = { this . dataPreviewModal . bind ( this ) }
196
- tooltip = "Data preview"
197
- href = "#"
198
- />
199
- < CopyToClipboard
200
- copyNode = {
201
- < a className = "fa fa-clipboard pull-left m-l-2" />
184
+ className = "table-name"
185
+ onClick = { ( e ) => { this . toggleTable ( e ) ; } }
186
+ >
187
+ < strong > { table . name } </ strong >
188
+ < small className = "m-l-5" >
189
+ < i className = { `fa fa-${ table . expanded ? 'minus' : 'plus' } -square-o` } />
190
+ </ small >
191
+ </ a >
192
+ </ div >
193
+ < div className = "pull-right" >
194
+ < ButtonGroup className = "ws-el-controls pull-right" >
195
+ { keyLink }
196
+ < Link
197
+ className = {
198
+ `fa fa-sort-${ ! this . state . sortColumns ? 'alpha' : 'numeric' } -asc ` +
199
+ 'pull-left sort-cols m-l-2' }
200
+ onClick = { this . toggleSortColumns . bind ( this ) }
201
+ tooltip = {
202
+ ! this . state . sortColumns ?
203
+ 'Sort columns alphabetically' :
204
+ 'Original table column order' }
205
+ href = "#"
206
+ />
207
+ < Link
208
+ className = "fa fa-search-plus pull-left m-l-2"
209
+ onClick = { this . dataPreviewModal . bind ( this ) }
210
+ tooltip = "Data preview"
211
+ href = "#"
212
+ />
213
+ { table . selectStar &&
214
+ < CopyToClipboard
215
+ copyNode = {
216
+ < a className = "fa fa-clipboard pull-left m-l-2" />
217
+ }
218
+ text = { table . selectStar }
219
+ shouldShowText = { false }
220
+ tooltipText = "Copy SELECT statement to clipboard"
221
+ />
202
222
}
203
- text = { table . selectStar }
204
- shouldShowText = { false }
205
- tooltipText = "Copy SELECT statement to clipboard"
206
- />
207
- < Link
208
- className = "fa fa-trash pull-left m-l-2"
209
- onClick = { this . removeTable . bind ( this ) }
210
- tooltip = "Remove from panel"
211
- href = "#"
212
- / >
213
- </ ButtonGroup >
223
+ < Link
224
+ className = "fa fa-trash table-remove pull-left m-l-2"
225
+ onClick = { this . removeTable . bind ( this ) }
226
+ tooltip = "Remove from panel"
227
+ href = "#"
228
+ />
229
+ </ ButtonGroup >
230
+ </ div >
231
+ </ div >
232
+ < div >
233
+ { this . renderMetadata ( ) }
214
234
</ div >
215
235
</ div >
216
- < div >
217
- { metadata }
218
- </ div >
219
- </ div >
236
+ </ Collapse >
220
237
) ;
221
238
}
222
239
}
0 commit comments