mirror of
https://github.com/gosticks/react-bootstrap-table2.git
synced 2025-10-16 11:55:39 +00:00
add dummy column story
This commit is contained in:
parent
849d9af8c4
commit
3b1fc3a559
221
packages/react-bootstrap-table2-example/examples/columns/dummy-column-table.js
vendored
Normal file
221
packages/react-bootstrap-table2-example/examples/columns/dummy-column-table.js
vendored
Normal file
@ -0,0 +1,221 @@
|
|||||||
|
/* eslint jsx-a11y/label-has-for: 0 */
|
||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import BootstrapTable from 'react-bootstrap-table-next';
|
||||||
|
import Code from 'components/common/code-block';
|
||||||
|
|
||||||
|
|
||||||
|
const products = [
|
||||||
|
{ id: 12, name: 'Item 12', price: 12.5, inStock: false },
|
||||||
|
{ id: 13, name: 'Item 13', price: 13.5, inStock: true },
|
||||||
|
{ id: 14, name: 'Item 14', price: 14.5, inStock: true }
|
||||||
|
];
|
||||||
|
|
||||||
|
const columns = [
|
||||||
|
{
|
||||||
|
dataField: 'id',
|
||||||
|
text: 'Product ID'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
dataField: 'name',
|
||||||
|
text: 'Product Name'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
dataField: 'price',
|
||||||
|
text: 'Product Price'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
dataField: 'inStock',
|
||||||
|
text: 'In Stock',
|
||||||
|
formatter: (cellContent, row) => (
|
||||||
|
<div className="checkbox disabled">
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" checked={ row.inStock } disabled />
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
dataField: 'df1',
|
||||||
|
isDummyField: true,
|
||||||
|
text: 'Action 1',
|
||||||
|
formatter: (cellContent, row) => {
|
||||||
|
if (row.inStock) {
|
||||||
|
return (
|
||||||
|
<h5>
|
||||||
|
<span className="label label-success"> Available</span>
|
||||||
|
</h5>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<h5>
|
||||||
|
<span className="label label-danger"> Backordered</span>
|
||||||
|
</h5>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
dataField: 'df2',
|
||||||
|
isDummyField: true,
|
||||||
|
text: 'Action 2',
|
||||||
|
formatter: (cellContent, row) => {
|
||||||
|
if (row.inStock) {
|
||||||
|
return (
|
||||||
|
<h5>
|
||||||
|
<span className="label label-success"> Available</span>
|
||||||
|
</h5>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<h5>
|
||||||
|
<span className="label label-danger"> Backordered</span>
|
||||||
|
</h5>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
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'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
dataField: 'inStock',
|
||||||
|
text: 'In Stock',
|
||||||
|
formatter: (cellContent, row) => (
|
||||||
|
<div className="checkbox disabled">
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" checked={ row.inStock } disabled />
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
dataField: 'df1',
|
||||||
|
isDummyField: true,
|
||||||
|
text: 'Action 1',
|
||||||
|
formatter: (cellContent, row) => {
|
||||||
|
if (row.inStock) {
|
||||||
|
return (
|
||||||
|
<h5>
|
||||||
|
<span className="label label-success"> Available</span>
|
||||||
|
</h5>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<h5>
|
||||||
|
<span className="label label-danger"> Backordered</span>
|
||||||
|
</h5>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
dataField: 'df2',
|
||||||
|
isDummyField: true,
|
||||||
|
text: 'Action 2',
|
||||||
|
formatter: (cellContent, row) => {
|
||||||
|
if (row.inStock) {
|
||||||
|
return (
|
||||||
|
<h5>
|
||||||
|
<span className="label label-success"> Available</span>
|
||||||
|
</h5>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<h5>
|
||||||
|
<span className="label label-danger"> Backordered</span>
|
||||||
|
</h5>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
class ProductList extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = { products };
|
||||||
|
}
|
||||||
|
|
||||||
|
toggleInStock = () => {
|
||||||
|
let newProducts = [...this.state.products];
|
||||||
|
newProducts = newProducts.map((d) => {
|
||||||
|
if (d.id === 13) {
|
||||||
|
return {
|
||||||
|
...d,
|
||||||
|
inStock: !d.inStock
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return d;
|
||||||
|
});
|
||||||
|
this.setState(curr => ({ ...curr, products: newProducts }));
|
||||||
|
};
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1 className="h2">Products</h1>
|
||||||
|
<BootstrapTable
|
||||||
|
keyField="id"
|
||||||
|
data={ this.state.products }
|
||||||
|
columns={ columns }
|
||||||
|
/>
|
||||||
|
<button onClick={ this.toggleInStock } className="btn btn-primary">
|
||||||
|
Toggle item 13 stock status
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
class ProductList extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = { products };
|
||||||
|
}
|
||||||
|
|
||||||
|
toggleInStock = () => {
|
||||||
|
let newProducts = [...this.state.products];
|
||||||
|
newProducts = newProducts.map((d) => {
|
||||||
|
if (d.id === 13) {
|
||||||
|
return {
|
||||||
|
...d,
|
||||||
|
inStock: !d.inStock
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return d;
|
||||||
|
});
|
||||||
|
this.setState(curr => ({ ...curr, products: newProducts }));
|
||||||
|
};
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h3>Action 1 and Action 2 are dummy column</h3>
|
||||||
|
<button onClick={ this.toggleInStock } className="btn btn-primary">
|
||||||
|
Toggle item 13 stock status
|
||||||
|
</button>
|
||||||
|
<BootstrapTable
|
||||||
|
keyField="id"
|
||||||
|
data={ this.state.products }
|
||||||
|
columns={ columns }
|
||||||
|
/>
|
||||||
|
<Code>{ sourceCode }</Code>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ProductList;
|
||||||
@ -30,6 +30,7 @@ import ColumnTitleTable from 'examples/columns/column-title-table';
|
|||||||
import ColumnEventTable from 'examples/columns/column-event-table';
|
import ColumnEventTable from 'examples/columns/column-event-table';
|
||||||
import ColumnHiddenTable from 'examples/columns/column-hidden-table';
|
import ColumnHiddenTable from 'examples/columns/column-hidden-table';
|
||||||
import ColumnAttrsTable from 'examples/columns/column-attrs-table';
|
import ColumnAttrsTable from 'examples/columns/column-attrs-table';
|
||||||
|
import DummyColumnTable from 'examples/columns/dummy-column-table';
|
||||||
|
|
||||||
// work on header columns
|
// work on header columns
|
||||||
import HeaderColumnFormatTable from 'examples/header-columns/column-format-table';
|
import HeaderColumnFormatTable from 'examples/header-columns/column-format-table';
|
||||||
@ -201,7 +202,8 @@ storiesOf('Work on Columns', module)
|
|||||||
.add('Column Event', () => <ColumnEventTable />)
|
.add('Column Event', () => <ColumnEventTable />)
|
||||||
.add('Customize Column Class', () => <ColumnClassTable />)
|
.add('Customize Column Class', () => <ColumnClassTable />)
|
||||||
.add('Customize Column Style', () => <ColumnStyleTable />)
|
.add('Customize Column Style', () => <ColumnStyleTable />)
|
||||||
.add('Customize Column HTML attribute', () => <ColumnAttrsTable />);
|
.add('Customize Column HTML attribute', () => <ColumnAttrsTable />)
|
||||||
|
.add('Dummy Column', () => <DummyColumnTable />);
|
||||||
|
|
||||||
storiesOf('Work on Header Columns', module)
|
storiesOf('Work on Header Columns', module)
|
||||||
.addDecorator(bootstrapStyle())
|
.addDecorator(bootstrapStyle())
|
||||||
|
|||||||
@ -177,22 +177,45 @@ describe('Cell', () => {
|
|||||||
let props;
|
let props;
|
||||||
let nextProps;
|
let nextProps;
|
||||||
|
|
||||||
describe('when content is change', () => {
|
describe('if column.isDummyField is false', () => {
|
||||||
const column = { dataField: 'name', text: 'Product Name' };
|
describe('when content is change', () => {
|
||||||
beforeEach(() => {
|
const column = { dataField: 'name', text: 'Product Name' };
|
||||||
props = {
|
beforeEach(() => {
|
||||||
row,
|
props = {
|
||||||
columnIndex: 1,
|
row,
|
||||||
rowIndex: 1,
|
columnIndex: 1,
|
||||||
column
|
rowIndex: 1,
|
||||||
};
|
column
|
||||||
wrapper = shallow(
|
};
|
||||||
<Cell { ...props } />);
|
wrapper = shallow(
|
||||||
});
|
<Cell { ...props } />);
|
||||||
|
});
|
||||||
|
|
||||||
it('should return true', () => {
|
it('should return true', () => {
|
||||||
nextProps = { ...props, row: { id: 1, name: 'CDE' } };
|
nextProps = { ...props, row: { id: 1, name: 'CDE' } };
|
||||||
expect(wrapper.instance().shouldComponentUpdate(nextProps)).toBe(true);
|
expect(wrapper.instance().shouldComponentUpdate(nextProps)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('if column.isDummyField is true', () => {
|
||||||
|
describe('when content is change', () => {
|
||||||
|
const column = { dataField: '', text: 'Product Name', isDummyField: true };
|
||||||
|
beforeEach(() => {
|
||||||
|
props = {
|
||||||
|
row,
|
||||||
|
columnIndex: 1,
|
||||||
|
rowIndex: 1,
|
||||||
|
column
|
||||||
|
};
|
||||||
|
wrapper = shallow(
|
||||||
|
<Cell { ...props } />);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return true', () => {
|
||||||
|
nextProps = { ...props, row: { id: 1, name: 'CDE', test: 'This is new Field' } };
|
||||||
|
expect(wrapper.instance().shouldComponentUpdate(nextProps)).toBe(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user