11import React from 'react' ;
22import PropTypes from 'prop-types' ;
33import update from 'immutability-helper' ;
4- import { PivotData } from './Utilities' ;
4+ import { PivotData , aggregators } from './Utilities' ;
55import DnDCell from './DnDCell' ;
66import TableRenderer from './TableRenderer' ;
77import './pivottable.css' ;
@@ -44,13 +44,20 @@ class PivotTableUI extends React.Component {
4444 this . attrValues = attrValues ;
4545 }
4646
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+ }
4854
4955 render ( ) {
56+ const numValsAllowed = aggregators [ this . props . aggregatorName ] ( [ ] ) ( ) . numInputs || 0 ;
5057 return (
5158 < table className = "pvtUi" > < tbody >
5259 < tr >
53- < td > (renderers) </ td >
60+ < td > < select > < option > Table </ option > </ select > </ td >
5461 < DnDCell
5562 items = { Object . keys ( this . attrValues )
5663 . filter ( e => ! this . props . rows . includes ( e ) && ! this . props . cols . includes ( e ) ) }
@@ -59,20 +66,40 @@ class PivotTableUI extends React.Component {
5966 />
6067 </ tr >
6168 < 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 >
6387 < DnDCell
6488 items = { this . props . cols } classes = "pvtAxisContainer pvtHorizList pvtCols"
65- onChange = { newCols => this . onChange ( { cols : { $set : newCols } } ) }
89+ onChange = { this . updateProps ( ' cols' ) }
6690 />
6791 </ tr >
6892 < tr >
6993 < DnDCell
7094 items = { this . props . rows } classes = "pvtAxisContainer pvtVertList pvtRows"
71- onChange = { newRows => this . onChange ( { rows : { $set : newRows } } ) }
95+ onChange = { this . updateProps ( ' rows' ) }
7296 />
7397 < td >
7498 < 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+ } ) ) }
76103 />
77104 </ td >
78105 </ tr >
@@ -83,14 +110,16 @@ class PivotTableUI extends React.Component {
83110}
84111
85112PivotTableUI . defaultProps = {
86- rows : [ ] , cols : [ ]
113+ rows : [ ] , cols : [ ] , vals : [ ] , aggregatorName : 'Count'
87114} ;
88115
89116PivotTableUI . propTypes = {
90117 data : PropTypes . oneOfType ( [ PropTypes . array , PropTypes . object , PropTypes . func ] ) . isRequired ,
91118 onChange : PropTypes . func . isRequired ,
119+ aggregatorName : PropTypes . string ,
92120 cols : PropTypes . arrayOf ( PropTypes . string ) ,
93- rows : PropTypes . arrayOf ( PropTypes . string )
121+ rows : PropTypes . arrayOf ( PropTypes . string ) ,
122+ vals : PropTypes . arrayOf ( PropTypes . string )
94123} ;
95124
96125
0 commit comments