|
@@ -1,296 +0,0 @@
|
1
|
|
-// @flow
|
2
|
|
-
|
3
|
|
-import React, { Component } from 'react';
|
4
|
|
-import { SafeAreaView, Text, TouchableOpacity, View } from 'react-native';
|
5
|
|
-
|
6
|
|
-import { Icon } from '../../../icons';
|
7
|
|
-import { connect } from '../../../redux';
|
8
|
|
-
|
9
|
|
-import styles from './styles';
|
10
|
|
-
|
11
|
|
-/**
|
12
|
|
- * The type of the React {@code Component} props of {@link PagedList}.
|
13
|
|
- */
|
14
|
|
-type Props = {
|
15
|
|
-
|
16
|
|
- /**
|
17
|
|
- * The zero-based index of the page that should be rendered (selected) by
|
18
|
|
- * default.
|
19
|
|
- */
|
20
|
|
- defaultPage: number,
|
21
|
|
-
|
22
|
|
- /**
|
23
|
|
- * Indicates if the list is disabled or not.
|
24
|
|
- */
|
25
|
|
- disabled: boolean,
|
26
|
|
-
|
27
|
|
- /**
|
28
|
|
- * The Redux dispatch function.
|
29
|
|
- */
|
30
|
|
- dispatch: Function,
|
31
|
|
-
|
32
|
|
- /**
|
33
|
|
- * Callback to execute on page change.
|
34
|
|
- */
|
35
|
|
- onSelectPage: ?Function,
|
36
|
|
-
|
37
|
|
- /**
|
38
|
|
- * The pages of the PagedList component to be rendered.
|
39
|
|
- *
|
40
|
|
- * NOTE 1: An element's {@code component} may be {@code undefined} and then
|
41
|
|
- * it won't need to be rendered.
|
42
|
|
- *
|
43
|
|
- * NOTE 2: There must be at least one page available and enabled.
|
44
|
|
- */
|
45
|
|
- pages: Array<{
|
46
|
|
- component: ?Object,
|
47
|
|
- icon: string | number,
|
48
|
|
- title: string
|
49
|
|
- }>
|
50
|
|
-};
|
51
|
|
-
|
52
|
|
-/**
|
53
|
|
- * The type of the React {@code Component} state of {@link PagedList}.
|
54
|
|
- */
|
55
|
|
-type State = {
|
56
|
|
-
|
57
|
|
- /**
|
58
|
|
- * The currently selected page.
|
59
|
|
- */
|
60
|
|
- pageIndex: number
|
61
|
|
-};
|
62
|
|
-
|
63
|
|
-/**
|
64
|
|
- * A component that renders a paged list.
|
65
|
|
- *
|
66
|
|
- * @augments PagedList
|
67
|
|
- */
|
68
|
|
-class PagedList extends Component<Props, State> {
|
69
|
|
-
|
70
|
|
- /**
|
71
|
|
- * Initializes a new {@code PagedList} instance.
|
72
|
|
- *
|
73
|
|
- * @inheritdoc
|
74
|
|
- */
|
75
|
|
- constructor(props: Props) {
|
76
|
|
- super(props);
|
77
|
|
-
|
78
|
|
- this.state = {
|
79
|
|
- pageIndex: this._validatePageIndex(props.defaultPage)
|
80
|
|
- };
|
81
|
|
-
|
82
|
|
- // Bind event handlers so they are only bound once per instance.
|
83
|
|
- this._maybeRefreshSelectedPage
|
84
|
|
- = this._maybeRefreshSelectedPage.bind(this);
|
85
|
|
- }
|
86
|
|
-
|
87
|
|
- /**
|
88
|
|
- * Renders the component.
|
89
|
|
- *
|
90
|
|
- * @inheritdoc
|
91
|
|
- */
|
92
|
|
- render() {
|
93
|
|
- const { disabled } = this.props;
|
94
|
|
- const pages = this.props.pages.filter(({ component }) => component);
|
95
|
|
- let children;
|
96
|
|
-
|
97
|
|
- if (pages.length > 1) {
|
98
|
|
- children = this._renderPagedList(disabled);
|
99
|
|
- } else {
|
100
|
|
- children = React.createElement(
|
101
|
|
-
|
102
|
|
- // $FlowExpectedError
|
103
|
|
- /* type */ pages[0].component,
|
104
|
|
- /* props */ {
|
105
|
|
- disabled,
|
106
|
|
- style: styles.pagedList
|
107
|
|
- });
|
108
|
|
- }
|
109
|
|
-
|
110
|
|
- return (
|
111
|
|
- <View
|
112
|
|
- style = { [
|
113
|
|
- styles.pagedListContainer,
|
114
|
|
- disabled ? styles.pagedListContainerDisabled : null
|
115
|
|
- ] }>
|
116
|
|
- {
|
117
|
|
-
|
118
|
|
- // $FlowExpectedError
|
119
|
|
- children
|
120
|
|
- }
|
121
|
|
- </View>
|
122
|
|
- );
|
123
|
|
- }
|
124
|
|
-
|
125
|
|
- /**
|
126
|
|
- * Constructs the style of an indicator.
|
127
|
|
- *
|
128
|
|
- * @param {number} indicatorIndex - The index of the indicator.
|
129
|
|
- * @private
|
130
|
|
- * @returns {Object}
|
131
|
|
- */
|
132
|
|
- _getIndicatorStyle(indicatorIndex) {
|
133
|
|
- if (this.state.pageIndex === indicatorIndex) {
|
134
|
|
- return styles.pageIndicatorActive;
|
135
|
|
- }
|
136
|
|
-
|
137
|
|
- return null;
|
138
|
|
- }
|
139
|
|
-
|
140
|
|
- _maybeRefreshSelectedPage: ?boolean => void;
|
141
|
|
-
|
142
|
|
- /**
|
143
|
|
- * Components that this PagedList displays may have a refresh function to
|
144
|
|
- * refresh its content when displayed (or based on custom logic). This
|
145
|
|
- * function invokes this logic if it's present.
|
146
|
|
- *
|
147
|
|
- * @private
|
148
|
|
- * @param {boolean} isInteractive - If true this refresh was caused by
|
149
|
|
- * direct user interaction, false otherwise.
|
150
|
|
- * @returns {void}
|
151
|
|
- */
|
152
|
|
- _maybeRefreshSelectedPage(isInteractive: boolean = true) {
|
153
|
|
- const selectedPage = this.props.pages[this.state.pageIndex];
|
154
|
|
- let component;
|
155
|
|
-
|
156
|
|
- if (selectedPage && (component = selectedPage.component)) {
|
157
|
|
- // react-i18n / react-redux wrap components and thus we cannot access
|
158
|
|
- // the wrapped component's static methods directly.
|
159
|
|
- const component_ = component.WrappedComponent || component;
|
160
|
|
- const { refresh } = component_;
|
161
|
|
-
|
162
|
|
- refresh.call(component, this.props.dispatch, isInteractive);
|
163
|
|
- }
|
164
|
|
- }
|
165
|
|
-
|
166
|
|
- /**
|
167
|
|
- * Sets the selected page.
|
168
|
|
- *
|
169
|
|
- * @param {number} pageIndex - The index of the selected page.
|
170
|
|
- * @protected
|
171
|
|
- * @returns {void}
|
172
|
|
- */
|
173
|
|
- _onSelectPage(pageIndex: number) {
|
174
|
|
- return () => {
|
175
|
|
- // eslint-disable-next-line no-param-reassign
|
176
|
|
- pageIndex = this._validatePageIndex(pageIndex);
|
177
|
|
-
|
178
|
|
- const { onSelectPage } = this.props;
|
179
|
|
-
|
180
|
|
- onSelectPage && onSelectPage(pageIndex);
|
181
|
|
-
|
182
|
|
- this.setState({ pageIndex }, this._maybeRefreshSelectedPage);
|
183
|
|
- };
|
184
|
|
- }
|
185
|
|
-
|
186
|
|
- /**
|
187
|
|
- * Renders a single page of the page list.
|
188
|
|
- *
|
189
|
|
- * @private
|
190
|
|
- * @param {Object} page - The page to render.
|
191
|
|
- * @param {boolean} disabled - Renders the page disabled.
|
192
|
|
- * @returns {React$Node}
|
193
|
|
- */
|
194
|
|
- _renderPage(page, disabled) {
|
195
|
|
- if (!page.component) {
|
196
|
|
- return null;
|
197
|
|
- }
|
198
|
|
-
|
199
|
|
- return (
|
200
|
|
- <View style = { styles.pageContainer }>
|
201
|
|
- {
|
202
|
|
- React.createElement(
|
203
|
|
- page.component,
|
204
|
|
- {
|
205
|
|
- disabled
|
206
|
|
- })
|
207
|
|
- }
|
208
|
|
- </View>
|
209
|
|
- );
|
210
|
|
- }
|
211
|
|
-
|
212
|
|
- /**
|
213
|
|
- * Renders the paged list if multiple pages are to be rendered.
|
214
|
|
- *
|
215
|
|
- * @param {boolean} disabled - True if the rendered lists should be
|
216
|
|
- * disabled.
|
217
|
|
- * @returns {ReactElement}
|
218
|
|
- */
|
219
|
|
- _renderPagedList(disabled) {
|
220
|
|
- const { pages } = this.props;
|
221
|
|
- const { pageIndex } = this.state;
|
222
|
|
-
|
223
|
|
- return (
|
224
|
|
- <View style = { styles.pagedListContainer }>
|
225
|
|
- {
|
226
|
|
- this._renderPage(pages[pageIndex], disabled)
|
227
|
|
- }
|
228
|
|
- <SafeAreaView style = { styles.pageIndicatorContainer }>
|
229
|
|
- {
|
230
|
|
- pages.map((page, index) => this._renderPageIndicator(
|
231
|
|
- page, index, disabled
|
232
|
|
- ))
|
233
|
|
- }
|
234
|
|
- </SafeAreaView>
|
235
|
|
- </View>
|
236
|
|
- );
|
237
|
|
- }
|
238
|
|
-
|
239
|
|
- /**
|
240
|
|
- * Renders a page indicator (icon) for the page.
|
241
|
|
- *
|
242
|
|
- * @private
|
243
|
|
- * @param {Object} page - The page the indicator is rendered for.
|
244
|
|
- * @param {number} index - The index of the page the indicator is rendered
|
245
|
|
- * for.
|
246
|
|
- * @param {boolean} disabled - Renders the indicator disabled.
|
247
|
|
- * @returns {React$Node}
|
248
|
|
- */
|
249
|
|
- _renderPageIndicator(page, index, disabled) {
|
250
|
|
- if (!page.component) {
|
251
|
|
- return null;
|
252
|
|
- }
|
253
|
|
-
|
254
|
|
- return (
|
255
|
|
- <TouchableOpacity
|
256
|
|
- disabled = { disabled }
|
257
|
|
- key = { index }
|
258
|
|
- onPress = { this._onSelectPage(index) }
|
259
|
|
- style = { styles.pageIndicator } >
|
260
|
|
- <View style = { styles.pageIndicatorContent }>
|
261
|
|
- <Icon
|
262
|
|
- src = { page.icon }
|
263
|
|
- style = { [
|
264
|
|
- styles.pageIndicatorIcon,
|
265
|
|
- this._getIndicatorStyle(index)
|
266
|
|
- ] } />
|
267
|
|
- <Text
|
268
|
|
- style = { [
|
269
|
|
- styles.pageIndicatorText,
|
270
|
|
- this._getIndicatorStyle(index)
|
271
|
|
- ] }>
|
272
|
|
- { page.title }
|
273
|
|
- </Text>
|
274
|
|
- </View>
|
275
|
|
- </TouchableOpacity>
|
276
|
|
- );
|
277
|
|
- }
|
278
|
|
-
|
279
|
|
- /**
|
280
|
|
- * Validates the requested page index and returns a safe value.
|
281
|
|
- *
|
282
|
|
- * @private
|
283
|
|
- * @param {number} pageIndex - The requested page index.
|
284
|
|
- * @returns {number}
|
285
|
|
- */
|
286
|
|
- _validatePageIndex(pageIndex) {
|
287
|
|
- // pageIndex may point to a non-existing page if some of the pages are
|
288
|
|
- // disabled (their component property is undefined).
|
289
|
|
- const maxPageIndex
|
290
|
|
- = this.props.pages.filter(({ component }) => component).length - 1;
|
291
|
|
-
|
292
|
|
- return Math.max(0, Math.min(maxPageIndex, pageIndex));
|
293
|
|
- }
|
294
|
|
-}
|
295
|
|
-
|
296
|
|
-export default connect()(PagedList);
|