fix#264: wrong col span when enable selection in a empty table (#265)

* noDataIndication

* use the correct amount of cells when the first row is select
* storybook added for development, not necessary in docs

fixes react-bootstrap-table/react-bootstrap-table2#264

* eslint complaints

  4:11  error  'columnLen' is never reassigned. Use 'const' instead                   prefer-const
  7:9   error  Expected an assignment or function call and instead saw an expression  no-unused-expressions

* tests updated
This commit is contained in:
Patrick O'Meara 2018-03-25 19:36:58 +11:00 committed by Allen
parent bd9150f88f
commit 42dbd00fd9
5 changed files with 76 additions and 7 deletions

View File

@ -0,0 +1,62 @@
/* eslint no-unused-vars: 0 */
import React from 'react';
import BootstrapTable from 'react-bootstrap-table-next';
import Code from 'components/common/code-block';
const columns = [{
dataField: 'id',
text: 'Product ID'
}, {
dataField: 'name',
text: 'Product Name'
}, {
dataField: 'price',
text: 'Product Price'
}];
const selectRow1 = {
mode: 'checkbox',
clickToSelect: true
};
const sourceCode1 = `\
import BootstrapTable from 'react-bootstrap-table-next';
const columns = [{
dataField: 'id',
text: 'Product ID'
}, {
dataField: 'name',
text: 'Product Name'
}, {
dataField: 'price',
text: 'Product Price'
}];
const selectRow = {
mode: 'checkbox',
clickToSelect: true
};
<BootstrapTable
keyField='id'
data={ [] }
columns={ columns }
selectRow={ selectRow }
noDataIndication={ 'no results found' }
/>
`;
export default () => (
<div>
<BootstrapTable
keyField="id"
data={ [] }
columns={ columns }
selectRow={ selectRow1 }
noDataIndication={ 'no results found' }
/>
<Code>{ sourceCode1 }</Code>
</div>
);

View File

@ -81,6 +81,7 @@ import ClickToSelectTable from 'examples/row-selection/click-to-select';
import DefaultSelectTable from 'examples/row-selection/default-select';
import SelectionManagement from 'examples/row-selection/selection-management';
import ClickToSelectWithCellEditTable from 'examples/row-selection/click-to-select-with-cell-edit';
import SelectionNoDataTable from 'examples/row-selection/selection-no-data';
import SelectionStyleTable from 'examples/row-selection/selection-style';
import SelectionClassTable from 'examples/row-selection/selection-class';
import NonSelectableRowsTable from 'examples/row-selection/non-selectable-rows';
@ -196,6 +197,7 @@ storiesOf('Row Selection', module)
.add('Default Select', () => <DefaultSelectTable />)
.add('Selection Management', () => <SelectionManagement />)
.add('Click to Select and Edit Cell', () => <ClickToSelectWithCellEditTable />)
.add('Selection without Data', () => <SelectionNoDataTable />)
.add('Selection Style', () => <SelectionStyleTable />)
.add('Selection Class', () => <SelectionClassTable />)
.add('Selection Background Color', () => <SelectionBgColorTable />)

View File

@ -1,6 +1,11 @@
export default ExtendBase =>
class ColumnResolver extends ExtendBase {
visibleColumnSize() {
return this.props.columns.filter(c => !c.hidden).length;
visibleColumnSize(includeSelectColumn = true) {
const columnLen = this.props.columns.filter(c => !c.hidden).length;
if (!includeSelectColumn) return columnLen;
if (this.props.selectRow && !this.props.selectRow.hideSelectColumn) {
return columnLen + 1;
}
return columnLen;
}
};

View File

@ -5,12 +5,12 @@ import _ from '../utils';
export default ExtendBase =>
class TableResolver extends ColumnResolver(ExtendBase) {
validateProps() {
const { columns, keyField } = this.props;
const { keyField } = this.props;
if (!keyField) {
throw new Error('Please specify a field as key via keyField');
}
if (this.visibleColumnSize(columns) <= 0) {
throw new Error('No any visible columns detect');
if (this.visibleColumnSize(false) <= 0) {
throw new Error('No visible columns detected');
}
}

View File

@ -56,7 +56,7 @@ describe('TableResolver', () => {
});
});
describe('if columns is all unvisible', () => {
describe('if no columns are visible', () => {
beforeEach(() => {
const mockElement = React.createElement(BootstrapTableMock, {
data, keyField, columns: []
@ -67,7 +67,7 @@ describe('TableResolver', () => {
it('should throw error', () => {
expect(() =>
wrapper.instance().validateProps()
).toThrow(new Error('No any visible columns detect'));
).toThrow(new Error('No visible columns detected'));
});
});
});