|
| 1 | +/* eslint max-len: 0 */ |
| 2 | +/* eslint no-alert: 0 */ |
| 3 | +/* eslint guard-for-in: 0 */ |
| 4 | +/* eslint no-unused-vars: 0 */ |
| 5 | +import React from 'react'; |
| 6 | +import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table'; |
| 7 | + |
| 8 | + |
| 9 | +const products = []; |
| 10 | + |
| 11 | +const currencies = [ 'USD', 'GBP', 'EUR' ]; |
| 12 | +const regions = [ 'North', 'South', 'East', 'West' ]; |
| 13 | + |
| 14 | +function addProducts(quantity) { |
| 15 | + const startId = products.length; |
| 16 | + for (let i = 0; i < quantity; i++) { |
| 17 | + const id = startId + i; |
| 18 | + products.push({ |
| 19 | + id: id, |
| 20 | + name: 'Item name ' + id, |
| 21 | + price: { |
| 22 | + amount: 2100 + i, |
| 23 | + currency: currencies[i % currencies.length] |
| 24 | + }, |
| 25 | + regions: regions.slice(0, (i % regions.length) + 1) |
| 26 | + }); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +addProducts(5); |
| 31 | + |
| 32 | +const cellEditProp = { |
| 33 | + mode: 'click' |
| 34 | +}; |
| 35 | + |
| 36 | +class PriceEditor extends React.Component { |
| 37 | + constructor(props) { |
| 38 | + super(props); |
| 39 | + this.updateData = this.updateData.bind(this); |
| 40 | + this.state = { amount: props.defaultValue.amount, currency: props.defaultValue.currency }; |
| 41 | + } |
| 42 | + focus() { |
| 43 | + this.refs.inputRef.focus(); |
| 44 | + } |
| 45 | + updateData() { |
| 46 | + this.props.onUpdate({ amount: this.state.amount, currency: this.state.currency }); |
| 47 | + } |
| 48 | + render() { |
| 49 | + return ( |
| 50 | + <span> |
| 51 | + <input |
| 52 | + ref='inputRef' |
| 53 | + { ...this.props } |
| 54 | + className={ ( this.props.editorClass || '') + ' form-control editor edit-text' } |
| 55 | + style={ { display: 'inline', width: '50%' } } |
| 56 | + type='text' |
| 57 | + value={ this.state.amount } |
| 58 | + onChange={ (ev) => { this.setState({ amount: parseInt(ev.currentTarget.value, 10) }); } } /> |
| 59 | + <select |
| 60 | + value={ this.state.currency } |
| 61 | + onChange={ (ev) => { this.setState({ currency: ev.currentTarget.value }); } } > |
| 62 | + { currencies.map(currency => (<option key={ currency } value={ currency }>{ currency }</option>)) } |
| 63 | + </select> |
| 64 | + <button |
| 65 | + className='btn btn-info btn-xs textarea-save-btn' |
| 66 | + onClick={ this.updateData }> |
| 67 | + save |
| 68 | + </button> |
| 69 | + </span> |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +class RegionsEditor extends React.Component { |
| 75 | + constructor(props) { |
| 76 | + super(props); |
| 77 | + this.updateData = this.updateData.bind(this); |
| 78 | + this.state = { regions: props.defaultValue }; |
| 79 | + this.onToggleRegion = this.onToggleRegion.bind(this); |
| 80 | + } |
| 81 | + focus() { |
| 82 | + } |
| 83 | + onToggleRegion(event) { |
| 84 | + const region = event.currentTarget.name; |
| 85 | + if (this.state.regions.indexOf(region) < 0) { |
| 86 | + this.setState({ regions: this.state.regions.concat([ region ]) }); |
| 87 | + } else { |
| 88 | + this.setState({ regions: this.state.regions.filter(r => r !== region) }); |
| 89 | + } |
| 90 | + } |
| 91 | + updateData() { |
| 92 | + this.props.onUpdate(this.state.regions); |
| 93 | + } |
| 94 | + render() { |
| 95 | + const regionCheckBoxes = regions.map(region => ( |
| 96 | + <span key={ `span-${region}` }> |
| 97 | + <input |
| 98 | + type='checkbox' |
| 99 | + key={ region } |
| 100 | + name={ region } |
| 101 | + checked={ this.state.regions.indexOf(region) > -1 } |
| 102 | + onChange={ this.onToggleRegion } /> |
| 103 | + <label key={ `label-${region}` } htmlFor={ region }>{ region }</label> |
| 104 | + </span> |
| 105 | + )); |
| 106 | + return ( |
| 107 | + <span ref='inputRef'> |
| 108 | + { regionCheckBoxes } |
| 109 | + <button |
| 110 | + className='btn btn-info btn-xs textarea-save-btn' |
| 111 | + onClick={ this.updateData }> |
| 112 | + save |
| 113 | + </button> |
| 114 | + </span> |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +function priceFormatter(cell, row) { |
| 120 | + return `<i class='glyphicon glyphicon-${cell.currency.toLowerCase()}'></i> ${cell.amount}`; |
| 121 | +} |
| 122 | + |
| 123 | +const regionsFormatter = (cell, row) => (<span>{ (cell || []).join(',') }</span>); |
| 124 | + |
| 125 | +export default class CustomCellEditTable extends React.Component { |
| 126 | + render() { |
| 127 | + return ( |
| 128 | + <BootstrapTable data={ products } cellEdit={ cellEditProp }> |
| 129 | + <TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn> |
| 130 | + <TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn> |
| 131 | + <TableHeaderColumn |
| 132 | + dataField='price' |
| 133 | + dataFormat={ priceFormatter } |
| 134 | + customEditor={ PriceEditor }> |
| 135 | + Product Price |
| 136 | + </TableHeaderColumn> |
| 137 | + <TableHeaderColumn |
| 138 | + dataField='regions' |
| 139 | + dataFormat={ regionsFormatter } |
| 140 | + customEditor={ RegionsEditor }> |
| 141 | + Regions |
| 142 | + </TableHeaderColumn> |
| 143 | + </BootstrapTable> |
| 144 | + ); |
| 145 | + } |
| 146 | +} |
0 commit comments