diff --git a/packages/react-bootstrap-table2-toolkit/context.js b/packages/react-bootstrap-table2-toolkit/context.js
new file mode 100644
index 0000000..31373fa
--- /dev/null
+++ b/packages/react-bootstrap-table2-toolkit/context.js
@@ -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 (
+
+ { this.props.children }
+
+ );
+ }
+}
+
+export default {
+ Provider: ToolkitProvider,
+ Consumer: ToolkitContext.Consumer
+};
diff --git a/packages/react-bootstrap-table2-toolkit/index.js b/packages/react-bootstrap-table2-toolkit/index.js
new file mode 100644
index 0000000..03b2521
--- /dev/null
+++ b/packages/react-bootstrap-table2-toolkit/index.js
@@ -0,0 +1,4 @@
+import ToolkitContext from './context';
+
+export default ToolkitContext;
+export { default as Search } from './src/search';
diff --git a/packages/react-bootstrap-table2-toolkit/package.json b/packages/react-bootstrap-table2-toolkit/package.json
new file mode 100644
index 0000000..e0b9ad9
--- /dev/null
+++ b/packages/react-bootstrap-table2-toolkit/package.json
@@ -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"
+ }
+}
diff --git a/packages/react-bootstrap-table2-toolkit/src/search/SearchBar.js b/packages/react-bootstrap-table2-toolkit/src/search/SearchBar.js
new file mode 100644
index 0000000..99ee8ee
--- /dev/null
+++ b/packages/react-bootstrap-table2-toolkit/src/search/SearchBar.js
@@ -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 = 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;
diff --git a/packages/react-bootstrap-table2-toolkit/src/search/context.js b/packages/react-bootstrap-table2-toolkit/src/search/context.js
new file mode 100644
index 0000000..150612b
--- /dev/null
+++ b/packages/react-bootstrap-table2-toolkit/src/search/context.js
@@ -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 (
+
+ { this.props.children }
+
+ );
+ }
+ }
+
+ return {
+ Provider: SearchProvider,
+ Consumer: SearchContext.Consumer
+ };
+};
diff --git a/packages/react-bootstrap-table2-toolkit/src/search/index.js b/packages/react-bootstrap-table2-toolkit/src/search/index.js
new file mode 100644
index 0000000..06fc1d7
--- /dev/null
+++ b/packages/react-bootstrap-table2-toolkit/src/search/index.js
@@ -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 };
diff --git a/packages/react-bootstrap-table2/src/contexts/data-context.js b/packages/react-bootstrap-table2/src/contexts/data-context.js
index 8452509..3394c7a 100644
--- a/packages/react-bootstrap-table2/src/contexts/data-context.js
+++ b/packages/react-bootstrap-table2/src/contexts/data-context.js
@@ -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;
}
diff --git a/packages/react-bootstrap-table2/src/contexts/index.js b/packages/react-bootstrap-table2/src/contexts/index.js
index a7e80f5..031d699 100644
--- a/packages/react-bootstrap-table2/src/contexts/index.js
+++ b/packages/react-bootstrap-table2/src/contexts/index.js
@@ -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
) => (
{
@@ -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 = n }
pagination={ this.props.pagination }
- data={ rootProps.getData(filterProps, sortProps) }
+ data={ rootProps.getData(filterProps, searchProps, sortProps) }
>
{
@@ -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 = n }
defaultSorted={ this.props.defaultSorted }
defaultSortDirection={ this.props.defaultSortDirection }
- data={ rootProps.getData(filterProps) }
+ data={ rootProps.getData(filterProps, searchProps) }
>
{
@@ -135,7 +147,8 @@ const withContext = Base =>
rootProps,
cellEditProps,
filterProps,
- sortProps
+ searchProps,
+ sortProps,
)
}
@@ -143,6 +156,32 @@ const withContext = Base =>
);
}
+ renderWithSearchCtx(base, baseProps) {
+ return (
+ rootProps,
+ cellEditProps,
+ filterProps
+ ) => (
+ this.searchContext = n }
+ data={ rootProps.getData(filterProps) }
+ searchText={ this.props.search.searchText }
+ >
+
+ {
+ searchProps => base(
+ rootProps,
+ cellEditProps,
+ filterProps,
+ searchProps
+ )
+ }
+
+
+ );
+ }
+
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);
}
diff --git a/packages/react-bootstrap-table2/src/props-resolver/remote-resolver.js b/packages/react-bootstrap-table2/src/props-resolver/remote-resolver.js
index 62cdb7a..e942ac5 100644
--- a/packages/react-bootstrap-table2/src/props-resolver/remote-resolver.js
+++ b/packages/react-bootstrap-table2/src/props-resolver/remote-resolver.js
@@ -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 }));
+ }
};