1
1
import React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import update from 'immutability-helper' ;
4
- import { PivotData } from './Utilities' ;
4
+ import { PivotData , aggregators } from './Utilities' ;
5
5
import DnDCell from './DnDCell' ;
6
6
import TableRenderer from './TableRenderer' ;
7
7
import './pivottable.css' ;
@@ -44,13 +44,20 @@ class PivotTableUI extends React.Component {
44
44
this . attrValues = attrValues ;
45
45
}
46
46
47
- onChange ( command ) { this . props . onChange ( update ( this . props , command ) ) ; }
47
+ sendPropUpdate ( command ) {
48
+ this . props . onChange ( update ( this . props , command ) ) ;
49
+ }
50
+
51
+ updateProps ( key ) {
52
+ return value => this . sendPropUpdate ( { [ key ] : { $set : value } } ) ;
53
+ }
48
54
49
55
render ( ) {
56
+ const numValsAllowed = aggregators [ this . props . aggregatorName ] ( [ ] ) ( ) . numInputs || 0 ;
50
57
return (
51
58
< table className = "pvtUi" > < tbody >
52
59
< tr >
53
- < td > (renderers) </ td >
60
+ < td > < select > < option > Table </ option > </ select > </ td >
54
61
< DnDCell
55
62
items = { Object . keys ( this . attrValues )
56
63
. filter ( e => ! this . props . rows . includes ( e ) && ! this . props . cols . includes ( e ) ) }
@@ -59,20 +66,40 @@ class PivotTableUI extends React.Component {
59
66
/>
60
67
</ tr >
61
68
< tr >
62
- < td > (aggregators)</ td >
69
+ < td className = "pvtVals" >
70
+ < select value = { this . props . aggregatorName }
71
+ onChange = { ( { target : { value} } ) => this . updateProps ( 'aggregatorName' ) ( value ) }
72
+ >
73
+ { Object . keys ( aggregators ) . map ( n => < option key = { `agg${ n } ` } value = { n } > { n } </ option > ) }
74
+ </ select >
75
+ { ( numValsAllowed > 0 ) && < br /> }
76
+ { new Array ( numValsAllowed ) . fill ( ) . map ( ( n , i ) =>
77
+ < select value = { this . props . vals [ i ] } key = { `val${ i } ` }
78
+ onChange = { ( { target : { value} } ) =>
79
+ this . sendPropUpdate ( { vals : { $splice : [ [ i , 1 , value ] ] } } ) }
80
+ >
81
+ < option key = { `none${ i } ` } value = "" > </ option >
82
+ { Object . keys ( this . attrValues ) . map ( ( v , j ) =>
83
+ < option key = { `${ i } -${ j } ` } value = { v } > { v } </ option > ) }
84
+ </ select >
85
+ ) }
86
+ </ td >
63
87
< DnDCell
64
88
items = { this . props . cols } classes = "pvtAxisContainer pvtHorizList pvtCols"
65
- onChange = { newCols => this . onChange ( { cols : { $set : newCols } } ) }
89
+ onChange = { this . updateProps ( ' cols' ) }
66
90
/>
67
91
</ tr >
68
92
< tr >
69
93
< DnDCell
70
94
items = { this . props . rows } classes = "pvtAxisContainer pvtVertList pvtRows"
71
- onChange = { newRows => this . onChange ( { rows : { $set : newRows } } ) }
95
+ onChange = { this . updateProps ( ' rows' ) }
72
96
/>
73
97
< td >
74
98
< TableRenderer pivotData = { new PivotData (
75
- update ( this . props , { data : { $set : this . materializedInput } } ) ) }
99
+ update ( this . props , {
100
+ data : { $set : this . materializedInput } ,
101
+ aggregator : { $set : aggregators [ this . props . aggregatorName ] ( this . props . vals ) }
102
+ } ) ) }
76
103
/>
77
104
</ td >
78
105
</ tr >
@@ -83,14 +110,16 @@ class PivotTableUI extends React.Component {
83
110
}
84
111
85
112
PivotTableUI . defaultProps = {
86
- rows : [ ] , cols : [ ]
113
+ rows : [ ] , cols : [ ] , vals : [ ] , aggregatorName : 'Count'
87
114
} ;
88
115
89
116
PivotTableUI . propTypes = {
90
117
data : PropTypes . oneOfType ( [ PropTypes . array , PropTypes . object , PropTypes . func ] ) . isRequired ,
91
118
onChange : PropTypes . func . isRequired ,
119
+ aggregatorName : PropTypes . string ,
92
120
cols : PropTypes . arrayOf ( PropTypes . string ) ,
93
- rows : PropTypes . arrayOf ( PropTypes . string )
121
+ rows : PropTypes . arrayOf ( PropTypes . string ) ,
122
+ vals : PropTypes . arrayOf ( PropTypes . string )
94
123
} ;
95
124
96
125
0 commit comments