Skip to content

Commit

Permalink
Refactor PivotTableUI a bit to make it easier to extend.
Browse files Browse the repository at this point in the history
  • Loading branch information
prakhargoel-beacon committed Jun 10, 2019
1 parent 61739b2 commit c9a2067
Showing 1 changed file with 37 additions and 56 deletions.
93 changes: 37 additions & 56 deletions src/PivotTableUI.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,32 +367,30 @@ class PivotTableUI extends React.PureComponent {
);
}

render() {
rendererCell = () => (
<td className="pvtRenderers">
<Dropdown
current={this.props.rendererName in this.props.renderers
? this.props.rendererName
: Object.keys(this.props.renderers)[0]
}
values={Object.keys(this.props.renderers)}
open={this.isOpen('renderer')}
zIndex={this.isOpen('renderer') ? this.state.maxZIndex + 1 : 1}
toggle={() =>
this.setState({
openDropdown: this.isOpen('renderer') ? false : 'renderer',
})
}
setValue={this.propUpdater('rendererName')}
/>
</td>
);

aggregatorCell = () => {
const numValsAllowed =
this.props.aggregators[this.props.aggregatorName]([])().numInputs || 0;

const rendererName =
this.props.rendererName in this.props.renderers
? this.props.rendererName
: Object.keys(this.props.renderers)[0];

const rendererCell = (
<td className="pvtRenderers">
<Dropdown
current={rendererName}
values={Object.keys(this.props.renderers)}
open={this.isOpen('renderer')}
zIndex={this.isOpen('renderer') ? this.state.maxZIndex + 1 : 1}
toggle={() =>
this.setState({
openDropdown: this.isOpen('renderer') ? false : 'renderer',
})
}
setValue={this.propUpdater('rendererName')}
/>
</td>
);

const sortIcons = {
key_a_to_z: {
rowSymbol: '↕',
Expand All @@ -407,7 +405,7 @@ class PivotTableUI extends React.PureComponent {
value_z_to_a: {rowSymbol: '↑', colSymbol: '←', next: 'key_a_to_z'},
};

const aggregatorCell = (
return (
<td className="pvtVals">
<Dropdown
current={this.props.aggregatorName}
Expand Down Expand Up @@ -466,7 +464,9 @@ class PivotTableUI extends React.PureComponent {
])}
</td>
);

}

render() {
const unusedAttrs = Object.keys(this.attrValues)
.filter(
e =>
Expand Down Expand Up @@ -520,40 +520,21 @@ class PivotTableUI extends React.PureComponent {
</td>
);

if (horizUnused) {
return (
<table className="pvtUi">
<tbody onClick={() => this.setState({openDropdown: false})}>
<tr>
{rendererCell}
{unusedAttrsCell}
</tr>
<tr>
{aggregatorCell}
{colAttrsCell}
</tr>
<tr>
{rowAttrsCell}
{outputCell}
</tr>
</tbody>
</table>
);
}

const outputRows = horizUnused
? [
(<tr key="R">{this.rendererCell()}{unusedAttrsCell}</tr>),
(<tr key="A">{this.aggregatorCell()}{colAttrsCell}</tr>),
(<tr key="O">{rowAttrsCell}{outputCell}</tr>),
]
: [
(<tr key="RA">{this.rendererCell()}{this.aggregatorCell()}{colAttrsCell}</tr>),
(<tr key="O">{unusedAttrsCell}{rowAttrsCell}{outputCell}</tr>),
];

return (
<table className="pvtUi">
<tbody onClick={() => this.setState({openDropdown: false})}>
<tr>
{rendererCell}
{aggregatorCell}
{colAttrsCell}
</tr>
<tr>
{unusedAttrsCell}
{rowAttrsCell}
{outputCell}
</tr>
{outputRows}
</tbody>
</table>
);
Expand Down

0 comments on commit c9a2067

Please sign in to comment.