-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
BaseOptionsList.js
185 lines (164 loc) · 6.21 KB
/
BaseOptionsList.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import _ from 'underscore';
import React, {forwardRef, Component} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import Log from '../../libs/Log';
import styles from '../../styles/styles';
import OptionRow from '../OptionRow';
import SectionList from '../SectionList';
import Text from '../Text';
import {propTypes as optionsListPropTypes, defaultProps as optionsListDefaultProps} from './optionsListPropTypes';
const propTypes = {
/** Determines whether the keyboard gets dismissed in response to a drag */
keyboardDismissMode: PropTypes.string,
/** Called when the user begins to drag the scroll view */
onScrollBeginDrag: PropTypes.func,
...optionsListPropTypes,
};
const defaultProps = {
keyboardDismissMode: 'none',
onScrollBeginDrag: () => {},
...optionsListDefaultProps,
};
class BaseOptionsList extends Component {
constructor(props) {
super(props);
this.renderItem = this.renderItem.bind(this);
this.renderSectionHeader = this.renderSectionHeader.bind(this);
this.extractKey = this.extractKey.bind(this);
this.onScrollToIndexFailed = this.onScrollToIndexFailed.bind(this);
this.onViewableItemsChanged = this.onViewableItemsChanged.bind(this);
this.viewabilityConfig = {viewAreaCoveragePercentThreshold: 95};
this.didLayout = false;
}
shouldComponentUpdate(nextProps) {
if (nextProps.focusedIndex !== this.props.focusedIndex) {
return true;
}
if (nextProps.selectedOptions.length !== this.props.selectedOptions.length) {
return true;
}
if (nextProps.headerMessage !== this.props.headerMessage) {
return true;
}
if (!_.isEqual(nextProps.sections, this.props.sections)) {
return true;
}
return false;
}
onViewableItemsChanged() {
if (this.didLayout || !this.props.onLayout) {
return;
}
this.didLayout = true;
this.props.onLayout();
}
/**
* We must implement this method in order to use the ref.scrollToLocation() method.
* See: https://reactnative.dev/docs/sectionlist#scrolltolocation
*
* @param {Object} info
*/
onScrollToIndexFailed(info) {
Log.hmmm('[OptionsList] scrollToIndex failed', info);
}
/**
* Returns the key used by the list
* @param {Object} option
* @return {String}
*/
extractKey(option) {
return option.keyForList;
}
/**
* Function which renders a row in the list
*
* @param {Object} params
* @param {Object} params.item
* @param {Number} params.index
* @param {Object} params.section
*
* @return {Component}
*/
renderItem({item, index, section}) {
return (
<OptionRow
option={item}
mode={this.props.optionMode}
showTitleTooltip={this.props.showTitleTooltip}
backgroundColor={this.props.optionBackgroundColor}
hoverStyle={this.props.optionHoveredStyle}
optionIsFocused={!this.props.disableFocusOptions
&& this.props.focusedIndex === (index + section.indexOffset)}
onSelectRow={this.props.onSelectRow}
isSelected={Boolean(_.find(this.props.selectedOptions, option => option.login === item.login))}
showSelectedState={this.props.canSelectMultipleOptions}
hideAdditionalOptionStates={this.props.hideAdditionalOptionStates}
forceTextUnreadStyle={this.props.forceTextUnreadStyle}
isDisabled={this.props.isDisabled}
/>
);
}
/**
* Function which renders a section header component
*
* @param {Object} params
* @param {Object} params.section
* @param {String} params.section.title
* @param {Boolean} params.section.shouldShow
*
* @return {Component}
*/
renderSectionHeader({section: {title, shouldShow}}) {
if (title && shouldShow && !this.props.hideSectionHeaders) {
return (
<View>
<Text style={[styles.p5, styles.textMicroBold, styles.colorHeading, styles.textUppercase]}>
{title}
</Text>
</View>
);
}
return <View />;
}
render() {
return (
<View style={this.props.listContainerStyles}>
{this.props.headerMessage ? (
<View style={[styles.ph5, styles.pb5]}>
<Text style={[styles.textLabel, styles.colorMuted]}>
{this.props.headerMessage}
</Text>
</View>
) : null}
<SectionList
ref={this.props.innerRef}
indicatorStyle="white"
keyboardShouldPersistTaps="always"
keyboardDismissMode={this.props.keyboardDismissMode}
onScrollBeginDrag={this.props.onScrollBeginDrag}
contentContainerStyle={this.props.contentContainerStyles}
showsVerticalScrollIndicator={false}
sections={_.filter(this.props.sections, section => section.data.length !== 0)}
keyExtractor={this.extractKey}
onScrollToIndexFailed={this.onScrollToIndexFailed}
stickySectionHeadersEnabled={false}
renderItem={this.renderItem}
renderSectionHeader={this.renderSectionHeader}
extraData={this.props.focusedIndex}
initialNumToRender={5}
maxToRenderPerBatch={5}
windowSize={5}
viewabilityConfig={this.viewabilityConfig}
onViewableItemsChanged={this.onViewableItemsChanged}
/>
</View>
);
}
}
BaseOptionsList.propTypes = propTypes;
BaseOptionsList.defaultProps = defaultProps;
export default forwardRef((props, ref) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<BaseOptionsList {...props} innerRef={ref} />
));