This commit is contained in:
AllenFang 2019-04-27 14:32:09 +08:00
parent ce7e05d7f9
commit 0f37fae23d
5 changed files with 155 additions and 3 deletions

View File

@ -20,6 +20,7 @@
* [hideSelectAll](#hideSelectAll)
* [selectionRenderer](#selectionRenderer)
* [selectionHeaderRenderer](#selectionHeaderRenderer)
* [headerColumnStyle](#headerColumnStyle)
### <a name="mode">selectRow.mode - [String]</a>
@ -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`.

View File

@ -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>
);

View File

@ -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';
@ -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 />)

View File

@ -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,

View File

@ -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
@ -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() {
@ -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 />;
}
@ -79,6 +87,10 @@ export default class SelectionHeaderCell extends Component {
attrs.onClick = this.handleCheckBoxClick;
}
attrs.style = _.isFunction(headerColumnStyle) ?
headerColumnStyle(checkedStatus) :
headerColumnStyle;
return (
<BootstrapContext.Consumer>
{