react-bootstrap-table2/packages/react-bootstrap-table2-toolkit/context.js
2018-08-01 20:26:00 +08:00

101 lines
2.4 KiB
JavaScript

import React from 'react';
import PropTypes from 'prop-types';
import statelessDrcorator from './statelessOp';
import createContext from './src/search/context';
const ToolkitContext = React.createContext();
class ToolkitProvider extends statelessDrcorator(React.Component) {
static propTypes = {
keyField: PropTypes.string.isRequired,
data: PropTypes.array.isRequired,
columns: PropTypes.array.isRequired,
children: PropTypes.node.isRequired,
bootstrap4: PropTypes.bool,
search: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.shape({
searchFormatted: PropTypes.bool
})
]),
exportCSV: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.shape({
fileName: PropTypes.string,
separator: PropTypes.string,
ignoreHeader: PropTypes.bool,
noAutoBOM: PropTypes.bool
})
])
}
static defaultProps = {
search: false,
exportCSV: false,
bootstrap4: false
}
constructor(props) {
super(props);
this.state = {
searchText: ''
};
this._ = null;
this.onSearch = this.onSearch.bind(this);
this.setDependencyModules = this.setDependencyModules.bind(this);
}
onSearch(searchText) {
this.setState({ searchText });
}
/**
*
* @param {*} _
* this function will be called only one time when table render
* react-bootstrap-table-next/src/context/index.js will call this cb for passing the _ module
* Please consider to extract a common module to handle _ module.
* this is just a quick fix
*/
setDependencyModules(_) {
this._ = _;
}
render() {
const baseProps = {
keyField: this.props.keyField,
columns: this.props.columns,
data: this.props.data,
bootstrap4: this.props.bootstrap4,
setDependencyModules: this.setDependencyModules
};
if (this.props.search) {
baseProps.search = {
searchContext: createContext(this.props.search),
searchText: this.state.searchText
};
}
return (
<ToolkitContext.Provider value={ {
searchProps: {
onSearch: this.onSearch
},
csvProps: {
onExport: this.handleExportCSV
},
baseProps
} }
>
{ this.props.children }
</ToolkitContext.Provider>
);
}
}
export default {
Provider: ToolkitProvider,
Consumer: ToolkitContext.Consumer
};