|
| 1 | +/* eslint max-len: 0 */ |
| 2 | +/* eslint no-unused-vars: 0 */ |
| 3 | +import React from 'react'; |
| 4 | +import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table'; |
| 5 | + |
| 6 | + |
| 7 | +const jobs = []; |
| 8 | +const jobTypes = [ 'A', 'B', 'C', 'D' ]; |
| 9 | + |
| 10 | +function addJobs(quantity) { |
| 11 | + const startId = jobs.length; |
| 12 | + for (let i = 0; i < quantity; i++) { |
| 13 | + const id = startId + i; |
| 14 | + jobs.push({ |
| 15 | + id: id, |
| 16 | + status: '200', |
| 17 | + name: 'Item name ' + id, |
| 18 | + type: 'B', |
| 19 | + active: i % 2 === 0 ? 'Y' : 'N' |
| 20 | + }); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +addJobs(5); |
| 25 | + |
| 26 | +const cellEditProp = { |
| 27 | + mode: 'click', |
| 28 | + blurToSave: true |
| 29 | +}; |
| 30 | + |
| 31 | +// validator function pass the user input value and row object. In addition, a bool return value is expected |
| 32 | +function jobNameValidator(value, row) { |
| 33 | + const response = { isValid: true, notification: { type: 'success', msg: '', title: '' } }; |
| 34 | + if (!value) { |
| 35 | + response.isValid = false; |
| 36 | + response.notification.type = 'error'; |
| 37 | + response.notification.msg = 'Value must be inserted'; |
| 38 | + response.notification.title = 'Requested Value'; |
| 39 | + } else if (value.length < 10) { |
| 40 | + response.isValid = false; |
| 41 | + response.notification.type = 'error'; |
| 42 | + response.notification.msg = 'Value must have 10+ characters'; |
| 43 | + response.notification.title = 'Invalid Value'; |
| 44 | + } |
| 45 | + return response; |
| 46 | +} |
| 47 | + |
| 48 | +function jobStatusValidator(value, row) { |
| 49 | + const nan = isNaN(parseInt(value, 10)); |
| 50 | + if (nan) { |
| 51 | + return 'Job Status must be a integer!'; |
| 52 | + } |
| 53 | + return true; |
| 54 | +} |
| 55 | + |
| 56 | +export default class EditTypeReadOnlyTable extends React.Component { |
| 57 | + render() { |
| 58 | + return ( |
| 59 | + <BootstrapTable data={ jobs } cellEdit={ cellEditProp } insertRow={ true }> |
| 60 | + <TableHeaderColumn dataField='id' isKey={ true }>Job ID</TableHeaderColumn> |
| 61 | + <TableHeaderColumn dataField='status' editable={ { validator: jobStatusValidator } }>Job Status</TableHeaderColumn> |
| 62 | + <TableHeaderColumn dataField='name' editable={ { type: 'textarea', validator: jobNameValidator, readOnly: true } }>Job Name</TableHeaderColumn> |
| 63 | + <TableHeaderColumn dataField='type' editable={ { type: 'select', options: { values: jobTypes } } }>Job Type</TableHeaderColumn> |
| 64 | + <TableHeaderColumn dataField='active' editable={ { type: 'checkbox', options: { values: 'Y:N' } } }>Active</TableHeaderColumn> |
| 65 | + </BootstrapTable> |
| 66 | + ); |
| 67 | + } |
| 68 | +} |
0 commit comments