|
| 1 | +/* eslint max-len: 0 */ |
| 2 | +/* eslint no-unused-vars: 0 */ |
| 3 | +/* eslint no-alert: 0 */ |
| 4 | +import React, { PropTypes } from 'react'; |
| 5 | +import DropdownButton from 'react-bootstrap/lib/DropdownButton'; |
| 6 | +import MenuItem from 'react-bootstrap/lib/MenuItem'; |
| 7 | +import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table'; |
| 8 | + |
| 9 | +// Initialize the product array. |
| 10 | +const products = []; |
| 11 | +function addProducts(quantity) { |
| 12 | + const startId = products.length; |
| 13 | + for (let i = 0; i < quantity; i++) { |
| 14 | + const id = startId + i; |
| 15 | + products.push({ |
| 16 | + id: id, |
| 17 | + name: 'Item name ' + id, |
| 18 | + price: 2100 + i |
| 19 | + }); |
| 20 | + } |
| 21 | +} |
| 22 | +addProducts(5); |
| 23 | + |
| 24 | +/* |
| 25 | +Custom Download button that uses the DropdownButton component |
| 26 | +from react-bootstrap. |
| 27 | +*/ |
| 28 | +function DownloadButton({ bootStrapTableOnClick, onSelect }) { |
| 29 | + /* |
| 30 | + This handler is fired by the react-bootstrap DropdownButton component |
| 31 | + when the user has selected an export option (tsv or csv). It passes |
| 32 | + in the eventKey from the MenuItem that was selected by the user. |
| 33 | + */ |
| 34 | + function handleOnSelect(eventKey) { |
| 35 | + /* |
| 36 | + Fire the user specified action and pass the key |
| 37 | + and react-bootstrap-table onClick function. |
| 38 | + */ |
| 39 | + onSelect(eventKey, bootStrapTableOnClick); |
| 40 | + } |
| 41 | + return ( |
| 42 | + <div> |
| 43 | + <DropdownButton bsStyle='default' title='Export' id='export' onSelect={ handleOnSelect }> |
| 44 | + <MenuItem eventKey='csv'>CSV</MenuItem> |
| 45 | + <MenuItem eventKey='tsv'>TSV</MenuItem> |
| 46 | + </DropdownButton> |
| 47 | + </div> |
| 48 | + ); |
| 49 | +} |
| 50 | +DownloadButton.propTypes = { |
| 51 | + // The onClick handler received from react-bootstrap-table. |
| 52 | + bootStrapTableOnClick: PropTypes.func.isRequired, |
| 53 | + // A handler that fires when the user selects an export option. |
| 54 | + onSelect: PropTypes.func.isRequired |
| 55 | +}; |
| 56 | + |
| 57 | +class CustomCSVOrTSVButtonTable extends React.Component { |
| 58 | + constructor(props) { |
| 59 | + super(props); |
| 60 | + this.state = { |
| 61 | + extension: 'csv', |
| 62 | + separator: ',' |
| 63 | + }; |
| 64 | + this.updateExportOpts = this.updateExportOpts.bind(this); |
| 65 | + this.getFilename = this.getFilename.bind(this); |
| 66 | + } |
| 67 | + |
| 68 | + getFilename() { |
| 69 | + return 'spreadsheet' + '.' + this.state.extension; |
| 70 | + } |
| 71 | + |
| 72 | + updateExportOpts(format, tableOnClick) { |
| 73 | + /* |
| 74 | + Here we pass the react-bootstrap-table onClick event handler to setState |
| 75 | + to ensure that it gets called after the state has been mutated. |
| 76 | + */ |
| 77 | + if (format === 'tsv') { |
| 78 | + this.setState({ extension: 'tsv', separator: '\t' }, tableOnClick); |
| 79 | + } else { |
| 80 | + this.setState({ extension: 'csv', separator: ',' }, tableOnClick); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + render() { |
| 85 | + const options = { |
| 86 | + exportCSVBtn: (onClick) => <DownloadButton bootStrapTableOnClick={ onClick } onSelect={ this.updateExportOpts } />, |
| 87 | + exportCSVSeparator: this.state.separator |
| 88 | + }; |
| 89 | + return ( |
| 90 | + <BootstrapTable data={ products } options={ options } csvFileName={ this.getFilename } exportCSV={ true }> |
| 91 | + <TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn> |
| 92 | + <TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn> |
| 93 | + <TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn> |
| 94 | + </BootstrapTable> |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
| 98 | +export default CustomCSVOrTSVButtonTable; |
0 commit comments