-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.js
162 lines (149 loc) · 4.19 KB
/
context.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* eslint no-param-reassign: 0 */
import React from 'react';
import PropTypes from 'prop-types';
import statelessDecorator from './statelessOp';
import createSearchContext from './src/search/context';
const ToolkitContext = React.createContext();
class ToolkitProvider extends statelessDecorator(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({
defaultSearch: PropTypes.string,
searchFormatted: PropTypes.bool
})
]),
exportCSV: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.shape({
fileName: PropTypes.string,
separator: PropTypes.string,
ignoreHeader: PropTypes.bool,
ignoreFooter: PropTypes.bool,
noAutoBOM: PropTypes.bool,
blobType: PropTypes.string,
exportAll: PropTypes.bool,
onlyExportFiltered: PropTypes.bool,
onlyExportSelection: PropTypes.bool
})
])
}
static defaultProps = {
search: false,
exportCSV: false,
bootstrap4: false
}
constructor(props) {
super(props);
const state = {};
this._ = null;
this.onClear = this.onClear.bind(this);
this.onSearch = this.onSearch.bind(this);
this.onColumnToggle = this.onColumnToggle.bind(this);
this.setDependencyModules = this.setDependencyModules.bind(this);
if (props.columnToggle) {
state.columnToggle = props.columns
.reduce((obj, column) => {
obj[column.dataField] = !column.hidden;
return obj;
}, {});
}
state.searchText = typeof props.search === 'object' ? (props.search.defaultSearch || '') : '';
this.state = state;
}
// eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
let columnToggle = this.state.columnToggle;
if (nextProps.columnToggle) {
columnToggle = nextProps.columns
.reduce((obj, column) => {
obj[column.dataField] = !column.hidden;
return obj;
}, {});
} else {
columnToggle = null;
}
this.setState({
...this.state,
columnToggle
});
}
onSearch(searchText) {
if (searchText !== this.state.searchText) {
this.setState({ searchText });
}
}
onClear() {
this.setState({ searchText: '' });
}
onColumnToggle(dataField) {
const { columnToggle } = this.state;
columnToggle[dataField] = !columnToggle[dataField];
this.setState(({
...this.state,
columnToggle
}));
}
/**
*
* @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,
registerExposedAPI: this.registerExposedAPI
};
if (this.props.search) {
baseProps.search = {
searchContext: createSearchContext(this.props.search),
searchText: this.state.searchText
};
}
if (this.props.columnToggle) {
baseProps.columnToggle = {
toggles: this.state.columnToggle
};
}
return (
<ToolkitContext.Provider value={ {
searchProps: {
searchText: this.state.searchText,
onSearch: this.onSearch,
onClear: this.onClear
},
csvProps: {
onExport: this.handleExportCSV
},
columnToggleProps: {
columns: this.props.columns,
toggles: this.state.columnToggle,
onColumnToggle: this.onColumnToggle
},
baseProps
} }
>
{ this.props.children }
</ToolkitContext.Provider>
);
}
}
export default {
Provider: ToolkitProvider,
Consumer: ToolkitContext.Consumer
};