mirror of
https://github.com/gosticks/react-bootstrap-table2.git
synced 2025-10-16 11:55:39 +00:00
127 lines
2.7 KiB
JavaScript
127 lines
2.7 KiB
JavaScript
/* eslint react/prop-types: 0 */
|
|
/* eslint no-return-assign: 0 */
|
|
import React from 'react';
|
|
|
|
import BootstrapTable from 'react-bootstrap-table-next';
|
|
import ToolkitContext, { Search } from 'react-bootstrap-table2-toolkit';
|
|
import Code from 'components/common/code-block';
|
|
import { productsGenerator } from 'utils/common';
|
|
|
|
const products = productsGenerator();
|
|
const { searchFactory } = Search;
|
|
|
|
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';
|
|
import ToolkitContext, { Search } from 'react-bootstrap-table2-toolkit';
|
|
|
|
const { searchFactory } = Search;
|
|
const columns = [{
|
|
dataField: 'id',
|
|
text: 'Product ID'
|
|
}, {
|
|
dataField: 'name',
|
|
text: 'Product Name'
|
|
}, {
|
|
dataField: 'price',
|
|
text: 'Product Price'
|
|
}];
|
|
|
|
const MySearch = (props) => {
|
|
let input;
|
|
const handleClick = () => {
|
|
props.onSearch(input.value);
|
|
};
|
|
return (
|
|
<div>
|
|
<input
|
|
className="form-control"
|
|
style={ { backgroundColor: 'pink' } }
|
|
ref={ n => input = n }
|
|
type="text"
|
|
/>
|
|
<button className="btn btn-warning" onClick={ handleClick }>Click to Search!!</button>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
<ToolkitContext.Provider
|
|
keyField="id"
|
|
data={ products }
|
|
columns={ columns }
|
|
>
|
|
<ToolkitContext.Consumer>
|
|
{
|
|
props => (
|
|
<div>
|
|
<BootstrapTable
|
|
{ ...props.baseProps }
|
|
search={ searchFactory({
|
|
...props.searchProps
|
|
}) }
|
|
/>
|
|
<MySearch { ...props.searchProps } />
|
|
<br />
|
|
</div>
|
|
)
|
|
}
|
|
</ToolkitContext.Consumer>
|
|
</ToolkitContext.Provider>
|
|
`;
|
|
|
|
const MySearch = (props) => {
|
|
let input;
|
|
const handleClick = () => {
|
|
props.onSearch(input.value);
|
|
};
|
|
return (
|
|
<div>
|
|
<input
|
|
className="form-control"
|
|
style={ { backgroundColor: 'pink' } }
|
|
ref={ n => input = n }
|
|
type="text"
|
|
/>
|
|
<button className="btn btn-warning" onClick={ handleClick }>Click to Search!!</button>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default () => (
|
|
<div>
|
|
<ToolkitContext.Provider
|
|
keyField="id"
|
|
data={ products }
|
|
columns={ columns }
|
|
>
|
|
<ToolkitContext.Consumer>
|
|
{
|
|
props => (
|
|
<div>
|
|
<BootstrapTable
|
|
{ ...props.baseProps }
|
|
search={ searchFactory({
|
|
...props.searchProps
|
|
}) }
|
|
/>
|
|
<MySearch { ...props.searchProps } />
|
|
<br />
|
|
</div>
|
|
)
|
|
}
|
|
</ToolkitContext.Consumer>
|
|
</ToolkitContext.Provider>
|
|
<Code>{ sourceCode }</Code>
|
|
</div>
|
|
);
|