|
@@ -55,16 +55,26 @@ export default class AbstractPagedList extends Component<Props, State> {
|
55
|
55
|
constructor(props: Props) {
|
56
|
56
|
super(props);
|
57
|
57
|
|
58
|
|
- // props.defaultPage may point to a non existing page if some of the
|
59
|
|
- // pages are disabled.
|
60
|
|
- const maxPageIndex
|
61
|
|
- = props.pages.filter(page => page.component).length - 1;
|
62
|
|
-
|
63
|
58
|
this.state = {
|
64
|
|
- pageIndex: Math.max(0, Math.min(maxPageIndex, props.defaultPage))
|
|
59
|
+ pageIndex: this._validatePageIndex(props.defaultPage)
|
65
|
60
|
};
|
66
|
61
|
}
|
67
|
62
|
|
|
63
|
+ /**
|
|
64
|
+ * Implements React {@code Component}'s componentWillReceiveProps.
|
|
65
|
+ *
|
|
66
|
+ * @inheritdoc
|
|
67
|
+ */
|
|
68
|
+ componentWillReceiveProps(newProps: Props) {
|
|
69
|
+ const { defaultPage } = newProps;
|
|
70
|
+
|
|
71
|
+ if (defaultPage !== this.props.defaultPage) {
|
|
72
|
+ // Default page changed due to a redux update. This is likely to
|
|
73
|
+ // happen after APP_WILL_MOUNT. So we update the active tab.
|
|
74
|
+ this._platformSpecificPageSelect(defaultPage);
|
|
75
|
+ }
|
|
76
|
+ }
|
|
77
|
+
|
68
|
78
|
/**
|
69
|
79
|
* Renders the component.
|
70
|
80
|
*
|
|
@@ -95,6 +105,20 @@ export default class AbstractPagedList extends Component<Props, State> {
|
95
|
105
|
);
|
96
|
106
|
}
|
97
|
107
|
|
|
108
|
+ _platformSpecificPageSelect: number => void
|
|
109
|
+
|
|
110
|
+ /**
|
|
111
|
+ * Method to be overriden by the components implementing this abstract class
|
|
112
|
+ * to handle platform specific actions on page select.
|
|
113
|
+ *
|
|
114
|
+ * @protected
|
|
115
|
+ * @param {number} pageIndex - The selected page index.
|
|
116
|
+ * @returns {void}
|
|
117
|
+ */
|
|
118
|
+ _platformSpecificPageSelect(pageIndex) {
|
|
119
|
+ this._selectPage(pageIndex);
|
|
120
|
+ }
|
|
121
|
+
|
98
|
122
|
_renderPagedList: boolean => React$Node;
|
99
|
123
|
|
100
|
124
|
_selectPage: number => void;
|
|
@@ -107,13 +131,15 @@ export default class AbstractPagedList extends Component<Props, State> {
|
107
|
131
|
* @returns {void}
|
108
|
132
|
*/
|
109
|
133
|
_selectPage(pageIndex: number) {
|
|
134
|
+ const validatedPageIndex = this._validatePageIndex(pageIndex);
|
|
135
|
+
|
110
|
136
|
this.setState({
|
111
|
|
- pageIndex
|
|
137
|
+ pageIndex: validatedPageIndex
|
112
|
138
|
});
|
113
|
139
|
|
114
|
140
|
// The page's Component may have a refresh(dispatch) function which we
|
115
|
141
|
// invoke when the page is selected.
|
116
|
|
- const selectedPage = this.props.pages[pageIndex];
|
|
142
|
+ const selectedPage = this.props.pages[validatedPageIndex];
|
117
|
143
|
|
118
|
144
|
if (selectedPage && selectedPage.component) {
|
119
|
145
|
const { refresh } = selectedPage.component;
|
|
@@ -121,4 +147,22 @@ export default class AbstractPagedList extends Component<Props, State> {
|
121
|
147
|
typeof refresh === 'function' && refresh(this.props.dispatch);
|
122
|
148
|
}
|
123
|
149
|
}
|
|
150
|
+
|
|
151
|
+ _validatePageIndex: number => number
|
|
152
|
+
|
|
153
|
+ /**
|
|
154
|
+ * Validates the requested page index and returns a safe value.
|
|
155
|
+ *
|
|
156
|
+ * @private
|
|
157
|
+ * @param {number} pageIndex - The requested page index.
|
|
158
|
+ * @returns {number}
|
|
159
|
+ */
|
|
160
|
+ _validatePageIndex(pageIndex) {
|
|
161
|
+ // pageIndex may point to a non existing page if some of the pages are
|
|
162
|
+ // disabled (their component property is undefined).
|
|
163
|
+ const maxPageIndex
|
|
164
|
+ = this.props.pages.filter(page => page.component).length - 1;
|
|
165
|
+
|
|
166
|
+ return Math.max(0, Math.min(maxPageIndex, pageIndex));
|
|
167
|
+ }
|
124
|
168
|
}
|