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 f6508ff

Browse files
construct context dynamically
1 parent 5a65347 commit f6508ff

File tree

3 files changed

+101
-37
lines changed

3 files changed

+101
-37
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: 92 additions & 35 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,67 +45,108 @@ const withContext = (Base) => {
3745
}
3846
}
3947

40-
renderBase(baseProps) {
41-
return (rootProps, cellEditProps, filterProps) => (
48+
renderBase() {
49+
return (
50+
rootProps,
51+
cellEditProps,
52+
filterProps,
53+
sortProps,
54+
selectionProps
55+
) => (
56+
<Base
57+
{ ...this.props }
58+
{ ...selectionProps }
59+
{ ...sortProps }
60+
{ ...cellEditProps }
61+
{ ...filterProps }
62+
data={ rootProps.getData(filterProps, sortProps) }
63+
/>
64+
);
65+
}
66+
67+
renderWithSelectionCtx(base, baseProps) {
68+
return (
69+
rootProps,
70+
cellEditProps,
71+
filterProps,
72+
sortProps
73+
) => (
74+
<SelectionContext.Provider
75+
{ ...baseProps }
76+
selectRow={ this.props.selectRow }
77+
data={ rootProps.getData(filterProps, sortProps) }
78+
>
79+
<SelectionContext.Consumer>
80+
{
81+
selectionProps => base(
82+
rootProps,
83+
cellEditProps,
84+
filterProps,
85+
sortProps,
86+
selectionProps
87+
)
88+
}
89+
</SelectionContext.Consumer>
90+
</SelectionContext.Provider>
91+
);
92+
}
93+
94+
renderWithSortCtx(base, baseProps) {
95+
return (
96+
rootProps,
97+
cellEditProps,
98+
filterProps
99+
) => (
42100
<SortContext.Provider
43101
{ ...baseProps }
44102
ref={ n => this.sortContext = n }
45103
defaultSorted={ this.props.defaultSorted }
46104
defaultSortDirection={ this.props.defaultSortDirection }
47-
data={ filterProps ? filterProps.data : rootProps.data }
105+
data={ rootProps.getData(filterProps) }
48106
>
49107
<SortContext.Consumer>
50108
{
51-
sortProps => (
52-
<SelectionContext.Provider
53-
{ ...baseProps }
54-
selectRow={ this.props.selectRow }
55-
data={ sortProps.data }
56-
>
57-
<SelectionContext.Consumer>
58-
{
59-
selectionProps => (
60-
<Base
61-
{ ...this.props }
62-
{ ...selectionProps }
63-
{ ...sortProps }
64-
{ ...cellEditProps }
65-
{ ...filterProps }
66-
data={ sortProps.data }
67-
/>
68-
)
69-
}
70-
</SelectionContext.Consumer>
71-
</SelectionContext.Provider>
109+
sortProps => base(
110+
rootProps,
111+
cellEditProps,
112+
filterProps,
113+
sortProps
72114
)
73115
}
74116
</SortContext.Consumer>
75117
</SortContext.Provider>
76118
);
77119
}
78120

79-
renderWithFilter(base, baseProps) {
80-
return (rootProps, cellEditprops) => (
121+
renderWithFilterCtx(base, baseProps) {
122+
return (
123+
rootProps,
124+
cellEditprops
125+
) => (
81126
<FilterContext.Provider
82127
{ ...baseProps }
83128
ref={ n => this.filterContext = n }
84-
data={ rootProps.data }
129+
data={ rootProps.getData() }
85130
>
86131
<FilterContext.Consumer>
87132
{
88-
filterProps => base(rootProps, cellEditprops, filterProps)
133+
filterProps => base(
134+
rootProps,
135+
cellEditprops,
136+
filterProps
137+
)
89138
}
90139
</FilterContext.Consumer>
91140
</FilterContext.Provider>
92141
);
93142
}
94143

95-
renderWithCellEdit(base, baseProps) {
144+
renderWithCellEditCtx(base, baseProps) {
96145
return rootProps => (
97146
<CellEditContext.Provider
98147
{ ...baseProps }
99148
cellEdit={ this.props.cellEdit }
100-
data={ rootProps.data }
149+
data={ rootProps.getData() }
101150
>
102151
<CellEditContext.Consumer>
103152
{
@@ -112,14 +161,22 @@ const withContext = (Base) => {
112161
const { keyField, columns } = this.props;
113162
const baseProps = { keyField, columns };
114163

115-
let base = this.renderBase(baseProps);
164+
let base = this.renderBase();
165+
166+
if (SelectionContext) {
167+
base = this.renderWithSelectionCtx(base, baseProps);
168+
}
169+
170+
if (SortContext) {
171+
base = this.renderWithSortCtx(base, baseProps);
172+
}
116173

117174
if (FilterContext) {
118-
base = this.renderWithFilter(base, baseProps);
175+
base = this.renderWithFilterCtx(base, baseProps);
119176
}
120177

121178
if (CellEditContext) {
122-
base = this.renderWithCellEdit(base, baseProps);
179+
base = this.renderWithCellEditCtx(base, baseProps);
123180
}
124181

125182
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 によって変換されたページ (->オリジナル) /