import React from 'react'; import PropTypes from 'prop-types'; import BootstrapTable from 'react-bootstrap-table-next'; import cellEditFactory from 'react-bootstrap-table2-editor'; import Code from 'components/common/code-block'; import { productsGenerator } from 'utils/common'; const products = productsGenerator(); const columns = [{ dataField: 'id', text: 'Product ID' }, { dataField: 'name', text: 'Product Name' }, { dataField: 'price', text: 'Product Price' }]; const sourceCode = `\ import BootstrapTable from 'react-bootstrap-table-next'; import cellEditFactory from 'react-bootstrap-table2-editor'; // ... const RemoteCellEdit = (props) => { const cellEdit = { mode: 'click', errorMessage: props.errorMessage }; return (
{ sourceCode }
); }; class Container extends React.Component { constructor(props) { super(props); this.state = { data: products, errorMessage: null }; } handleTableChange = (type, { data, cellEdit: { rowId, dataField, newValue } }) => { setTimeout(() => { if (newValue === 'test' && dataField === 'name') { this.setState(() => ({ data, errorMessage: 'Oops, product name shouldn't be "test"' })); } else { const result = data.map((row) => { if (row.id === rowId) { const newRow = { ...row }; newRow[dataField] = newValue; return newRow; } return row; }); this.setState(() => ({ data: result, errorMessage: null })); } }, 2000); } render() { return ( ); } } `; const RemoteCellEdit = (props) => { const cellEdit = { mode: 'click', errorMessage: props.errorMessage }; return (
{ sourceCode }
); }; RemoteCellEdit.propTypes = { data: PropTypes.array.isRequired, onTableChange: PropTypes.func.isRequired, errorMessage: PropTypes.string.isRequired }; class Container extends React.Component { constructor(props) { super(props); this.state = { data: products, errorMessage: null }; } handleTableChange = (type, { data, cellEdit: { rowId, dataField, newValue } }) => { setTimeout(() => { if (newValue === 'test' && dataField === 'name') { this.setState(() => ({ data, errorMessage: 'Oops, product name shouldn\'t be "test"' })); } else { const result = data.map((row) => { if (row.id === rowId) { const newRow = { ...row }; newRow[dataField] = newValue; return newRow; } return row; }); this.setState(() => ({ data: result, errorMessage: null })); } }, 2000); } render() { return ( ); } } export default Container;