mirror of
https://github.com/gosticks/react-bootstrap-table2.git
synced 2026-11-30 01:51:42 +00:00
implement table search
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
/* eslint react/prop-types: 0 */
|
||||
/* eslint react/require-default-props: 0 */
|
||||
import React from 'react';
|
||||
|
||||
const ToolkitContext = React.createContext();
|
||||
|
||||
class ToolkitProvider extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.test = false;
|
||||
this.searchProps = {
|
||||
onSearch: this.onSearch.bind(this),
|
||||
searchText: ''
|
||||
};
|
||||
}
|
||||
|
||||
onSearch(searchText) {
|
||||
this.searchProps = {
|
||||
...this.searchProps,
|
||||
searchText
|
||||
};
|
||||
this.forceUpdate();
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<ToolkitContext.Provider value={ {
|
||||
searchProps: this.searchProps
|
||||
} }
|
||||
>
|
||||
{ this.props.children }
|
||||
</ToolkitContext.Provider>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
Provider: ToolkitProvider,
|
||||
Consumer: ToolkitContext.Consumer
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
import ToolkitContext from './context';
|
||||
|
||||
export default ToolkitContext;
|
||||
export { default as Search } from './src/search';
|
||||
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"name": "react-bootstrap-table2-toolkit",
|
||||
"version": "0.1.0",
|
||||
"description": "The toolkit for react-bootstrap-table2",
|
||||
"main": "./lib/index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/react-bootstrap-table/react-bootstrap-table2.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "AllenFang",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"react",
|
||||
"bootstrap",
|
||||
"table",
|
||||
"grid",
|
||||
"react-bootstrap-table-addons",
|
||||
"react-component"
|
||||
],
|
||||
"contributors": [
|
||||
{
|
||||
"name": "Allen Fang",
|
||||
"email": "ayu780129@hotmail.com",
|
||||
"url": "https://github.com/AllenFang"
|
||||
},
|
||||
{
|
||||
"name": "Chun-MingChen",
|
||||
"email": "nick830314@gmail.com",
|
||||
"url": "https://github.com/Chun-MingChen"
|
||||
}
|
||||
],
|
||||
"peerDependencies": {
|
||||
"prop-types": "^15.0.0",
|
||||
"react": "^16.0.0",
|
||||
"react-dom": "^16.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/* eslint no-return-assign: 0 */
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
const handleDebounce = (func, wait, immediate) => {
|
||||
let timeout;
|
||||
|
||||
return () => {
|
||||
const later = () => {
|
||||
timeout = null;
|
||||
|
||||
if (!immediate) {
|
||||
func.apply(this, arguments);
|
||||
}
|
||||
};
|
||||
|
||||
const callNow = immediate && !timeout;
|
||||
|
||||
clearTimeout(timeout);
|
||||
|
||||
timeout = setTimeout(later, wait || 0);
|
||||
|
||||
if (callNow) {
|
||||
func.appy(this, arguments);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const SearchBar = ({
|
||||
delay,
|
||||
onSearch,
|
||||
className,
|
||||
style,
|
||||
placeholder,
|
||||
searchText,
|
||||
...rest
|
||||
}) => {
|
||||
let input;
|
||||
const debounceCallback = handleDebounce(() => {
|
||||
onSearch(input.value);
|
||||
}, delay);
|
||||
|
||||
return (
|
||||
<input
|
||||
ref={ n => input = n }
|
||||
type="text"
|
||||
style={ style }
|
||||
onKeyUp={ () => debounceCallback() }
|
||||
className={ `form-control ${className}` }
|
||||
placeholder={ placeholder || SearchBar.defaultProps.placeholder }
|
||||
{ ...rest }
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
SearchBar.propTypes = {
|
||||
onSearch: PropTypes.func.isRequired,
|
||||
className: PropTypes.string,
|
||||
placeholder: PropTypes.string,
|
||||
style: PropTypes.object,
|
||||
delay: PropTypes.number,
|
||||
searchText: PropTypes.string
|
||||
};
|
||||
|
||||
SearchBar.defaultProps = {
|
||||
className: '',
|
||||
style: {},
|
||||
placeholder: 'Search',
|
||||
delay: 250,
|
||||
searchText: ''
|
||||
};
|
||||
|
||||
export default SearchBar;
|
||||
@@ -0,0 +1,83 @@
|
||||
/* eslint react/prop-types: 0 */
|
||||
/* eslint react/require-default-props: 0 */
|
||||
/* eslint no-continue: 0 */
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
export default (options = {
|
||||
searchFormatted: false
|
||||
}) => (
|
||||
_,
|
||||
isRemoteSearch,
|
||||
handleRemoteSearchChange
|
||||
) => {
|
||||
const SearchContext = React.createContext();
|
||||
|
||||
class SearchProvider extends React.Component {
|
||||
static propTypes = {
|
||||
data: PropTypes.array.isRequired,
|
||||
columns: PropTypes.array.isRequired,
|
||||
searchText: PropTypes.string
|
||||
}
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.needToSearch = true;
|
||||
}
|
||||
|
||||
componentWillReceiveProps(nextProps) {
|
||||
if (nextProps.searchText !== this.props.searchText) {
|
||||
this.needToSearch = true;
|
||||
} else {
|
||||
this.needToSearch = false;
|
||||
}
|
||||
}
|
||||
|
||||
search() {
|
||||
const { data, columns } = this.props;
|
||||
let { searchText } = this.props;
|
||||
|
||||
if (!this.needToSearch) return data;
|
||||
|
||||
if (isRemoteSearch()) {
|
||||
handleRemoteSearchChange(searchText);
|
||||
return data;
|
||||
}
|
||||
|
||||
searchText = searchText.toLowerCase();
|
||||
return data.filter((row, ridx) => {
|
||||
for (let cidx = 0; cidx < columns.length; cidx += 1) {
|
||||
const column = columns[cidx];
|
||||
if (column.searchable === false) continue;
|
||||
let targetValue = _.get(row, column.dataField);
|
||||
if (column.formatter && options.searchFormatted) {
|
||||
targetValue = column.formatter(targetValue, row, ridx, column.formatExtraData);
|
||||
} else if (column.filterValue) {
|
||||
targetValue = column.filterValue(targetValue, row);
|
||||
}
|
||||
if (targetValue !== null && typeof targetValue !== 'undefined') {
|
||||
targetValue = targetValue.toString().toLowerCase();
|
||||
if (targetValue.indexOf(searchText) > -1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
const data = this.search();
|
||||
return (
|
||||
<SearchContext.Provider value={ { data } }>
|
||||
{ this.props.children }
|
||||
</SearchContext.Provider>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
Provider: SearchProvider,
|
||||
Consumer: SearchContext.Consumer
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
import SearchBar from './SearchBar';
|
||||
import createContext from './context';
|
||||
|
||||
|
||||
const searchFactory = ({ searchText, onSearch, ...options }) => ({
|
||||
createContext: createContext(options),
|
||||
searchText
|
||||
});
|
||||
|
||||
export default { SearchBar, searchFactory };
|
||||
@@ -16,9 +16,10 @@ export default () => {
|
||||
this.setState(() => ({ data: nextProps.data }));
|
||||
}
|
||||
|
||||
getData = (filterProps, sortProps, paginationProps) => {
|
||||
getData = (filterProps, searchProps, sortProps, paginationProps) => {
|
||||
if (paginationProps) return paginationProps.data;
|
||||
else if (sortProps) return sortProps.data;
|
||||
else if (searchProps) return searchProps.data;
|
||||
else if (filterProps) return filterProps.data;
|
||||
return this.props.data;
|
||||
}
|
||||
|
||||
+49
-6
@@ -37,6 +37,11 @@ const withContext = Base =>
|
||||
this.PaginationContext = props.pagination.createContext(
|
||||
this.isRemotePagination, this.handleRemotePageChange);
|
||||
}
|
||||
|
||||
if (props.search) {
|
||||
this.SearchContext = props.search.createContext(
|
||||
_, this.isRemoteSearch, this.handleRemoteSearchChange);
|
||||
}
|
||||
}
|
||||
|
||||
renderBase() {
|
||||
@@ -44,6 +49,7 @@ const withContext = Base =>
|
||||
rootProps,
|
||||
cellEditProps,
|
||||
filterProps,
|
||||
searchProps,
|
||||
sortProps,
|
||||
paginationProps,
|
||||
selectionProps
|
||||
@@ -54,8 +60,9 @@ const withContext = Base =>
|
||||
{ ...sortProps }
|
||||
{ ...cellEditProps }
|
||||
{ ...filterProps }
|
||||
{ ...searchProps }
|
||||
{ ...paginationProps }
|
||||
data={ rootProps.getData(filterProps, sortProps, paginationProps) }
|
||||
data={ rootProps.getData(filterProps, searchProps, sortProps, paginationProps) }
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -65,13 +72,14 @@ const withContext = Base =>
|
||||
rootProps,
|
||||
cellEditProps,
|
||||
filterProps,
|
||||
searchProps,
|
||||
sortProps,
|
||||
paginationProps
|
||||
) => (
|
||||
<this.SelectionContext.Provider
|
||||
{ ...baseProps }
|
||||
selectRow={ this.props.selectRow }
|
||||
data={ rootProps.getData(filterProps, sortProps, paginationProps) }
|
||||
data={ rootProps.getData(filterProps, searchProps, sortProps, paginationProps) }
|
||||
>
|
||||
<this.SelectionContext.Consumer>
|
||||
{
|
||||
@@ -79,6 +87,7 @@ const withContext = Base =>
|
||||
rootProps,
|
||||
cellEditProps,
|
||||
filterProps,
|
||||
searchProps,
|
||||
sortProps,
|
||||
paginationProps,
|
||||
selectionProps
|
||||
@@ -94,12 +103,13 @@ const withContext = Base =>
|
||||
rootProps,
|
||||
cellEditProps,
|
||||
filterProps,
|
||||
searchProps,
|
||||
sortProps
|
||||
) => (
|
||||
<this.PaginationContext.Provider
|
||||
ref={ n => this.paginationContext = n }
|
||||
pagination={ this.props.pagination }
|
||||
data={ rootProps.getData(filterProps, sortProps) }
|
||||
data={ rootProps.getData(filterProps, searchProps, sortProps) }
|
||||
>
|
||||
<this.PaginationContext.Consumer>
|
||||
{
|
||||
@@ -107,6 +117,7 @@ const withContext = Base =>
|
||||
rootProps,
|
||||
cellEditProps,
|
||||
filterProps,
|
||||
searchProps,
|
||||
sortProps,
|
||||
paginationProps
|
||||
)
|
||||
@@ -120,14 +131,15 @@ const withContext = Base =>
|
||||
return (
|
||||
rootProps,
|
||||
cellEditProps,
|
||||
filterProps
|
||||
filterProps,
|
||||
searchProps
|
||||
) => (
|
||||
<this.SortContext.Provider
|
||||
{ ...baseProps }
|
||||
ref={ n => this.sortContext = n }
|
||||
defaultSorted={ this.props.defaultSorted }
|
||||
defaultSortDirection={ this.props.defaultSortDirection }
|
||||
data={ rootProps.getData(filterProps) }
|
||||
data={ rootProps.getData(filterProps, searchProps) }
|
||||
>
|
||||
<this.SortContext.Consumer>
|
||||
{
|
||||
@@ -135,7 +147,8 @@ const withContext = Base =>
|
||||
rootProps,
|
||||
cellEditProps,
|
||||
filterProps,
|
||||
sortProps
|
||||
searchProps,
|
||||
sortProps,
|
||||
)
|
||||
}
|
||||
</this.SortContext.Consumer>
|
||||
@@ -143,6 +156,32 @@ const withContext = Base =>
|
||||
);
|
||||
}
|
||||
|
||||
renderWithSearchCtx(base, baseProps) {
|
||||
return (
|
||||
rootProps,
|
||||
cellEditProps,
|
||||
filterProps
|
||||
) => (
|
||||
<this.SearchContext.Provider
|
||||
{ ...baseProps }
|
||||
ref={ n => this.searchContext = n }
|
||||
data={ rootProps.getData(filterProps) }
|
||||
searchText={ this.props.search.searchText }
|
||||
>
|
||||
<this.SearchContext.Consumer>
|
||||
{
|
||||
searchProps => base(
|
||||
rootProps,
|
||||
cellEditProps,
|
||||
filterProps,
|
||||
searchProps
|
||||
)
|
||||
}
|
||||
</this.SearchContext.Consumer>
|
||||
</this.SearchContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
renderWithFilterCtx(base, baseProps) {
|
||||
return (
|
||||
rootProps,
|
||||
@@ -201,6 +240,10 @@ const withContext = Base =>
|
||||
base = this.renderWithSortCtx(base, baseProps);
|
||||
}
|
||||
|
||||
if (this.SearchContext) {
|
||||
base = this.renderWithSearchCtx(base, baseProps);
|
||||
}
|
||||
|
||||
if (this.FilterContext) {
|
||||
base = this.renderWithFilterCtx(base, baseProps);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ export default ExtendBase =>
|
||||
let sortField;
|
||||
let page;
|
||||
let sizePerPage;
|
||||
let searchText;
|
||||
let filters = {};
|
||||
|
||||
if (this.sortContext) {
|
||||
@@ -25,17 +26,27 @@ export default ExtendBase =>
|
||||
sizePerPage = this.paginationContext.currSizePerPage;
|
||||
}
|
||||
|
||||
if (this.searchContext) {
|
||||
searchText = this.searchContext.props.searchText;
|
||||
}
|
||||
|
||||
return {
|
||||
sortOrder,
|
||||
sortField,
|
||||
filters,
|
||||
page,
|
||||
sizePerPage,
|
||||
searchText,
|
||||
...state,
|
||||
data: this.props.data
|
||||
};
|
||||
}
|
||||
|
||||
isRemoteSearch = () => {
|
||||
const { remote } = this.props;
|
||||
return remote === true || (_.isObject(remote) && remote.search);
|
||||
}
|
||||
|
||||
isRemotePagination = () => {
|
||||
const { remote } = this.props;
|
||||
return remote === true || (_.isObject(remote) && remote.pagination);
|
||||
@@ -77,4 +88,8 @@ export default ExtendBase =>
|
||||
const cellEdit = { rowId, dataField, newValue };
|
||||
this.props.onTableChange('cellEdit', this.getNewestState({ cellEdit }));
|
||||
}
|
||||
|
||||
handleRemoteSearchChange = (searchText) => {
|
||||
this.props.onTableChange('search', this.getNewestState({ searchText }));
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user