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
26 changes: 26 additions & 0 deletions docs/row-selection.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* [hideSelectAll](#hideSelectAll)
* [selectionRenderer](#selectionRenderer)
* [selectionHeaderRenderer](#selectionHeaderRenderer)
* [headerColumnStyle](#headerColumnStyle)

### <a name="mode">selectRow.mode - [String]</a>

Expand Down Expand Up @@ -198,6 +199,31 @@ const selectRow = {

> By default, `react-bootstrap-table2` will help you to handle the click event, it's not necessary to handle again by developer.


### <a name='headerColumnStyle'>selectRow.headerColumnStyle - [Object | Function]</a>
A way to custome the selection header cell. `headerColumnStyle` not only accept a simple style object but also a callback function for more flexible customization:

### Style Object

```js
const selectRow = {
mode: 'checkbox',
headerColumnStyle: { backgroundColor: 'blue' }
};
```

### Callback Function

```js
const selectRow = {
mode: 'checkbox',
headerColumnStyle: (status) => (
// status available value is checked, indeterminate and unchecked
return { backgroundColor: 'blue' };
)
};
```

### <a name='onSelect'>selectRow.onSelect - [Function]</a>
This callback function will be called when a row is select/unselect and pass following three arguments:
`row`, `isSelect`, `rowIndex` and `e`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ class ExposedFunctionTable extends React.Component {
console.log(this.node.table.props.data);
}

handleGetCurrentData = () => {
console.log(this.node.table.props.data);
}

handleGetSelectedData = () => {
console.log(this.node.selectionContext.state.selected);
console.log(this.node.selectionContext.selected);
}

handleGetExpandedData = () => {
Expand Down Expand Up @@ -117,7 +121,7 @@ export default class ExposedFunctionTable extends React.Component {
}

handleGetSelectedData = () => {
console.log(this.node.selectionContext.state.selected);
console.log(this.node.selectionContext.selected);
}

handleGetExpandedData = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const columns = [{
}];

const expandRow = {
onlyOneExpanding: true,
renderer: row => (
<div>
<p>{ \`This Expand row is belong to rowKey $\{row.id}\` }</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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(2);

const columns = [{
dataField: 'id',
text: 'Product ID'
}, {
dataField: 'name',
text: 'Product Name'
}, {
dataField: 'price',
text: 'Product Price'
}];

const selectRow1 = {
mode: 'checkbox',
clickToSelect: true,
headerColumnStyle: {
backgroundColor: 'blue'
}
};

const sourceCode1 = `\
import BootstrapTable from 'react-bootstrap-table-next';

const columns = ...

const selectRow = {
mode: 'checkbox',
clickToSelect: true,
headerColumnStyle: {
backgroundColor: 'blue'
}
};

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

const selectRow2 = {
mode: 'checkbox',
clickToSelect: true,
headerColumnStyle: (status) => {
if (status === 'checked') {
return {
backgroundColor: 'yellow'
};
} else if (status === 'indeterminate') {
return {
backgroundColor: 'pink'
};
} else if (status === 'unchecked') {
return {
backgroundColor: 'grey'
};
}
return {};
}
};

const sourceCode2 = `\
import BootstrapTable from 'react-bootstrap-table-next';

const columns = ...

const selectRow = {
mode: 'checkbox',
clickToSelect: true,
headerColumnStyle: (status) => {
if (status === 'checked') {
return {
backgroundColor: 'yellow'
};
} else if (status === 'indeterminate') {
return {
backgroundColor: 'pink'
};
} else if (status === 'unchecked') {
return {
backgroundColor: 'grey'
};
}
return {};
}
};

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

export default () => (
<div>
<BootstrapTable keyField="id" data={ products } columns={ columns } selectRow={ selectRow1 } />
<Code>{ sourceCode1 }</Code>
<BootstrapTable keyField="id" data={ products } columns={ columns } selectRow={ selectRow2 } />
<Code>{ sourceCode2 }</Code>
</div>
);
2 changes: 2 additions & 0 deletions packages/react-bootstrap-table2-example/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ import SelectionWithExpansionTable from 'examples/row-selection/selection-with-e
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 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';
Expand Down Expand Up @@ -385,6 +386,7 @@ storiesOf('Row Selection', module)
.add('Selection without Data', () => <SelectionNoDataTable />)
.add('Selection Style', () => <SelectionStyleTable />)
.add('Selection Class', () => <SelectionClassTable />)
.add('Custom Selection Column Header Style', () => <HeaderStyleTable />)
.add('Hide Select All', () => <HideSelectAllTable />)
.add('Custom Selection', () => <CustomSelectionTable />)
.add('Selection Background Color', () => <SelectionBgColorTable />)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const transform = (
cellContent = m.formatter(cellContent, row, rowIndex, m.formatExtraData);
}
if (m.type === String) {
return `"${cellContent}"`;
return `"${`${cellContent}`.replace(/"/g, '""')}"`;
}
return cellContent;
}).join(separator)).join('\n');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ SearchBar.defaultProps = {
placeholder: 'Search',
delay: 250,
searchText: '',
tableId: 0
tableId: '0'
};

export default SearchBar;
3 changes: 2 additions & 1 deletion packages/react-bootstrap-table2/src/bootstrap-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ BootstrapTable.propTypes = {
bgColor: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
hideSelectColumn: PropTypes.bool,
selectionRenderer: PropTypes.func,
selectionHeaderRenderer: PropTypes.func
selectionHeaderRenderer: PropTypes.func,
headerColumnStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.func])
}),
expandRow: PropTypes.shape({
renderer: PropTypes.func,
Expand Down
21 changes: 7 additions & 14 deletions packages/react-bootstrap-table2/src/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import _ from './utils';
class Cell extends eventDelegater(Component) {
constructor(props) {
super(props);
this.handleEditingCell = this.handleEditingCell.bind(this);
this.createHandleEditingCell = this.createHandleEditingCell.bind(this);
}

shouldComponentUpdate(nextProps) {
Expand Down Expand Up @@ -43,17 +43,10 @@ class Cell extends eventDelegater(Component) {
return shouldUpdate;
}

handleEditingCell(e) {
const { column, onStart, rowIndex, columnIndex, clickToEdit, dbclickToEdit } = this.props;
const { events } = column;
if (events) {
if (clickToEdit) {
const customClick = events.onClick;
if (_.isFunction(customClick)) customClick(e);
} else if (dbclickToEdit) {
const customDbClick = events.onDoubleClick;
if (_.isFunction(customDbClick)) customDbClick(e);
}
createHandleEditingCell = originFunc => (e) => {
const { onStart, rowIndex, columnIndex, clickToEdit, dbclickToEdit } = this.props;
if ((clickToEdit || dbclickToEdit) && _.isFunction(originFunc)) {
originFunc(e);
}
if (onStart) {
onStart(rowIndex, columnIndex);
Expand Down Expand Up @@ -85,9 +78,9 @@ class Cell extends eventDelegater(Component) {
}

if (clickToEdit && editable) {
attrs.onClick = this.handleEditingCell;
attrs.onClick = this.createHandleEditingCell(attrs.onClick);
} else if (dbclickToEdit && editable) {
attrs.onDoubleClick = this.handleEditingCell;
attrs.onDoubleClick = this.createHandleEditingCell(attrs.onDoubleClick);
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import dataOperator from '../store/operators';
import _ from '../utils';

const RowExpandContext = React.createContext();

Expand Down Expand Up @@ -81,7 +82,7 @@ class RowExpandProvider extends React.Component {
if (expandAll) {
currExpanded = expanded.concat(dataOperator.expandableKeys(data, keyField, nonExpandable));
} else {
currExpanded = expanded.filter(s => typeof data.find(d => d[keyField] === s) === 'undefined');
currExpanded = expanded.filter(s => typeof data.find(d => _.get(d, keyField) === s) === 'undefined');
}

if (onExpandAll) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Const from '../const';
import { BootstrapContext } from '../contexts/bootstrap';
import _ from '../utils';

export const CheckBox = ({ className, checked, indeterminate }) => (
<input
Expand All @@ -28,7 +29,8 @@ export default class SelectionHeaderCell extends Component {
checkedStatus: PropTypes.string,
onAllRowsSelect: PropTypes.func,
hideSelectAll: PropTypes.bool,
selectionHeaderRenderer: PropTypes.func
selectionHeaderRenderer: PropTypes.func,
headerColumnStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.func])
}

constructor() {
Expand Down Expand Up @@ -64,7 +66,13 @@ export default class SelectionHeaderCell extends Component {
CHECKBOX_STATUS_CHECKED, CHECKBOX_STATUS_INDETERMINATE, ROW_SELECT_MULTIPLE
} = Const;

const { mode, checkedStatus, selectionHeaderRenderer, hideSelectAll } = this.props;
const {
mode,
checkedStatus,
selectionHeaderRenderer,
hideSelectAll,
headerColumnStyle
} = this.props;
if (hideSelectAll) {
return <th data-row-selection />;
}
Expand All @@ -79,6 +87,10 @@ export default class SelectionHeaderCell extends Component {
attrs.onClick = this.handleCheckBoxClick;
}

attrs.style = _.isFunction(headerColumnStyle) ?
headerColumnStyle(checkedStatus) :
headerColumnStyle;

return (
<BootstrapContext.Consumer>
{
Expand Down
3 changes: 2 additions & 1 deletion packages/react-bootstrap-table2/src/store/rows.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from '../utils';

export const matchRow = (keyField, id) => row => row[keyField] === id;
export const matchRow = (keyField, id) => row => _.get(row, keyField) === id;

export const getRowByRowId = (data, keyField, id) => data.find(matchRow(keyField, id));
8 changes: 4 additions & 4 deletions packages/react-bootstrap-table2/test/cell.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ describe('Cell', () => {
onClick: sinon.stub()
};
});
it('should calling custom onClick callback also', () => {

it('should call onStart correctly', () => {
wrapper.find('td').simulate('click');
expect(onStartCallBack.callCount).toBe(1);
expect(column.events.onClick.callCount).toBe(1);
});
});
});
Expand Down Expand Up @@ -164,10 +164,10 @@ describe('Cell', () => {
onDoubleClick: sinon.stub()
};
});
it('should calling custom onDoubleClick callback also', () => {

it('should call onStart correctly', () => {
wrapper.find('td').simulate('doubleclick');
expect(onStartCallBack.callCount).toBe(1);
expect(column.events.onDoubleClick.callCount).toBe(1);
});
});
});
Expand Down