Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2bf95d3

Browse files
construct context dynamically
1 parent 5a65347 commit 2bf95d3

File tree

3 files changed

+117
-14
lines changed

3 files changed

+117
-14
lines changed

‎packages/react-bootstrap-table2/src/contexts/data-context.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ export default () => {
1616
this.setState(() => ({ data: nextProps.data }));
1717
}
1818

19+
getData = (filterProps, sortProps) => {
20+
if (sortProps) return sortProps.data;
21+
else if (filterProps) return filterProps.data;
22+
return this.props.data;
23+
}
24+
1925
render() {
2026
return (
2127
<DataContext.Provider
2228
value={ {
23-
data: this.state.data
29+
data: this.state.data,
30+
getData: this.getData
2431
} }
2532
>
2633
{ this.props.children }

‎packages/react-bootstrap-table2/src/contexts/index.js

Lines changed: 108 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,20 @@ const withContext = (Base) => {
1919
constructor(props) {
2020
super(props);
2121
DataContext = createDataContext(props.data);
22-
SelectionContext = createSelectionContext(dataOperator);
23-
SortContext = createSortContext(dataOperator, this.isRemoteSort, this.handleSortChange);
22+
23+
if (props.columns.filter(col => col.sort).length > 0) {
24+
SortContext = createSortContext(dataOperator, this.isRemoteSort, this.handleSortChange);
25+
}
26+
27+
if (props.selectRow) {
28+
SelectionContext = createSelectionContext(dataOperator);
29+
}
30+
2431
if (props.cellEdit && props.cellEdit.createContext) {
2532
CellEditContext = props.cellEdit.createContext(
2633
_, dataOperator, this.isRemoteCellEdit, this.handleCellChange);
2734
}
35+
2836
if (props.filter) {
2937
FilterContext = props.filter.createContext(
3038
_, this.isRemoteFiltering, this.handleRemoteFilterChange);
@@ -37,7 +45,7 @@ const withContext = (Base) => {
3745
}
3846
}
3947

40-
renderBase(baseProps) {
48+
renderBaseTemp(baseProps) {
4149
return (rootProps, cellEditProps, filterProps) => (
4250
<SortContext.Provider
4351
{ ...baseProps }
@@ -76,28 +84,108 @@ const withContext = (Base) => {
7684
);
7785
}
7886

79-
renderWithFilter(base, baseProps) {
80-
return (rootProps, cellEditprops) => (
87+
renderBase() {
88+
return (
89+
rootProps,
90+
cellEditProps,
91+
filterProps,
92+
sortProps,
93+
selectionProps
94+
) => (
95+
<Base
96+
{ ...this.props }
97+
{ ...selectionProps }
98+
{ ...sortProps }
99+
{ ...cellEditProps }
100+
{ ...filterProps }
101+
data={ rootProps.getData(filterProps, sortProps) }
102+
/>
103+
);
104+
}
105+
106+
renderWithSelectionCtx(base, baseProps) {
107+
return (
108+
rootProps,
109+
cellEditProps,
110+
filterProps,
111+
sortProps
112+
) => (
113+
<SelectionContext.Provider
114+
{ ...baseProps }
115+
selectRow={ this.props.selectRow }
116+
data={ rootProps.getData(filterProps, sortProps) }
117+
>
118+
<SelectionContext.Consumer>
119+
{
120+
selectionProps => base(
121+
rootProps,
122+
cellEditProps,
123+
filterProps,
124+
sortProps,
125+
selectionProps
126+
)
127+
}
128+
</SelectionContext.Consumer>
129+
</SelectionContext.Provider>
130+
);
131+
}
132+
133+
renderWithSortCtx(base, baseProps) {
134+
return (
135+
rootProps,
136+
cellEditProps,
137+
filterProps
138+
) => (
139+
<SortContext.Provider
140+
{ ...baseProps }
141+
ref={ n => this.sortContext = n }
142+
defaultSorted={ this.props.defaultSorted }
143+
defaultSortDirection={ this.props.defaultSortDirection }
144+
data={ rootProps.getData(filterProps) }
145+
>
146+
<SortContext.Consumer>
147+
{
148+
sortProps => base(
149+
rootProps,
150+
cellEditProps,
151+
filterProps,
152+
sortProps
153+
)
154+
}
155+
</SortContext.Consumer>
156+
</SortContext.Provider>
157+
);
158+
}
159+
160+
renderWithFilterCtx(base, baseProps) {
161+
return (
162+
rootProps,
163+
cellEditprops
164+
) => (
81165
<FilterContext.Provider
82166
{ ...baseProps }
83167
ref={ n => this.filterContext = n }
84-
data={ rootProps.data }
168+
data={ rootProps.getData() }
85169
>
86170
<FilterContext.Consumer>
87171
{
88-
filterProps => base(rootProps, cellEditprops, filterProps)
172+
filterProps => base(
173+
rootProps,
174+
cellEditprops,
175+
filterProps
176+
)
89177
}
90178
</FilterContext.Consumer>
91179
</FilterContext.Provider>
92180
);
93181
}
94182

95-
renderWithCellEdit(base, baseProps) {
183+
renderWithCellEditCtx(base, baseProps) {
96184
return rootProps => (
97185
<CellEditContext.Provider
98186
{ ...baseProps }
99187
cellEdit={ this.props.cellEdit }
100-
data={ rootProps.data }
188+
data={ rootProps.getData() }
101189
>
102190
<CellEditContext.Consumer>
103191
{
@@ -112,14 +200,22 @@ const withContext = (Base) => {
112200
const { keyField, columns } = this.props;
113201
const baseProps = { keyField, columns };
114202

115-
let base = this.renderBase(baseProps);
203+
let base = this.renderBase();
204+
205+
if (SelectionContext) {
206+
base = this.renderWithSelectionCtx(base, baseProps);
207+
}
208+
209+
if (SortContext) {
210+
base = this.renderWithSortCtx(base, baseProps);
211+
}
116212

117213
if (FilterContext) {
118-
base = this.renderWithFilter(base, baseProps);
214+
base = this.renderWithFilterCtx(base, baseProps);
119215
}
120216

121217
if (CellEditContext) {
122-
base = this.renderWithCellEdit(base, baseProps);
218+
base = this.renderWithCellEditCtx(base, baseProps);
123219
}
124220

125221
return (

‎packages/react-bootstrap-table2/src/store/selection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _ from '../utils';
22
import { getRowByRowId } from './rows';
33

4-
export const isSelectedAll = (data, selected) => data.length === selected.length;
4+
export const isSelectedAll = (data, selected=[]) => data.length === selected.length;
55

66
export const isAnySelectedRow = (selected, skips = []) => {
77
if (skips.length === 0) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /