add selection management example

This commit is contained in:
AllenFang 2018-03-18 16:44:39 +08:00
parent 6f9361934a
commit d5ddd8c3af
2 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,144 @@
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 sourceCode = `\
import BootstrapTable from 'react-bootstrap-table-next';
class SelectionManagment extends React.Component {
constructor(props) {
super(props);
this.state = { selected: [0, 1] };
}
handleBtnClick = () => {
if (!this.state.selected.includes(2)) {
this.setState(() => ({
selected: [...this.state.selected, 2]
}));
} else {
this.setState(() => ({
selected: this.state.selected.filter(x => x !== 2)
}));
}
}
handleOnSelect = (row, isSelect) => {
if (isSelect) {
this.setState(() => ({
selected: [...this.state.selected, row.id]
}));
} else {
this.setState(() => ({
selected: this.state.selected.filter(x => x !== row.id)
}));
}
}
handleOnSelectAll = (isSelect, rows) => {
const ids = rows.map(r => r.id);
if (isSelect) {
this.setState(() => ({
selected: ids
}));
} else {
this.setState(() => ({
selected: []
}));
}
}
render() {
const selectRow = {
mode: 'checkbox',
clickToSelect: true,
selected: this.state.selected,
onSelect: this.handleOnSelect,
onSelectAll: this.handleOnSelectAll
};
return (
<div>
<button className="btn btn-success" onClick={ this.handleBtnClick }>Select/UnSelect 3rd row</button>
<BootstrapTable keyField="id" data={ products } columns={ columns } selectRow={ selectRow } />
<Code>{ sourceCode }</Code>
</div>
);
}
}
`;
export default class SelectionManagment extends React.Component {
constructor(props) {
super(props);
this.state = { selected: [0, 1] };
}
handleBtnClick = () => {
if (!this.state.selected.includes(2)) {
this.setState(() => ({
selected: [...this.state.selected, 2]
}));
} else {
this.setState(() => ({
selected: this.state.selected.filter(x => x !== 2)
}));
}
}
handleOnSelect = (row, isSelect) => {
if (isSelect) {
this.setState(() => ({
selected: [...this.state.selected, row.id]
}));
} else {
this.setState(() => ({
selected: this.state.selected.filter(x => x !== row.id)
}));
}
}
handleOnSelectAll = (isSelect, rows) => {
const ids = rows.map(r => r.id);
if (isSelect) {
this.setState(() => ({
selected: ids
}));
} else {
this.setState(() => ({
selected: []
}));
}
}
render() {
const selectRow = {
mode: 'checkbox',
clickToSelect: true,
selected: this.state.selected,
onSelect: this.handleOnSelect,
onSelectAll: this.handleOnSelectAll
};
return (
<div>
<button className="btn btn-success" onClick={ this.handleBtnClick }>Select/UnSelect 3rd row</button>
<BootstrapTable keyField="id" data={ products } columns={ columns } selectRow={ selectRow } />
<Code>{ sourceCode }</Code>
</div>
);
}
}

View File

@ -79,6 +79,7 @@ import SingleSelectionTable from 'examples/row-selection/single-selection';
import MultipleSelectionTable from 'examples/row-selection/multiple-selection';
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 SelectionStyleTable from 'examples/row-selection/selection-style';
import SelectionClassTable from 'examples/row-selection/selection-class';
@ -193,6 +194,7 @@ storiesOf('Row Selection', module)
.add('Multiple Selection', () => <MultipleSelectionTable />)
.add('Click to Select', () => <ClickToSelectTable />)
.add('Default Select', () => <DefaultSelectTable />)
.add('Selection Management', () => <SelectionManagement />)
.add('Click to Select and Edit Cell', () => <ClickToSelectWithCellEditTable />)
.add('Selection Style', () => <SelectionStyleTable />)
.add('Selection Class', () => <SelectionClassTable />)