Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions docs/row-selection.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# Row selection
`react-bootstrap-table2` supports the row selection feature. By passing prop `selectRow` to enable row selection. When you enable this feature, `react-bootstrap-table2` will prepend a new selection column.
`react-bootstrap-table2` supports the row selection feature. By passing prop `selectRow` to enable row selection. When you enable this feature, `react-bootstrap-table2` will prepend a new selection column.

## Required
* [mode (**required**)](#mode)
Expand All @@ -11,6 +11,8 @@
* [classes)](#classes)
* [bgColor](#bgColor)
* [nonSelectable)](#nonSelectable)
* [nonSelectableStyle](#nonSelectableStyle)
* [nonSelectableClasses](#nonSelectableClasses)
* [clickToSelect)](#clickToSelect)
* [clickToExpand)](#clickToExpand)
* [clickToEdit](#clickToEdit)
Expand Down Expand Up @@ -139,6 +141,54 @@ const selectRow = {
};
```

### <a name='nonSelectableStyle'>selectRow.nonSelectableStyle - [Object | Function]</a>
This prop allow you to customize the non selectable rows. `selectRow.nonSelectableStyle` accepts an style object
and a callback function for more flexible customization.

### Style Object

```js
const selectRow = {
mode: 'checkbox',
nonSelectable: [1, 3 ,5],
nonSelectableStyle: { backgroundColor: 'gray' }
};
```

### Callback Function

```js
const selectRow = {
mode: 'checkbox',
nonSelectable: [1, 3 ,5],
nonSelectableStyle: (row, rowIndex) => { return ...; }
};
```

### <a name='nonSelectableClasses'>selectRow.nonSelectableClasses - [String | Function]</a>
This prop allow you to set a custom class for the non selectable rows, or use a callback function for more
flexible customization

### String

```js
const selectRow = {
mode: 'checkbox',
nonSelectable: [1, 3 ,5],
nonSelectableClasses: 'my-class'
};
```

### Callback Function

```js
const selectRow = {
mode: 'checkbox',
nonSelectable: [1, 3 ,5],
nonSelectableClasses: (row, rowIndex) => { return ...; }
};
```

### <a name='clickToSelect'>selectRow.clickToSelect - [Bool]</a>
Allow user to select row by clicking on the row.

Expand All @@ -149,7 +199,7 @@ const selectRow = {
};
```

> Note: When you also enable [cellEdit](./cell-edit.md), the `selectRow.clickToSelect` will deactivate the functionality of cell editing
> Note: When you also enable [cellEdit](./cell-edit.md), the `selectRow.clickToSelect` will deactivate the functionality of cell editing
> If you want to click on row to select row and edit cell simultaneously, you are suppose to enable [`selectRow.clickToEdit`](#clickToEdit)

### <a name='clickToExpand'>selectRow.clickToExpand - [Bool]</a>
Expand Down Expand Up @@ -307,7 +357,7 @@ const selectRow = {
mode: 'checkbox',
onSelectAll: (isSelect, rows, e) => {
if (isSelect && SOME_CONDITION) {
return [1, 3, 4]; // finally, key 1, 3, 4 will being selected
return [1, 3, 4]; // finally, key 1, 3, 4 will being selected
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react';

import BootstrapTable from 'react-bootstrap-table-next';
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 selectRow = {
mode: 'checkbox',
clickToSelect: true,
nonSelectable: [0, 2, 4],
nonSelectableClasses: 'row-index-bigger-than-2101'
};

const sourceCode = `\
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,
nonSelectable: [0, 2, 4],
nonSelectableClasses: 'row-index-bigger-than-2101'
};

<BootstrapTable
keyField='id'
data={ products }
columns={ columns }
selectRow={ selectRow }
/>
`;

export default () => (
<div>
<BootstrapTable keyField="id" data={ products } columns={ columns } selectRow={ selectRow } />
<Code>{ sourceCode }</Code>
</div>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react';

import BootstrapTable from 'react-bootstrap-table-next';
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 selectRow = {
mode: 'checkbox',
clickToSelect: true,
nonSelectable: [0, 2, 4],
nonSelectableStyle: { backgroundColor: 'gray' }
};

const sourceCode = `\
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,
nonSelectable: [0, 2, 4],
nonSelectableStyle: { backgroundColor: 'gray' }
};

<BootstrapTable
keyField='id'
data={ products }
columns={ columns }
selectRow={ selectRow }
/>
`;

export default () => (
<div>
<BootstrapTable keyField="id" data={ products } columns={ columns } selectRow={ selectRow } />
<Code>{ sourceCode }</Code>
</div>
);
4 changes: 4 additions & 0 deletions packages/react-bootstrap-table2-example/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ import HeaderStyleTable from 'examples/row-selection/header-style';
import HideSelectAllTable from 'examples/row-selection/hide-select-all';
import CustomSelectionTable from 'examples/row-selection/custom-selection';
import NonSelectableRowsTable from 'examples/row-selection/non-selectable-rows';
import NonSelectableRowsStyleTable from 'examples/row-selection/non-selectable-rows-style';
import NonSelectableRowsClassTable from 'examples/row-selection/non-selectable-rows-class';
import SelectionBgColorTable from 'examples/row-selection/selection-bgcolor';
import SelectionHooks from 'examples/row-selection/selection-hooks';
import HideSelectionColumnTable from 'examples/row-selection/hide-selection-column';
Expand Down Expand Up @@ -410,6 +412,8 @@ storiesOf('Row Selection', module)
.add('Custom Selection', () => <CustomSelectionTable />)
.add('Selection Background Color', () => <SelectionBgColorTable />)
.add('Not Selectabled Rows', () => <NonSelectableRowsTable />)
.add('Not Selectabled Rows Style', () => <NonSelectableRowsStyleTable />)
.add('Not Selectabled Rows Class', () => <NonSelectableRowsClassTable />)
.add('Selection Hooks', () => <SelectionHooks />)
.add('Hide Selection Column', () => <HideSelectionColumnTable />)
.add('Custom Selection Column Style', () => <SelectionColumnStyleTable />)
Expand Down
2 changes: 2 additions & 0 deletions packages/react-bootstrap-table2/src/bootstrap-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ BootstrapTable.propTypes = {
style: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
classes: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
nonSelectable: PropTypes.array,
nonSelectableStyle: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
nonSelectableClasses: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
bgColor: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
hideSelectColumn: PropTypes.bool,
selectionRenderer: PropTypes.func,
Expand Down
1 change: 1 addition & 0 deletions packages/react-bootstrap-table2/src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const Filters = (props) => {
columns.forEach((column, i) => {
filterColumns.push(<FiltersCell
index={ i }
key={ column.dataField }
column={ column }
currFilters={ currFilters }
onExternalFilter={ onExternalFilter }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class ExpandCell extends Component {
if (tabIndex !== -1) attrs.tabIndex = tabIndex;

return (
<td onClick={ this.handleClick } { ...attrs }>
<td className="expand-cell" onClick={ this.handleClick } { ...attrs }>
{
expandColumnRenderer ? expandColumnRenderer({
expandable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class ExpansionHeaderCell extends Component {
};

return (
<th data-row-selection { ...attrs }>
<th className="expand-cell-header" data-row-selection { ...attrs }>
{
expandHeaderColumnRenderer ?
expandHeaderColumnRenderer({ isAnyExpands }) :
Expand Down
17 changes: 17 additions & 0 deletions packages/react-bootstrap-table2/src/row-selection/row-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default (Component) => {
const key = props.value;
const selected = _.contains(selectRow.selected, key);
const selectable = !selectRow.nonSelectable || !_.contains(selectRow.nonSelectable, key);
const notSelectable = _.contains(selectRow.nonSelectable, key);

let {
style,
Expand Down Expand Up @@ -38,6 +39,22 @@ export default (Component) => {
}
}

if (notSelectable) {
const notSelectableStyle = _.isFunction(selectRow.nonSelectableStyle)
? selectRow.nonSelectableStyle(props.row, props.rowIndex)
: selectRow.nonSelectableStyle;

const notSelectableClasses = _.isFunction(selectRow.nonSelectableClasses)
? selectRow.nonSelectableClasses(props.row, props.rowIndex)
: selectRow.nonSelectableClasses;

style = {
...style,
...notSelectableStyle
};
className = cs(className, notSelectableClasses) || undefined;
}

return (
<Component
{ ...props }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default class SelectionCell extends Component {
<BootstrapContext.Consumer>
{
({ bootstrap4 }) => (
<td onClick={ this.handleClick } { ...attrs }>
<td className="selection-cell" onClick={ this.handleClick } { ...attrs }>
{
selectionRenderer ? selectionRenderer({
mode: inputType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export default class SelectionHeaderCell extends Component {
);
}
return (
<th data-row-selection { ...attrs }>{ content }</th>
<th className="selection-cell-header" data-row-selection { ...attrs }>{ content }</th>
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`<SelectionCell /> render should render component correctly 1`] = `
<td
className="selection-cell"
onClick={[Function]}
>
<input
Expand Down