|
| 1 | +/* eslint react/prop-types: 0 */ |
| 2 | +/* eslint no-unused-vars: 0 */ |
| 3 | +import React from 'react'; |
| 4 | + |
| 5 | +import BootstrapTable from 'react-bootstrap-table-next'; |
| 6 | +import ToolkitProvider, { Search } from 'react-bootstrap-table2-toolkit'; |
| 7 | +import Code from 'components/common/code-block'; |
| 8 | +import { productsGenerator } from 'utils/common'; |
| 9 | + |
| 10 | +const { SearchBar } = Search; |
| 11 | +const products = productsGenerator(); |
| 12 | + |
| 13 | +const columns = [{ |
| 14 | + dataField: 'id', |
| 15 | + text: 'Product ID' |
| 16 | +}, { |
| 17 | + dataField: 'name', |
| 18 | + text: 'Product Name' |
| 19 | +}, { |
| 20 | + dataField: 'price', |
| 21 | + text: 'Product Price' |
| 22 | +}]; |
| 23 | + |
| 24 | +const sourceCode = `\ |
| 25 | +import BootstrapTable from 'react-bootstrap-table-next'; |
| 26 | +import ToolkitProvider, { Search } from 'react-bootstrap-table2-toolkit'; |
| 27 | +
|
| 28 | +const { SearchBar } = Search; |
| 29 | +const columns = [{ |
| 30 | + dataField: 'id', |
| 31 | + text: 'Product ID' |
| 32 | +}, { |
| 33 | + dataField: 'name', |
| 34 | + text: 'Product Name' |
| 35 | +}, { |
| 36 | + dataField: 'price', |
| 37 | + text: 'Product Price' |
| 38 | +}]; |
| 39 | +
|
| 40 | +// Implement startWith instead of contain |
| 41 | +function customMatchFunc({ |
| 42 | + searchText, |
| 43 | + value, |
| 44 | + column, |
| 45 | + row |
| 46 | +}) { |
| 47 | + if (typeof value !== 'undefined') { |
| 48 | + return value.startsWith(searchText); |
| 49 | + } |
| 50 | + return false; |
| 51 | +} |
| 52 | +
|
| 53 | +export default () => ( |
| 54 | + <div> |
| 55 | + <ToolkitProvider |
| 56 | + keyField="id" |
| 57 | + data={ products } |
| 58 | + columns={ columns } |
| 59 | + search={ { customMatchFunc } } |
| 60 | + > |
| 61 | + { |
| 62 | + props => ( |
| 63 | + <div> |
| 64 | + <h3>Input something at below input field:</h3> |
| 65 | + <SearchBar { ...props.searchProps } /> |
| 66 | + <hr /> |
| 67 | + <BootstrapTable |
| 68 | + { ...props.baseProps } |
| 69 | + /> |
| 70 | + </div> |
| 71 | + ) |
| 72 | + } |
| 73 | + </ToolkitProvider> |
| 74 | + <Code>{ sourceCode }</Code> |
| 75 | + </div> |
| 76 | +); |
| 77 | +`; |
| 78 | + |
| 79 | +// Implement startWith instead of contain |
| 80 | +function customMatchFunc({ |
| 81 | + searchText, |
| 82 | + value, |
| 83 | + column, |
| 84 | + row |
| 85 | +}) { |
| 86 | + if (typeof value !== 'undefined') { |
| 87 | + return `${value}`.toLowerCase().startsWith(searchText.toLowerCase()); |
| 88 | + } |
| 89 | + return false; |
| 90 | +} |
| 91 | + |
| 92 | +export default () => ( |
| 93 | + <div> |
| 94 | + <h1>Custom a search match function by startWith instead of contain</h1> |
| 95 | + <ToolkitProvider |
| 96 | + keyField="id" |
| 97 | + data={ products } |
| 98 | + columns={ columns } |
| 99 | + search={ { onColumnMatch: customMatchFunc } } |
| 100 | + > |
| 101 | + { |
| 102 | + props => ( |
| 103 | + <div> |
| 104 | + <h3>Input something at below input field:</h3> |
| 105 | + <SearchBar { ...props.searchProps } /> |
| 106 | + <hr /> |
| 107 | + <BootstrapTable |
| 108 | + { ...props.baseProps } |
| 109 | + /> |
| 110 | + </div> |
| 111 | + ) |
| 112 | + } |
| 113 | + </ToolkitProvider> |
| 114 | + <Code>{ sourceCode }</Code> |
| 115 | + </div> |
| 116 | +); |
0 commit comments