-
Notifications
You must be signed in to change notification settings - Fork 775
/
Copy pathcustom-cell-edit-table.js
207 lines (194 loc) · 6.44 KB
/
custom-cell-edit-table.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* eslint max-len: 0 */
/* eslint no-alert: 0 */
/* eslint guard-for-in: 0 */
/* eslint no-unused-vars: 0 */
import React from 'react';
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
const products = [];
const currencies = [ 'USD', 'GBP', 'EUR' ];
const regions = [ 'North', 'South', 'East', 'West' ];
function addProducts(quantity) {
const startId = products.length;
for (let i = 0; i < quantity; i++) {
const id = startId + i;
products.push({
id: id,
name: 'Item name ' + id,
price: {
amount: 2100 + i,
currency: currencies[i % currencies.length]
},
regions: regions.slice(0, (i % regions.length) + 1)
});
}
}
addProducts(5);
const cellEditProp = {
mode: 'click'
};
class NameEditor extends React.Component {
constructor(props) {
super(props);
this.updateData = this.updateData.bind(this);
this.state = {
name: props.defaultValue,
open: true
};
}
focus() {
this.refs.inputRef.focus();
}
updateData() {
this.props.onUpdate(this.state.name);
}
close = () => {
this.setState({ open: false });
this.props.onUpdate(this.props.defaultValue);
}
render() {
const fadeIn = this.state.open ? 'in' : '';
const display = this.state.open ? 'block' : 'none';
return (
<div className={ `modal fade ${fadeIn}` } id='myModal' role='dialog' style={ { display } }>
<div className='modal-dialog'>
<div className='modal-content'>
<div className='modal-body'>
<input
ref='inputRef'
className={ ( this.props.editorClass || '') + ' form-control editor edit-text' }
style={ { display: 'inline', width: '50%' } }
type='text'
value={ this.state.name }
onChange={ e => { this.setState({ name: e.currentTarget.value }); } } />
</div>
<div className='modal-footer'>
<button type='button' className='btn btn-primary' onClick={ this.updateData }>Save</button>
<button type='button' className='btn btn-default' onClick={ this.close }>Close</button>
</div>
</div>
</div>
</div>
);
}
}
class PriceEditor extends React.Component {
constructor(props) {
super(props);
this.updateData = this.updateData.bind(this);
this.state = { amount: props.defaultValue.amount, currency: props.defaultValue.currency };
}
focus() {
this.refs.inputRef.focus();
}
updateData() {
this.props.onUpdate({ amount: this.state.amount, currency: this.state.currency });
}
render() {
return (
<span>
<input
ref='inputRef'
className={ ( this.props.editorClass || '') + ' form-control editor edit-text' }
style={ { display: 'inline', width: '50%' } }
type='text'
value={ this.state.amount }
onKeyDown={ this.props.onKeyDown }
onChange={ (ev) => { this.setState({ amount: parseInt(ev.currentTarget.value, 10) }); } } />
<select
value={ this.state.currency }
onKeyDown={ this.props.onKeyDown }
onChange={ (ev) => { this.setState({ currency: ev.currentTarget.value }); } } >
{ currencies.map(currency => (<option key={ currency } value={ currency }>{ currency }</option>)) }
</select>
<button
className='btn btn-info btn-xs textarea-save-btn'
onClick={ this.updateData }>
save
</button>
</span>
);
}
}
class RegionsEditor extends React.Component {
constructor(props) {
super(props);
this.updateData = this.updateData.bind(this);
this.state = { regions: props.defaultValue };
this.onToggleRegion = this.onToggleRegion.bind(this);
}
focus() {
}
onToggleRegion(event) {
const region = event.currentTarget.name;
if (this.state.regions.indexOf(region) < 0) {
this.setState({ regions: this.state.regions.concat([ region ]) });
} else {
this.setState({ regions: this.state.regions.filter(r => r !== region) });
}
}
updateData() {
this.props.onUpdate(this.state.regions);
}
render() {
const regionCheckBoxes = regions.map(region => (
<span key={ `span-${region}` }>
<input
type='checkbox'
key={ region }
name={ region }
checked={ this.state.regions.indexOf(region) > -1 }
onKeyDown={ this.props.onKeyDown }
onChange={ this.onToggleRegion } />
<label key={ `label-${region}` } htmlFor={ region }>{ region }</label>
</span>
));
return (
<span ref='inputRef'>
{ regionCheckBoxes }
<button
className='btn btn-info btn-xs textarea-save-btn'
onClick={ this.updateData }>
save
</button>
</span>
);
}
}
function priceFormatter(cell, row) {
return `<i class='glyphicon glyphicon-${cell.currency.toLowerCase()}'></i> ${cell.amount}`;
}
const regionsFormatter = (cell, row) => (<span>{ (cell || []).join(',') }</span>);
/*
The getElement function take two arguments,
1. onUpdate: if you want to apply the modified data, call this function
2. props: contain customEditorParameters, whole row data, defaultValue and attrs
*/
const createNameEditor = (onUpdate, props) => (<NameEditor onUpdate={ onUpdate } {...props}/>);
const createPriceEditor = (onUpdate, props) => (<PriceEditor onUpdate={ onUpdate } {...props}/>);
const createRegionsEditor = (onUpdate, props) => (<RegionsEditor onUpdate={ onUpdate } {...props}/>);
export default class CustomCellEditTable extends React.Component {
render() {
return (
<BootstrapTable data={ products } cellEdit={ cellEditProp }>
<TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
<TableHeaderColumn
dataField='name'
customEditor={ { getElement: createNameEditor } }>
Product Name
</TableHeaderColumn>
<TableHeaderColumn
dataField='price'
dataFormat={ priceFormatter }
customEditor={ { getElement: createPriceEditor, customEditorParameters: { currencies: currencies } } }>
Product Price
</TableHeaderColumn>
<TableHeaderColumn
dataField='regions'
dataFormat={ regionsFormatter }
customEditor={ { getElement: createRegionsEditor } }>
Regions
</TableHeaderColumn>
</BootstrapTable>
);
}
}